Download presentation
Presentation is loading. Please wait.
1
Abstract Factory Doug Jeffries Tom Schneider CS490 Design Patterns April 3, 2003
2
Summary Abstract Factory definedAbstract Factory defined Quick ExampleQuick Example Generic UML for Abstract FactoryGeneric UML for Abstract Factory Metsker ExampleMetsker Example Challenge Problems!Challenge Problems!
3
What is Abstract Factory? GOF definitionGOF definition –Provide and interface for creating families of related or dependent objects without specifying their concrete classes. Sometimes called kitSometimes called kit –Especially when it creates UI components Avoid decision-making at every instantiationAvoid decision-making at every instantiation Compare and contrast with Factory MethodCompare and contrast with Factory Method
4
Example UI supporting multiple platformsUI supporting multiple platforms Interfaces simplify things, but we need a way to instantiate objects without always knowing the class namesInterfaces simplify things, but we need a way to instantiate objects without always knowing the class names An abstract factory defines methods like createButton() and createWindow()An abstract factory defines methods like createButton() and createWindow() Implementations create the right kind of Button, Window, etc.Implementations create the right kind of Button, Window, etc.
5
UML
6
In the Wild Where have you seen this pattern?Where have you seen this pattern?
7
In the Wild Configurable manufacturing facilityConfigurable manufacturing facility java.awt.Toolkitjava.awt.Toolkit Play-doh fun factoryPlay-doh fun factory Pasta makerPasta maker
8
Metsker Example Extending the CreditCheck example of Chapter 16 (Factory Method)Extending the CreditCheck example of Chapter 16 (Factory Method) Add ability to check billing and shipping addressesAdd ability to check billing and shipping addresses –Add a class for each Updated diagram on next slideUpdated diagram on next slide
9
Metsker Example
10
We now want to do business in CanadaWe now want to do business in Canada –Different credit agency and data sources –Need another Factory to handle Canada So we have a Factory for each countrySo we have a Factory for each country Abstract Factory organizes the factoriesAbstract Factory organizes the factories
11
Metsker Example
12
Challenge 17.1 Complete the diagram in Figure 17.3, which shows the classes in com.oozinoz.check.canada and their relation to classes and interfaces in com.oozinoz.check.Complete the diagram in Figure 17.3, which shows the classes in com.oozinoz.check.canada and their relation to classes and interfaces in com.oozinoz.check. Diagram on next slideDiagram on next slide
13
Challenge 17.1
14
Solution 17.1
15
Challenge 17.2 Complete the code for CheckFactoryCanada.java:Complete the code for CheckFactoryCanada.java: package com.oozinoz.check.canada; import com.oozinoz.check.*; public class CheckFactoryCanada extends CheckFactory { // ?? // ??}
16
Solution 17.2 package com.oozinoz.check.canada; import com.oozinoz.check.*; public class CheckFactoryCanada extends CheckFactory { public BillingCheck createBillingCheck() public BillingCheck createBillingCheck() { return new BillingCheckCanada(); return new BillingCheckCanada(); } public CreditCheck createCreditCheck() public CreditCheck createCreditCheck() { if (isAgencyUp()) if (isAgencyUp()) return new CreditCheckCanadaOnline(); return new CreditCheckCanadaOnline(); else else return new CreditCheckOffline(); return new CreditCheckOffline(); } public ShippingCheck createShippingCheck() public ShippingCheck createShippingCheck() { return new ShippingCheckCanada(); return new ShippingCheckCanada(); }}
17
Challenge 17.3 Your system needs only one factory object for Canada checks and one for United States checks.Your system needs only one factory object for Canada checks and one for United States checks. Write CheckFactory.java, including static variables that make it easy for an application developer to access these factories:Write CheckFactory.java, including static variables that make it easy for an application developer to access these factories:
18
Challenge 17.3 package com.oozinoz.check; import ?? public abstract class CheckFactory { public static final CheckFactory US = ?? public static final CheckFactory US = ?? public static final CheckFactory ?? public static final CheckFactory ?? public abstract ?? createBillingCheck ?? public abstract ?? createBillingCheck ?? public abstract ?? createCreditCheck ?? public abstract ?? createCreditCheck ?? public abstract ?? createShippingCheck ?? public abstract ?? createShippingCheck ??}
19
Solution 17.3 package com.oozinoz.check; import com.oozinoz.check.us.CheckFactoryUS; import com.oozinoz.check.canada.CheckFactoryCanada; public abstract class CheckFactory { public static final CheckFactory US = public static final CheckFactory US = new CheckFactoryUS(); new CheckFactoryUS(); public static final CheckFactory CANADA = public static final CheckFactory CANADA = new CheckFactoryCanada(); new CheckFactoryCanada(); public abstract BillingCheck createBillingCheck(); public abstract BillingCheck createBillingCheck(); public abstract CreditCheck createCreditCheck(); public abstract CreditCheck createCreditCheck(); public abstract ShippingCheck createShippingCheck(); public abstract ShippingCheck createShippingCheck();}
20
Challenge 17.4 Write down an argument supporting the decision to place each factory and its related classes in a separate package.Write down an argument supporting the decision to place each factory and its related classes in a separate package. Alternatively, argue that another approach is superior.Alternatively, argue that another approach is superior.
21
Solution 17.4 Each factory in separate packageEach factory in separate package –Organize software and development –Country-specific packages independent –Add support for new countries easily Single packageSingle package –Separation nice in theory, but overwrought in practice –Single package easier to see at once in IDE –Simpler for only a few countries
22
Challenge 17.5 Write down two reasons why Oozinoz might want to provide different look-and-feels in user environments.Write down two reasons why Oozinoz might want to provide different look-and-feels in user environments.
23
Solution 17.5 One set of components for new users and one for power usersOne set of components for new users and one for power users Different display types/sizes may warrant different standard componentsDifferent display types/sizes may warrant different standard components Distinguish major release versionsDistinguish major release versions Clearly mark beta versionClearly mark beta version
24
Challenge 17.6 Draw a diagram of an abstract class that will create standard Oozinoz components.Draw a diagram of an abstract class that will create standard Oozinoz components.
25
Solution 17.6
26
Comments?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.