战略设计模式和国家设计模式之间的区别是什么?我在网上浏览了不少文章,但看不出明显的区别。
有人能用外行的语言解释一下吗?
战略设计模式和国家设计模式之间的区别是什么?我在网上浏览了不少文章,但看不出明显的区别。
有人能用外行的语言解释一下吗?
当前回答
有人能用外行的话解释一下吗?
设计模式并不是真正的“门外汉”概念,但我将尽量使其清楚。任何设计模式都可以从三个维度来考虑:
模式解决的问题; 模式的静态结构(类图); 模式的动态(序列图)。
让我们比较国家和战略。
模式解决的问题
State有两种用法[GoF book p. 306]:
An object's behavior depends on its state, and it must change its behavior at run-time depending on that state. Operations have large, multipart conditional statements that depend on the object's state. This state is usually represented by one or more enumerated constants. Often, several operations will contain this same conditional structure. The State pattern puts each branch of the conditional in a separate class. This lets you treat the object's state as an object in its own right that can vary independently from other objects.
如果您希望确保您确实存在状态模式解决的问题,那么您应该能够使用有限状态机对对象的状态进行建模。你可以在这里找到一个应用的例子。
每个状态转换都是state接口中的一个方法。这意味着对于设计来说,在应用此模式之前必须非常确定状态转换。否则,如果您添加或删除转换,则需要更改接口和实现它的所有类。
我个人认为这种模式并不有用。您总是可以使用查找表实现有限状态机(这不是面向对象的方法,但它工作得非常好)。
策略用于以下[GoF书第316页]:
many related classes differ only in their behavior. Strategies provide a way to configure a class with one of many behaviors. you need different variants of an algorithm. For example, you might define algorithms reflecting different space/time trade-offs. Strategies can be used when these variants are implemented as a class hierarchy of algorithms [HO87]. an algorithm uses data that clients shouldn't know about. Use the Strategy pattern to avoid exposing complex, algorithm-specific data structures. a class defines many behaviors, and these appear as multiple conditional statements in its operations. Instead of many conditionals, move related conditional branches into their own Strategy class.
在哪里应用Strategy的最后一种情况与称为用多态性替换条件的重构有关。
总结:国家和战略解决的问题完全不同。如果您的问题不能用有限状态机建模,那么可能的状态模式就不合适。如果您的问题不是关于封装复杂算法的变体,那么Strategy就不适用。
模式的静态结构
State具有以下UML类结构:
Strategy具有以下UML类结构:
总结:就静态结构而言,这两种模式基本相同。事实上,像这样的模式检测工具认为“的结构 […]模式是相同的,禁止他们 通过自动过程进行区分(例如,不引用 到概念信息)。”
但是,如果ConcreteStates自己决定状态转换(参见上图中的“可能决定”关联),那么就会有很大的不同。这导致了具体状态之间的耦合。例如(请参阅下一节),状态A决定到状态b的转换。如果Context类决定到下一个具体状态的转换,这些依赖关系就消失了。
模式的动态性
正如上面的Problem部分所提到的,State意味着行为在运行时根据对象的某些状态而改变。因此,状态转换的概念适用于有限状态机的关系。[GoF]提到转换可以定义在ConcreteState子类中,也可以定义在一个集中的位置(比如基于表的位置)。
让我们假设一个简单的有限状态机:
假设子类决定状态转换(通过返回下一个状态对象),动态看起来像这样:
为了展示《战略》的动态,我们可以借用一个真实的例子。
Summary: Each pattern uses a polymorphic call to do something depending on the context. In the State pattern, the polymorphic call (transition) often causes a change in the next state. In the Strategy pattern, the polymorphic call does not typically change the context (e.g., paying by credit card once doesn't imply you'll pay by PayPal the next time). Again, the State pattern's dynamics are determined by its corresponding fininte state machine, which (to me) is essential to correct application of this pattern.
其他回答
战略模式和状态模式具有相同的结构。如果您查看两种模式的UML类图,它们看起来完全相同,但是它们的意图完全不同。状态设计模式用于定义和管理对象的状态,而策略模式用于定义一组可互换的算法,并让客户选择其中之一。因此,策略模式是客户端驱动的模式,而对象可以管理自己的状态。
不同之处在于它们解决的问题不同:
State模式处理对象(处于)什么(状态或类型)——它封装了依赖状态的行为,而 策略模式处理对象如何执行特定任务——它封装了一个算法。
然而,实现这些不同目标的结构非常相似;这两种模式都是带有委托的组合示例。
关于它们的优点:
通过使用State模式,状态保持(上下文)类不再知道它是什么状态或类型以及可用的状态或类型。这意味着类遵循开闭设计原则(OCP):类对状态/类型的更改是关闭的,但是状态/类型对扩展是开放的。
By using the Strategy pattern the algorithm-using (context) class is relieved from knowledge of how to perform a certain task (-- the "algorithm"). This case also creates an adherence to the OCP; the class is closed for changes regarding how to perform this task, but the design is very open to additions of other algorithms for solving this task. This likely also improves the context class' adherence to the single responsibility principle (SRP). Further the algorithm becomes easily available for reuse by other classes.
简而言之,使用策略模式,我们可以动态地设置一些行为,使用状态模式,我们可以确定,对象将随着状态的变化而在内部改变其行为。
有人能用外行的话解释一下吗?
设计模式并不是真正的“门外汉”概念,但我将尽量使其清楚。任何设计模式都可以从三个维度来考虑:
模式解决的问题; 模式的静态结构(类图); 模式的动态(序列图)。
让我们比较国家和战略。
模式解决的问题
State有两种用法[GoF book p. 306]:
An object's behavior depends on its state, and it must change its behavior at run-time depending on that state. Operations have large, multipart conditional statements that depend on the object's state. This state is usually represented by one or more enumerated constants. Often, several operations will contain this same conditional structure. The State pattern puts each branch of the conditional in a separate class. This lets you treat the object's state as an object in its own right that can vary independently from other objects.
如果您希望确保您确实存在状态模式解决的问题,那么您应该能够使用有限状态机对对象的状态进行建模。你可以在这里找到一个应用的例子。
每个状态转换都是state接口中的一个方法。这意味着对于设计来说,在应用此模式之前必须非常确定状态转换。否则,如果您添加或删除转换,则需要更改接口和实现它的所有类。
我个人认为这种模式并不有用。您总是可以使用查找表实现有限状态机(这不是面向对象的方法,但它工作得非常好)。
策略用于以下[GoF书第316页]:
many related classes differ only in their behavior. Strategies provide a way to configure a class with one of many behaviors. you need different variants of an algorithm. For example, you might define algorithms reflecting different space/time trade-offs. Strategies can be used when these variants are implemented as a class hierarchy of algorithms [HO87]. an algorithm uses data that clients shouldn't know about. Use the Strategy pattern to avoid exposing complex, algorithm-specific data structures. a class defines many behaviors, and these appear as multiple conditional statements in its operations. Instead of many conditionals, move related conditional branches into their own Strategy class.
在哪里应用Strategy的最后一种情况与称为用多态性替换条件的重构有关。
总结:国家和战略解决的问题完全不同。如果您的问题不能用有限状态机建模,那么可能的状态模式就不合适。如果您的问题不是关于封装复杂算法的变体,那么Strategy就不适用。
模式的静态结构
State具有以下UML类结构:
Strategy具有以下UML类结构:
总结:就静态结构而言,这两种模式基本相同。事实上,像这样的模式检测工具认为“的结构 […]模式是相同的,禁止他们 通过自动过程进行区分(例如,不引用 到概念信息)。”
但是,如果ConcreteStates自己决定状态转换(参见上图中的“可能决定”关联),那么就会有很大的不同。这导致了具体状态之间的耦合。例如(请参阅下一节),状态A决定到状态b的转换。如果Context类决定到下一个具体状态的转换,这些依赖关系就消失了。
模式的动态性
正如上面的Problem部分所提到的,State意味着行为在运行时根据对象的某些状态而改变。因此,状态转换的概念适用于有限状态机的关系。[GoF]提到转换可以定义在ConcreteState子类中,也可以定义在一个集中的位置(比如基于表的位置)。
让我们假设一个简单的有限状态机:
假设子类决定状态转换(通过返回下一个状态对象),动态看起来像这样:
为了展示《战略》的动态,我们可以借用一个真实的例子。
Summary: Each pattern uses a polymorphic call to do something depending on the context. In the State pattern, the polymorphic call (transition) often causes a change in the next state. In the Strategy pattern, the polymorphic call does not typically change the context (e.g., paying by credit card once doesn't imply you'll pay by PayPal the next time). Again, the State pattern's dynamics are determined by its corresponding fininte state machine, which (to me) is essential to correct application of this pattern.
这是一个相当老的问题,但我也在寻找同样的答案,这就是我所发现的。
对于状态模式,让我们考虑一个中间播放器播放按钮的例子。当我们开始播放时,它开始播放,并让上下文意识到它正在播放。每次客户端想要执行播放操作时,他都会检查播放器的当前状态。现在客户端知道对象的状态是通过上下文对象播放的,所以他调用暂停状态对象的动作方法。客户端实现状态的部分以及它需要在什么状态上执行操作的部分可以被自动化。
https://www.youtube.com/watch?v=e45RMc76884 https://www.tutorialspoint.com/design_pattern/state_pattern.htm
在策略模式的情况下,类图的安排与状态模式相同。客户来此安排做一些操作。也就是说,不同的算法取代了不同的状态,比如需要对模式进行不同的分析。在这里,客户端告诉上下文它想做什么,用什么算法(业务定义的自定义算法),然后执行。
https://www.tutorialspoint.com/design_pattern/strategy_pattern.htm
两者都实现了开闭原则,因此开发人员可以向状态模式和新算法中添加新的状态。
但不同之处在于它们使用的是基于对象状态执行不同逻辑的状态模式。在战略的案例中有不同的逻辑。