如何不用策略模式去除if条件长度

如何不用策略模式去除if条件长度

消除if条件判断的长度,秘诀swtich的关键字fallthrough

fallthrough关键字

swtich的这个关键字,执行完事,不break,还继续向下执行

if痛点是什么

对于下面代码,看着还好,对于10多个字母的判断,那就很长了,看着不是很好(至少对于我来说),就是为了一个ok赋值,拆分为策略模式,还是大材小用的

	if this == "a" || this == "b" || this == "c" {
		ok = true
	}

用swtich解决

对于条件模式 || 修改

原来if代码
// || 形式改装
func isIf(this string) bool  {
	var ok bool

	ok =  false

	if this == "a" || this == "b" || this == "c" {
		ok = true
	}

	return ok
}
优化的swtich代码
func isSwitch(this string) bool {
	var ok bool

	ok = false

	switch this {
	case "a":
		ok = true
	case "b":
		ok =  true
	case "c":
		ok = true
	}

	return ok
}

对于条件模式 &&

原来的if代码

三个条件都看着很长了

// 且形式
func isIf2(this string) bool  {
	var ok bool

	ok =  false

	if strings.Contains(this, "a") && strings.Contains(this, "b") && strings.Contains(this, "c") {
		ok = true
	}

	return ok
}
优化swtich代码

实现思路:

  1. 传进去swtich的通用条件,我这里用了长度
  2. 每次符合条件,都记录一次
  3. 最后长度判断即可
func isSwitch2(this string) bool {
	var (
		ok bool
		okCount int // 符合的条件个数,最后判断即可
		do int
	)

	ok = false
	okCount = 0
	do = len(this)

	// 这里面的坑是必须都符合条件才能都执行
	switch len(this) {
	case do:
		if strings.Contains(this, "a") {
			okCount++
		}
		fallthrough
	case do:
		if strings.Contains(this, "b") {
			okCount++
		}
		fallthrough
	case do:
		if strings.Contains(this, "c") {
			okCount++
		}

		if okCount == 3 {
			ok = true
		}

		fmt.Println(okCount)
	}

	return ok
}

整体代码测试

package main

import (
	"fmt"
	"strings"
)

// || 形式改装
func isIf(this string) bool  {
	var ok bool

	ok =  false

	if this == "a" || this == "b" || this == "c" {
		ok = true
	}

	return ok
}

func isSwitch(this string) bool {
	var ok bool

	ok = false

	switch this {
	case "a":
		ok = true
	case "b":
		ok =  true
	case "c":
		ok = true
	}

	return ok
}

// 且形式
func isIf2(this string) bool  {
	var ok bool

	ok =  false

	if strings.Contains(this, "a") && strings.Contains(this, "b") && strings.Contains(this, "c") {
		ok = true
	}

	return ok
}

func isSwitch2(this string) bool {
	var (
		ok bool
		okCount int // 符合的条件个数,最后判断即可
		do int
	)

	ok = false
	okCount = 0
	do = len(this)

	// 这里面的坑是必须都符合条件才能都执行
	switch len(this) {
	case do:
		if strings.Contains(this, "a") {
			okCount++
		}
		fallthrough
	case do:
		if strings.Contains(this, "b") {
			okCount++
		}
		fallthrough
	case do:
		if strings.Contains(this, "c") {
			okCount++
		}

		if okCount == 3 {
			ok = true
		}

		fmt.Println(okCount)
	}

	return ok
}

func main() {
	fmt.Println("测试||形式")
	s1 := "b"
	resIf := isIf(s1)
	resSwtich := isSwitch(s1)
	fmt.Println("if结果:",resIf)
	fmt.Println("swtich结果:", resSwtich)

	fmt.Println("测试&&判断")
	s2 := "xabcx"
	resIf2 := isIf2(s2)
	resSwtich2 := isSwitch2(s2)
	fmt.Println("if2结果:",resIf2)
	fmt.Println("swtich2结果:", resSwtich2)

	fmt.Println("测试&&判断")
	s3 := "xabx"
	resIf3 := isIf2(s3)
	resSwtich3 := isSwitch2(s3)
	fmt.Println("if2结果:",resIf3)
	fmt.Println("swtich2结果:", resSwtich3)
}


## 结果输出
测试||形式
if结果: true
swtich结果: true
测试&&判断
3
if2结果: true
swtich2结果: true
测试&&判断
2
if3结果: false
swtich3结果: false

效率问题

对于运行时间来说,几乎没太大差别

代码方针

swtich就是把if横向代码变为竖向的

swtich多个条件的代码判断可以封装为func()

但是if的,封装为func()还是很长

选用swtich,自行衡量,根据判断,几个条件,还是if比swtich好

原文地址:https://www.cnblogs.com/maomaomaoge/p/14297879.html