• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Difference of Strategy Pattern with Factory Method?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am studying patterns, and I can't distinguish that much their difference. Below is a strategy code. Can someone write the factory method equivalent of it?


 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Strategy and Factory are two different and distinct patterns and they have different goals. Strategy is a behavioral pattern while Factory is a creational pattern. There is no equivalence between the two, as far as I know. Where/how did you get the idea that there is some kind of equivalence between them?
 
jamby vedar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Welcome to the Ranch!

Strategy and Factory are two different and distinct patterns and they have different goals. Strategy is a behavioral pattern while Factory is a creational pattern. There is no equivalence between the two, as far as I know. Where/how did you get the idea that there is some kind of equivalence between them?



Thanks.Yeah they are different. But this code I created below is the factory method style right?

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK... But do you know why you would want to use Factory Method instead of just simply calling the constructor of the target class directly? It is important to understand the motivation for using a pattern otherwise you will be like someone walking around a woodworking shop trying to cut wood with a hammer. In other words, a pattern applied in an inappropriate context is an anti-pattern so be sure you understand the why behind the pattern.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both are different
Example of Strategy pattern













Factory pattern Example








 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You appear not to have posted much, so welcome to the Ranch even after 8 years. I added code tags to your post. Always use the: doesn't it look better
There is something not right about using "bark" and "quack" in a factory method. That sounds a bit to me like having to know implementation details. It will also fall down when you have this sort of class:-
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that this kind of implementation violates the Open-Closed Principle
 
jamby vedar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Dineshgopinath- I believe your factory example is simple factory(not mentioned by GOF).

@Lacar- yeah it's very important that you know what pattern/ideas to use at specific problem. With the code below, can I say that I combined strategy and factory patterns? OperationFactor is the factory. While AddCalc and MultiCalc are the strategy. Am I correct?

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jamby vedar wrote:With the code below, can I say that I combined strategy and factory patterns?


This has the same problem as the other example with the Pets: it violates the Open-Closed Principle. If you decide to add more operators later, you will have to modify this code to extend the if-then-else statement. Not a very good thing. That if statement needs to be replaced by something like a map lookup where the map is externally provided.

Factory Method is usually used in conjunction with a Template Method.
 
jamby vedar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

jamby vedar wrote:With the code below, can I say that I combined strategy and factory patterns?


This has the same problem as the other example with the Pets: it violates the Open-Closed Principle. If you decide to add more operators later, you will have to modify this code to extend the if-then-else statement. Not a very good thing. That if statement needs to be replaced by something like a map lookup where the map is externally provided.

Factory Method is usually used in conjunction with a Template Method.



I guess that if else problem will be solved by using Factory Method(not simple factory) with strategy. But one of the advantage of simple factory is that it eliminates the need for object=new on the client(program to the interface not implementation principle). Template method has their on drawback to as it add object=new to the client. But I guess every pattern has a drawback, and it's up to the user to use them very well.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jamby vedar wrote:I guess that if else problem will be solved by using Factory Method(not simple factory) with strategy. But one of the advantage of simple factory is that it eliminates the need for object=new on the client(program to the interface not implementation principle). Template method has their on drawback to as it add object=new to the client. But I guess every pattern has a drawback, and it's up to the user to use them very well.


I don't quite follow your logic there but it seems like you have some concepts mixed up, particularly as to how these relate to programming to interfaces. A Template will actually call a factory method precisely so it can avoid instantiating with the new keyword; it defers the decision of what the concrete type will be to the Factory method.
 
jamby vedar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

jamby vedar wrote:I guess that if else problem will be solved by using Factory Method(not simple factory) with strategy. But one of the advantage of simple factory is that it eliminates the need for object=new on the client(program to the interface not implementation principle). Template method has their on drawback to as it add object=new to the client. But I guess every pattern has a drawback, and it's up to the user to use them very well.


I don't quite follow your logic there but it seems like you have some concepts mixed up, particularly as to how these relate to programming to interfaces. A Template will actually call a factory method precisely so it can avoid instantiating with the new keyword; it defers the decision of what the concrete type will be to the Factory method.



I am not really that familiar yet with template pattern. But as I told I think one of the benefits of simple factory(i am not talking about factory method) is it avoids new on the client side.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jamby vedar wrote:I am not really that familiar yet with template pattern. But as I told I think one of the benefits of simple factory(i am not talking about factory method) is it avoids new on the client side.


Well, so does the Factory Method. Sure, the Factory method may use the new keyword but that is hidden from the rest of the class that it's in. This takes me back to the point I made earlier about needing to understand why you would want to apply a pattern. Just using a pattern so you can say you used it is not a good motivation but I see that happening all the time. This often results in more pain than benefit.

I have programmed for many years and I seldom consciously start with a design pattern in mind. The main thing I focus on is assignment of responsibilities and naming things so their intent is clear. With this foundation and constant refactoring, I can usually recognize that my code wants to follow a known pattern. That is, I subscribe more to the idea of refactoring towards a pattern rather starting out with a pattern in mind then structuring my code around that idea.

For example, when I see a method's parameter list grow to more than three items, I start thinking about the Builder pattern and refactoring to that. When I see a series of if statements for creating different variants of a class of objects, I start thinking about refactoring to a Factory or Factory Method. When I see that the code needs to use different algorithms, I start looking for opportunities to refactoring to a Strategy. The common thread in all this is changing where a responsibility is assigned.

So my approach can really be summed up as: Clean Code First, then Refactor to Patterns as the code tells you to.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jamby vedar wrote:avoids new on the client side.


There's another nuance to this, too, and it's a key point in understanding how and when to use the pattern: In what situations would there be a valid reason to avoid using new to get an object? Do you always want to avoid using new to instantiate objects?
reply
    Bookmark Topic Watch Topic
  • New Topic