From experience, we all know that using standard OO design can help us create beautiful java projects. But they may not always be the cleanest or very expandable. A solution for this is using many different types of patterns. One that must be used in the beginning is the 'Strategy Pattern'. We all understand the standard OO design basics of abstraction, encapsulation, polymorphism, etc. But what strategy patterns do for us is make these basics more efficient. By definition a strategy pattern defines a family of algorithms, encapsulates each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
There is this guy named Joe, right? Well, he is a programmer for this big company, right? His job is to make a duck simulation for his boss to take on a business trip. He does this, and it is all good except there is a fundamental flaw in his program. True it lets the ducks fly and quack and all that, but with his methods he inadvertently makes decoy ducks able to fly as well. This is a big problem. He could have fixed this using an override method and just explicitly state that the decoy duck can't fly. This is inefficient though, plus, it does not allow for the chance that we get a decoy duck in the future that may be able to fly, or we strap it to a rocket. So to fix this we move all these duck behaviors into different interfaces and then call of the behaviors for each duck independently. This way if in the future the big boss man wants our decoy to all of a sudden look like it is diving for food, we can add that easily and not have to go through all our code and make the new explanation in all our pages, just the one behavior page.
No comments:
Post a Comment