设计模式命令和工厂哪个更好实现?

发布于 2024-10-26 02:49:00 字数 50 浏览 1 评论 0原文

我们还可以通过实现工厂和命令模式来解决一些设计问题。 那么,从性能来看,哪一个更好呢?

We can solve some Design Problems By implementing Factory as well as Command Pattern also.
so, according to performance which is the better one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落花浅忆 2024-11-02 02:49:00

我不确定如何通过工厂模式和命令模式解决一些问题。他们解决完全不同类型的问题。

抽象工厂以这样的方式处理对象的创建,您可以轻松地在产品系列之间切换,并且可以强制对象的一致性(即,不要意外地混合不同的产品系列)。即使只有一个产品系列,它也可以创建一个灵活的系统,使创建的对象更易于管理。

工厂方法将对象的创建推迟到子类,因为基类不知道要实例化哪个具体类。这里基类知道何时创建对象,但不知道创建哪个具体对象。

Command 用于封装请求,以便您像处理其他对象一样处理请求,例如将请求作为参数传递给另一个方法/对象、对请求进行排队、重用请求等。它不处理对象的创建。

除非我错得很离谱,否则它们是针对不同问题的不同模式。您可以查看 GoF 的设计模式Head First 设计模式 了解有关这些模式的详细信息。

I am not sure how you can solve some problems by both factory and command pattern. They solve completely different types of problem.

Abstract Factory handles the creation of objects in such a way that you can switch among the families of products easily and you can enforce the consistency of objects (i.e. do not mix different product families by accident). Even if there is only a single product family, it creates a flexible system where the created objects are easier to manage.

A Factory Method defers the creation of object to subclass as the base class do not have the knowledge of which concrete class to instantiate. Here the base class knows when to create the object, but don't know which concrete object to create.

And Command is used to encapsulate a request so that you handle a request just like other objects, for example pass a request as a parameter to another method/object, queue requests, reuse a request etc. It does not deal with the creation of objects.

Unless I am very very wrong, they are separate patters which attack separate problems. You can check Design Patterns by GoF and Head First Design Patters for the details about these patterns.

玻璃人 2024-11-02 02:49:00

考虑到在某些情况下两者都适用——但实际上没有想到——我认为两者都不是“更好”;最好的解决方案取决于问题、整体系统、团队等。如果其中一个“更好”,那么另一个就可以被丢弃!

Given that there are some situations where both apply -- and none really come to mind -- I'd assume neither is "better"; the best solution depends on the problem, the overall system, the team, etc. If one was "better" then the other could simply be discarded!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文