问题描述
我正在学习设计模式.
我遇到了工厂和策略设计模式,它们看起来非常重叠并且在使用中很相似.
两种模式都使用"new"关键字来返回对象(工厂)或return和算法(策略).
在概念上似乎不同,工厂用于相似类组的不同实例化.其中,如Strategy用于在算法之间进行切换.但是当我一起查看它们的示例时,它们看起来非常相似.
谁能帮我举一个合适的例子来区分它们及其正确用法.
在此先感谢.
Hi,
I am learning design patterns.
I came across factory and strategy design pattern which looks quite overlapping and similar in use.
Both patterns use "new" keyword to return an object(factory) or return and algorithm(strategy).
Conceptually it seems different, factory is used for differed instantiation of similar group of classes. where as Strategy is used for switching between algorithms. But when i see their examples together, they look quite similar.
Can anyone help me with a suitable example to differentiate them and their proper usage.
Thanks in advance.
推荐答案
这篇关于工厂和策略设计模式有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!