简体   繁体   English

策略和工厂设计模式有什么区别?

[英]What is the difference between Strategy and Factory design pattern?

When I observe closely to Strategy and Factory design patterns Its almost looks similar. 当我仔细观察战略和工厂设计模式时,它看起来几乎相似。 We can achieve the solution with any one of these patterns. 我们可以使用这些模式中的任何一种来实现解决方案。 Then I wanted to know when to use what. 然后我想知道何时使用什么。

The Abstract Factory design pattern is used to solve problems like: Abstract Factory设计模式用于解决以下问题:
- How can a system be independent of how its objects are created? - 系统如何独立于其对象的创建方式?
- How can the way the objects are created be changed independently - 如何独立地更改对象的创建方式
(without having to change existing classes)? (无需更改现有类)?

The key idea in this pattern is to abstract the process of object creation. 这种模式的关键思想是抽象对象创建过程。
Clients (that need to create objects) refer to an abstraction (interface) and are independent of an implementation (how the objects are created / which concrete classes are instantiated). 客户端(需要创建对象)引用抽象(接口)并且独立于实现(如何创建对象/实例化哪些具体类)。

When not to use Abstract Factory: Object creation that does not change (when the concrete classes that get instantiated never change). 何时不使用抽象工厂:不改变的对象创建(当实例化的具体类永远不会改变时)。

The Strategy design pattern is used to solve problems like: Strategy设计模式用于解决以下问题:
- How can an object support using different algorithms - 对象如何支持使用不同的算法
so that which algorithm to use can be selected and changed dynamically? 那么可以动态选择和更改使用哪种算法?
- And how can the way an algorithm is implemented be changed independently - 如何实现算法的方式可以独立更改
(without having to change existing classes)? (无需更改现有类)?

For example, calculating prices in an order processing system. 例如,在订单处理系统中计算价格。
To calculate prices in different ways, it should be possible to support different pricing algorithms so that which algorithm to use can be selected and changed dynamically at run-time. 要以不同方式计算价格,应该可以支持不同的定价算法,以便可以在运行时动态选择和更改要使用的算法。

The key idea in this pattern is to decouple an algorithm (that changes) from its context (and encapsulate it in a separate object). 这种模式的关键思想是将算法(改变)与其上下文分离(并将其封装在单独的对象中)。

For further discussion see the GoF Design Patterns Memory for learning object-oriented design & programming at http://w3sdesign.com . 如需进一步讨论,请参阅在学习面向对象的设计和编程的GoF设计模式记忆http://w3sdesign.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 战略设计模式与抽象工厂模式之间的差异是什么? - What is the diffrence between strategy design pattern and abstract factory pattern? 策略设计模式和状态设计模式有什么区别? - What is the difference between Strategy design pattern and State design pattern? 工厂设计模式和DAO设计模式有什么区别 - What is difference between factory design pattern and DAO design pattern Java中的工厂设计模式与抽象工厂设计模式有什么区别? - what is the difference between factory and abstract factory design pattern in java? 目标c中的工厂设计模式与工厂方法设计模式和抽象工厂之间有什么区别? - What is the difference between Factory design pattern from Factory method design pattern and abstract factory in objective c? Factory方法设计模式和Bridge模式有什么区别? - What is the difference between Factory method design pattern and Bridge pattern? 服务定位器和工厂设计模式之间有什么区别? - What's the difference between the Service Locator and the Factory Design pattern? 桥接模式和策略模式有什么区别? - What is the difference between the bridge pattern and the strategy pattern? 策略模式和访问者模式有什么区别? - What is the difference between Strategy pattern and Visitor Pattern? 模板模式和策略模式有什么区别? - What is the difference between template pattern and strategy pattern?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM