Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调用方法取决于某些设置?_C#_Design Patterns_Conditional - Fatal编程技术网

C# 调用方法取决于某些设置?

C# 调用方法取决于某些设置?,c#,design-patterns,conditional,C#,Design Patterns,Conditional,我有一些代码如下所示: if(someCondition) { SomeClass1.DoSomething(); SomeClass2.DoSomethingElse(); SomeClass3.DoSomethingElseAgain(); } if(someOtherCondition) { SomeClass4.WhatEver(); } 现在,有时候所有这些方法都应该被调用,有时候只是一些。例如,有时我只想打电话 SomeClass2.DoSomet

我有一些代码如下所示:

if(someCondition)
{
    SomeClass1.DoSomething();
    SomeClass2.DoSomethingElse();
    SomeClass3.DoSomethingElseAgain();
}

if(someOtherCondition)
{
    SomeClass4.WhatEver();
}
现在,有时候所有这些方法都应该被调用,有时候只是一些。例如,有时我只想打电话

SomeClass2.DoSomethingElse();
SomeClass3.DoSomethingElseAgain();
其余的不应该被称为。有没有一个好的模式或技巧来实现这一点


谢谢:-)

您可以创建一个
列表
,对于每个条件(如果(someCondition)部分),添加要调用到操作列表的方法,然后在循环结束时执行每个操作

如果您的方法与
操作
模式不匹配(零个或一个参数不返回值),则可以创建另一个自定义委托,该委托将以相同的方式进行操作

下面是一些伪代码:

static void Main(string[] args)
{
    List<Action> actionList = new List<Action>();

    if (true)
    {
        actionList.Add(new Action(DoSomething));
    }
    // etc.

    foreach (Action a in actionList)
    {
        a();
    }
}

static void DoSomething()
{
    // Code to do something.
}
static void Main(字符串[]args)
{
List actionList=新列表();
如果(真)
{
添加(新操作(DoSomething));
}
//等等。
foreach(操作列表中的操作a)
{
a();
}
}
静态空隙剂量测定法()
{
//代码来做某事。
}

我知道这似乎是一种更长、更复杂的方法,可以在OP的帖子中使用if/else方法,但在某些情况下,这实际上是一种更好的设计(只是不是所有情况)。很难知道什么能起到很好的作用,因为OP太模糊了。

您可以创建一个
列表
,对于每个条件(如果(someCondition)部分),将您想要调用的方法添加到操作列表中,然后在结束时循环操作并执行每个操作

如果您的方法与
操作
模式不匹配(零个或一个参数不返回值),则可以创建另一个自定义委托,该委托将以相同的方式进行操作

下面是一些伪代码:

static void Main(string[] args)
{
    List<Action> actionList = new List<Action>();

    if (true)
    {
        actionList.Add(new Action(DoSomething));
    }
    // etc.

    foreach (Action a in actionList)
    {
        a();
    }
}

static void DoSomething()
{
    // Code to do something.
}
static void Main(字符串[]args)
{
List actionList=新列表();
如果(真)
{
添加(新操作(DoSomething));
}
//等等。
foreach(操作列表中的操作a)
{
a();
}
}
静态空隙剂量测定法()
{
//代码来做某事。
}

我知道这似乎是一种更长、更复杂的方法,可以在OP的帖子中使用if/else方法,但在某些情况下,这实际上是一种更好的设计(只是不是所有情况)。很难知道什么会起作用,因为OP太模糊了。

在这种情况下,策略模式会很好地起作用:

例如:

class StrategyExample {

    public static void main(String[] args) {

        Context context;

        context = new Context(new SomeCondition());
        context.executeStrategy();

        context = new Context(new SomeOtherCondition());
        context.executeStrategy();
    }
}

// The classes that implement a concrete strategy should implement this. The context class uses this to call the concrete strategy
interface Strategy {
    void DoSomething(); 
}

// Implements the algorithm using the strategy interface
class SomeCondition implements Strategy {

    public void DoSomething() {
        // some condition code
    }
}

// Implements the algorithm using the strategy interface
class SomeOtherCondition implements Strategy {

    public void DoSomething() {
        // dome other condition code
    }
}

// Configured with a concrete strategy object
class Context {

    private Strategy strategy;

    // Constructor
    public Context(Strategy strategy) {
        this.strategy = strategy;
    }

    public void executeStrategy() {
        strategy.DoSomething();
    }
}

在这种情况下,策略模式会很好地工作:

例如:

class StrategyExample {

    public static void main(String[] args) {

        Context context;

        context = new Context(new SomeCondition());
        context.executeStrategy();

        context = new Context(new SomeOtherCondition());
        context.executeStrategy();
    }
}

// The classes that implement a concrete strategy should implement this. The context class uses this to call the concrete strategy
interface Strategy {
    void DoSomething(); 
}

// Implements the algorithm using the strategy interface
class SomeCondition implements Strategy {

    public void DoSomething() {
        // some condition code
    }
}

// Implements the algorithm using the strategy interface
class SomeOtherCondition implements Strategy {

    public void DoSomething() {
        // dome other condition code
    }
}

// Configured with a concrete strategy object
class Context {

    private Strategy strategy;

    // Constructor
    public Context(Strategy strategy) {
        this.strategy = strategy;
    }

    public void executeStrategy() {
        strategy.DoSomething();
    }
}

我想你可以使用下面的开关箱块

开关(外部ToLower()) { 大小写“.htm”: 大小写“.html”: type=“text/HTML”; 中断

            case ".txt":
                type = "text/plain";
                break;

            case ".doc":
            case ".rtf":
                type = "Application/msword";
                break;
            case ".xls":
                type = "application/vnd.ms-excel";
                break;
            case ".xlsx":
                type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                break;
        }

我想你可以使用下面的开关箱块

开关(外部ToLower()) { 大小写“.htm”: 大小写“.html”: type=“text/HTML”; 中断

            case ".txt":
                type = "text/plain";
                break;

            case ".doc":
            case ".rtf":
                type = "Application/msword";
                break;
            case ".xls":
                type = "application/vnd.ms-excel";
                break;
            case ".xlsx":
                type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                break;
        }

没什么,但我想避免太多的if/else。我希望有一些好的模式或什么的?没什么,但我想避免太多的if/else。我希望有一些好的模式或什么的?建议使用switch子句不是一个好主意。应该省略switch,您的答案不会让我们更接近简洁的解决方案。建议使用switch子句不是一个好主意。应该省略switch,而您的答案并不能使我们更接近简洁的解决方案。