开发者

Design Pattern(S) Command or Factory Which is Better to implement?

开发者 https://www.devze.com 2023-02-19 17:00 出处:网络
We can solve some Design Problems By implementing Factor开发者_开发知识库y as well as Command Pattern also.

We can solve some Design Problems By implementing Factor开发者_开发知识库y as well as Command Pattern also. so, according to performance which is the better one?


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.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号