English 中文(简体)
避免多重情形的设计模式
原标题:A design pattern to avoid multiple ifs

I have a really awfull class with two methods that start or stops some services when that services are available. Something like the following (they are not if-elses, just if):

void startServices() { 
if (service1 == true) { 
  start1();
}
if (service2 == true) { 
  start2();
} if (serviceN == true) { 
  startN();
} 
} 


void stopServices() { 
if (service1 == true) { 
  stop1();
}
if (service2 == true) { 
  stop2();
}
if (serviceN == true) { 
  stopN();
}

}

你们是否推荐我任何设计赞助人,使其成为主人?

感谢!

最佳回答

依靠的是,我的第一个反应是将服务储存在一个散装或阵列中。 每个部门都采用开端和停用方法的接口。 开始或停止服务只需要服务钥匙或指数。

也许,它仍然只是一种微薄的碎片,但是,如果不知道有多少人不敢肯定如何“独一无二”,那么它会更像你所做的那样看待。

问题回答

You can use the Strategy pattern.

想法是,你应知道,当你当下课时,你会采取什么样的战略(或你可以动态地改变这种策略)。 因此,你可以在战略中即时通过(并在以后选择取代)。

public interface IStartupStrategy
{
    void Start();
}

public interface IStopStrategy
{
    void Stop();
}

public class MyClass
{
    private readonly IEnumerable<IStartupStrategy> startupStrategies;
    private readonly IEnumerable<IStopStrategy> stopStrategies;

    public MyClass(IEnumerable<IStartupStrategy> startup, IEnumerable<IStopStrategy> stop)
    {
        this.startupStrategies = startup;
        this.stopStrategies = stop;
    }

    public void Start()
    {
        foreach(var strategy in this.startupStrategies)
        {
            strategy.Start();
        }
    }

    public void Stop()
    {
        foreach(var strategy in this.stopStrategies)
        {
            strategy.Stop();
        }
    }
}

Use Objects, where you have a list of Services which you can iterate through turning them off with an inherited method stop().

public interface Service {
  void start();
  void stop();
}

public class TestService implements Service {
  @Override
  void start() {
  }

  @Override
  void stop() {
  }
}

Each service can also store its state so as to only turn them off if they are on.

换文不太令人混淆。 如果与斜体法一起使用,就非常可读。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签