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?
jamby vedar wrote:With the code below, can I say that I combined strategy and factory patterns?
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.
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.
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.
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.
jamby vedar wrote:avoids new on the client side.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |