Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 350 – Software Design Abstract Factory Pattern – Chapter 11 Provides an interface for creating families of related or dependent objects without specifying.

Similar presentations


Presentation on theme: "CS 350 – Software Design Abstract Factory Pattern – Chapter 11 Provides an interface for creating families of related or dependent objects without specifying."— Presentation transcript:

1 CS 350 – Software Design Abstract Factory Pattern – Chapter 11 Provides an interface for creating families of related or dependent objects without specifying their concrete classes. Imagine you had a computer system with a High and low resolution display driver as well as a high and low resolution print driver. For DriverIn a Low Capacity MachineIn a High Capacity Machine DisplayLRDDHRDD Low Resolution Display DriverHigh Resolution Display Driver PrintLRPDHRPD Low Resolution Print DriverHigh Resolution Print Driver While in this case the items are mutually exclusive, this is not always the case. A mid-range computer system might use a LRDD and a HRPD.

2 CS 350 – Software Design Abstract Factory Pattern – Chapter 11 A simple solution is to use switch statements as in the following code: //JAVA CODE FRAGMENT Class ApControl {...... public void doDraw() { public void doDraw() {...... switch (RESOLUTION) { switch (RESOLUTION) { case LOW: case LOW: //use LRDD case HIGH: case HIGH: //use HRDD } } public void doPrint() { public void doPrint() {...... switch (RESOLUTION) { switch (RESOLUTION) { case LOW: case LOW: //use LRPD case HIGH: case HIGH: //use HRPD } }} Why is this bad? Obviously I have said switch statements are not good, but that’s the superficial answer. The problem is that the code has to decide over and over again and in multiple places what driver it has to use. This leads to tight coupling and weak cohesion.

3 CS 350 – Software Design Abstract Factory Pattern – Chapter 11 Let’s apply inheritance to the problem by having two different ap controllers.

4 CS 350 – Software Design Abstract Factory Pattern – Chapter 11 What’s wrong with the previous approach? Combinatorial explosion. Instead as we said before, it is better to favor aggregation. Instead, let’s have two classes with inheritance.

5 CS 350 – Software Design Abstract Factory Pattern – Chapter 11 Ap controller now can use the two classes and not have to perform switches to determine which driver to use.

6 CS 350 – Software Design Abstract Factory Pattern – Chapter 11 The improved Ap controller code follows: //JAVA CODE FRAGMENT Class ApControl {...... public void doDraw() { public void doDraw() {...... myDisplayDriver.draw(); myDisplayDriver.draw(); } public void doPrint() { public void doPrint() {...... myPrintDrive.draw(); myPrintDrive.draw(); }}


Download ppt "CS 350 – Software Design Abstract Factory Pattern – Chapter 11 Provides an interface for creating families of related or dependent objects without specifying."

Similar presentations


Ads by Google