工厂与策略模式有什么区别? [英] What is the difference between Factory and Strategy patterns?

查看:218
本文介绍了工厂与策略模式有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何一个人可以解释工厂和战略模式之间的区别吗?



对于我来说,除了额外的工厂类别(在工厂模式下创建产品的对象)外,我们都是一样的。

解决方案

工厂模式是一种创意模式。战略模式是一种操作模式。换句话说,工厂模式用于创建特定类型的对象。策略模式用于以特定方式执行操作(或一组操作)。在经典的例子中,工厂可能会创建不同类型的动物:狗,猫,虎,而策略模式将执行特定的动作,例如Move;使用Run,Walk或Lope策略。



其实这两个可以一起使用。例如,您可能有一个创建业务对象的工厂。它可以使用基于持久性介质的不同策略。如果您的数据在本地存储在XML中,则会使用一种策略。如果数据远程在不同的数据库中,它将使用另一个。


Can any one explain the difference between factory and strategy patterns?

For me both are looking same other than an extra factory class (which create an object of product in factory patterns)

解决方案

A factory pattern is a creational pattern. A strategy pattern is an operational pattern. Put another way, a factory pattern is used to create objects of a specific type. A strategy pattern is use to perform an operation (or set of operations) in a particular manner. In the classic example, a factory might create different types of Animals: Dog, Cat, Tiger, while a strategy pattern would perform particular actions, for example, Move; using Run, Walk, or Lope strategies.

In fact the two can be used together. For example, you may have a factory that creates your business objects. It may use different strategies based on the persistence medium. If your data is stored locally in XML it would use one strategy. If the data were remote in a different database, it would use another.

这篇关于工厂与策略模式有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆