I take the class of design patterns at school and read some chapters of design patterns of the head of the first. What I would like to know depends on the extent to which design patterns prevent rewriting of existing code.
Take, for example, the Duck and FlyBehavior . In my public static void main(String[] args) following code:
Duck mallard = new MallardDuck(new FlyWithWings());
Am I saying correctly that you need to change your main() method if you want to add a new strategy? So you are modifying existing code, right? I specifically refer to the part that mentions a particular strategy class: new FlyWithWings().
If you have implemented the factory method template in your code, you can FlyWithWings specific class ( FlyWithWings ) at all:
public FlyBehavior returnBehavior(FlyBehaviorFactory factory, String behaviorType) { return factory.getFlyBehavior(behaviorType); }
And thus enter the following line of code:
Duck mallard = new MallardDuck(returnBehavior(flyFactory, "wings"));
Thus, a certain part of your program should not know what to use FlyBehaviorFactory. However, your main() method will still have to specify certain parameters in the returnBehavior method to find out which factory will create which strategy. So, can I say that you still have to change main() if I add a new FlyBehavior class and want to add it as a parameter to returnBehavior() ?
Is it possible to improve this situation further?
source share