2: Putting Plans into Action with the Strategy Pattern in Java
2: Putting Plans into Action with the Strategy Pattern ANSI/AIM Code 128 Drawer In Java Using Barcode generation for Java Control to generate, create Code 128 Code Set A image in Java applications. And you get: Bar Code Generator In Java Using Barcode creator for Java Control to generate, create bar code image in Java applications. Now I m driving Now I m driving
Barcode Scanner In Java Using Barcode scanner for Java Control to read, scan read, scan image in Java applications. Not bad, say the CEO and the board Who needs design patterns they ask, shooting you dirty looks But then they get a contract to produce helicopters Helicopters, they reason, are just another type of vehicle So they create helicopters using a Helicopter class, extending the Vehicle class like this: Make Code 128B In C# Using Barcode maker for Visual Studio .NET Control to generate, create Code 128 Code Set B image in .NET framework applications. public class Helicopter extends Vehicle { public Helicopter() { } } Generate Code 128C In VS .NET Using Barcode drawer for Visual Studio .NET Control to generate, create Code 128B image in VS .NET applications. But now there s a problem if you run the helicopter in addition to the cars like this: Create ANSI/AIM Code 128 In Visual Basic .NET Using Barcode creator for VS .NET Control to generate, create Code 128C image in Visual Studio .NET applications. public static void main(String[] args) { StreetRacer streetRacer = new StreetRacer(); FormulaOne formulaOne = new FormulaOne(); Helicopter helicopter = new Helicopter(); streetRacergo(); formulaOnego(); helicoptergo(); } Painting Data Matrix ECC200 In Java Using Barcode maker for Java Control to generate, create ECC200 image in Java applications. Then you get this when you run all three vehicles: one street racer, one Formula One race car, and one helicopter: EAN 128 Drawer In Java Using Barcode printer for Java Control to generate, create EAN128 image in Java applications. Now I m driving Now I m driving Now I m driving
Generating EAN 13 In Java Using Barcode maker for Java Control to generate, create EAN13 image in Java applications. That doesn t look right, says the CEO doubtfully Why should the helicopter be driving Shouldn t it be flying And the problem only gets worse when MegaGigaCo gets a contract to produce jets, which they also extend from the Vehicle class: Print Bar Code In Java Using Barcode generation for Java Control to generate, create barcode image in Java applications. public class Jet extends Vehicle { public Jet() { } } Generating Code 39 Full ASCII In Java Using Barcode drawer for Java Control to generate, create Code 39 Full ASCII image in Java applications. Part I: Getting to Know Patterns
Identcode Creation In Java Using Barcode creation for Java Control to generate, create Identcode image in Java applications. Running all four vehicles street racer, Formula One race car, helicopter, and jet, now gives you this: Code 39 Extended Encoder In VB.NET Using Barcode maker for Visual Studio .NET Control to generate, create Code39 image in Visual Studio .NET applications. Now Now Now Now I m I m I m I m driving driving driving driving
Draw ECC200 In Visual C#.NET Using Barcode generation for .NET framework Control to generate, create Data Matrix 2d barcode image in .NET applications. That s definitely not right, says the CEO Jets don t drive when they re in the air They fly and fast No problem, say the company programmers We can just override the go method in the Helicopter and Jet classes to give the right behavior They create something like this, which makes the Helicopter class fly, not drive: UPC - 13 Encoder In VB.NET Using Barcode drawer for VS .NET Control to generate, create EAN / UCC - 13 image in .NET applications. public class Helicopter extends Vehicle { public Helicopter() { } public void go() { Systemoutprintln( Now I m flying ); } } Bar Code Scanner In .NET Framework Using Barcode reader for VS .NET Control to read, scan read, scan image in .NET framework applications. Okay, says the CEO, but the board of directors has already voted to change that from Now I m flying to Now I m flying at 200 mph next week And more changes will be coming later, if I know them There s a problem here, you explain, and it s that the company programmers are spreading the way a single task is accomplished driving a car or flying a helicopter across several generations of classes That s not necessarily a big problem, but if how you want to handle that task is going to change fairly often, as here, having to edit all those classes becomes a maintenance issue You say: maybe inheritance isn t the answer in a case like this, where you have to spread out the handling of a changeable task over several generations of classes You re going to be maintaining a lot of customized code across generations of classes when that task changes And as the derived classes get to be long and involved, it s going to be tough to maintain them through all those changes You re going to have to update the go method forever The problem you re trying to solve is how to avoid spreading out the handling of a particular, changeable task over several generations of classes If you don t avoid that, you ll be editing a lot of files to update your code Drawing UPC-A Supplement 5 In Visual C#.NET Using Barcode creation for Visual Studio .NET Control to generate, create UCC - 12 image in Visual Studio .NET applications. 2: Putting Plans into Action with the Strategy Pattern
Painting European Article Number 13 In Visual Studio .NET Using Barcode drawer for .NET Control to generate, create UPC - 13 image in VS .NET applications. Perhaps there s a better way of handling the task of making vehicles move than using inheritance in this case Hey, says a company programmer how about using interfaces instead of using inheritance You could set up an IFly interface and give that interface a method named go that the Helicopter class has to implement as shown in the following: Encode Code 128 Code Set B In C# Using Barcode generator for .NET Control to generate, create Code-128 image in Visual Studio .NET applications. public class Helicopter implements IFly { public Helicopter() { } public void go() { Systemoutprintln( Now I m flying ); } } Decode ECC200 In .NET Using Barcode decoder for .NET Control to read, scan read, scan image in .NET applications. No good, you say You haven t solved the problem at all each class and subclass still has to implement its own version of the go method, which is just as bad as when you used inheritance And because interfaces don t include code, you still have to write custom code in each class, which means code reuse goes out the window
|
|
|
|
| ALL RIGHTS RESERVED. Business Refinery (c) 2006 - 2010. | Terms of Use | Privacy Policy |