c# - 基于Bool而没有if语句的方法调用

时间:2014-10-28 10:02:32

标签: c# methods boolean-logic

很难制定标题。 但是,我会很快解释我目前的选择,希望有人可以告诉我一个更简洁的方法来完成这项工作。


我们的第一个“快速”解决方案是:

public class Test
{
        public bool UseSomething = false;
        public bool UseSomethingElse = false;
        public bool UseAnother = false;

    public void MyMethod(){

        if(UseSomething){
            if(UseSomethingElse){
                if(UseAnother){
                    // UseSomething, UseSomethingElse, UseAnother
                }
                else{
                    // UseSomething, UseSomethingElse
                }
            }
            else{
                // UseSomething
            }
        }
        else if(UseSomethingElse){
            if(UseAnother){
                // UseSomethingElse, UseAnother
            }
            else{
                // UseSomethingElse
            }
        }

        // etc...

    }
}

现在这在我看来是一个丑陋的解决方案,并且很快变得混乱,特别是如果你想添加选项。更不用说,除了我自己以外的任何人都会在第一眼就迷失在何处去/改变/无论如何。

所以我很快想出了另一个解决方案如下:

public class Test
{
    public bool UseSomething = false;
    public bool UseSomethingElse = false;
    public bool UseAnother = false;
    short options = 0;

    public void Init() // call this @ start of your program
    {
        if (UseSomething)
            options += 1;
        if (UseSomethingElse)
            options += 2;
        if (UseAnother)
            options += 4;
    }

    public void MyMethod(){ 
        Something something = MatchOption(foo); 
    }
    public void MatchOption(Foo foo)
    {
        switch (options) // based on the Options value (which is unique for every bool-triggered) perform a specific method.
        {
            case 0: //000
                return NoOptions(foo);
            case 1: //100
                return OptionSomething(foo);
            case 2: //010
                return OptionSomethingElse(foo);
            case 4: //001
                return ... etc;
            case 3: //110
                break; 
            case 5: //101
                break;
            case 6: //011
                break;
            case 7: // 111
                break;
            case -1:
                return;
        }
    }
}

现在,这使得它更易于管理,人们基本上不必担心将if放入哪个if / else语句。此外,方法是干净的,只做它们应该做的事情。

但我仍然不能放手,必须有其他方法来做到这一点。

这不是一个代码不起作用的问题。更重要的是,我想要一个“最好”或“最干净”的方式来做到这一点。 ^ _ ^ 我是软件工程师的第三年学生,仍在寻找清理或优化代码的方法。

如果您有任何意见或建议,请告诉我们!

注意:这主要是伪代码,我没有运行或测试过这个。这不是关于工作,这是我想要弄清楚的一个概念。

2 个答案:

答案 0 :(得分:1)

我会去做一个更实用的方法。

首先是DoSomething层次结构。

abstract class BaseDoingThings
{
    abstract void Do();
}

class Something : BaseDoingThings
{
    override Do() { ... }
}

class SomethingElse : BaseDoingThings
{
    override Do() { ... }
}

然后是测试类

class Test
{
    private List<BaseDoingThings> stuffToDo = new List<BaseDoingThings>();
    public void AddStuffToDo(BaseDoingThings todo)
    {
        stuffToDo.Add(todo);
    }

    public void Execute()
    {
        foreach(var stuff in stuffToDo)
        {
            stuff.Do();
        }
    }
}

这是基本的想法。现在你必须适应你的情况,这意味着你必须正确定义BaseDoingThings接口。

答案 1 :(得分:0)

你为什么不写:

if (useSomething)
    //use something
if (useSomethingElse)
    //use somethingElse
if (useAnother)
    //use another

如果你要添加新的布尔值,我猜你会用List来做到这一点:

List<bool> useThings = new List<bool>();
// populate the list
foreach (var useThing in useThings)
{
    if (useThing)
        //useThatThing
}