We can solve some Design Problems By implementing Factory as well as Command Pattern also. so, according to performance which is the better one?
-
5Err. How are you solving the same kinds of problems using either a Factory or a Command pattern? They solve different kinds of problems: Factory solves creational issues, and Command solves behavioral issues. – Jeff Hubbard Mar 26 '11 at 05:27
-
2Jeff is correct...have a look at this book "Head first design patterns" where the author has very nicely explained these patterns – taher chhabrawala Mar 26 '11 at 05:41
2 Answers
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.

- 45,586
- 12
- 116
- 142
-
Could you not have a factory that creates commands to be digested by the command pattern? – Emobe Aug 14 '19 at 14:12
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!

- 80,601
- 10
- 150
- 186