26
Abstract Factory Abstract Factory Doug Jeffries Doug Jeffries Tom Schneider Tom Schneider CS490 Design Patterns CS490 Design Patterns April 3, 2003 April 3, 2003

Abstract Factory Doug Jeffries Tom Schneider CS490 Design Patterns April 3, 2003

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

Abstract FactoryAbstract Factory

Doug JeffriesDoug Jeffries

Tom SchneiderTom Schneider

CS490 Design PatternsCS490 Design Patterns

April 3, 2003April 3, 2003

SummarySummary

• Abstract Factory definedAbstract Factory defined• Quick ExampleQuick Example• Generic UML for Abstract FactoryGeneric UML for Abstract Factory• Metsker ExampleMetsker Example• Challenge Problems!Challenge Problems!

What is Abstract Factory?What is Abstract Factory?

• GOF definitionGOF definition– Provide and interface for creating families of Provide and interface for creating families of

related or dependent objects without specifying related or dependent objects without specifying their concrete classes.their concrete classes.

• Sometimes called Sometimes called kitkit– Especially when it creates UI componentsEspecially when it creates UI components

• Avoid decision-making at every Avoid decision-making at every instantiationinstantiation

• Compare and contrast with Factory MethodCompare and contrast with Factory Method

ExampleExample

• UI supporting multiple platformsUI supporting multiple platforms• Interfaces simplify things, but we Interfaces simplify things, but we

need a way to instantiate objects need a way to instantiate objects without always knowing the class without always knowing the class namesnames

• An abstract factory defines methods An abstract factory defines methods like like createButton()createButton() and and createWindow()createWindow()

• Implementations create the right Implementations create the right kind of kind of ButtonButton, , WindowWindow, etc., etc.

UMLUML

In the WildIn the Wild

• Where have you seen this pattern?Where have you seen this pattern?

In the WildIn the Wild

• Configurable manufacturing facilityConfigurable manufacturing facility• java.awt.Toolkitjava.awt.Toolkit

• Play-doh fun factoryPlay-doh fun factory• Pasta makerPasta maker

Metsker ExampleMetsker Example

• Extending the CreditCheck Extending the CreditCheck example of Chapter 16 (Factory example of Chapter 16 (Factory Method)Method)

• Add ability to check billing and Add ability to check billing and shipping addressesshipping addresses– Add a class for eachAdd a class for each

• Updated diagram on next slideUpdated diagram on next slide

Metsker ExampleMetsker Example

Metsker ExampleMetsker Example

• We now want to do business in CanadaWe now want to do business in Canada– Different credit agency and data sourcesDifferent credit agency and data sources– Need another Factory to handle CanadaNeed another Factory to handle Canada

• So we have a Factory for each countrySo we have a Factory for each country• Abstract Factory organizes the Abstract Factory organizes the

factoriesfactories

Metsker ExampleMetsker Example

Challenge 17.1Challenge 17.1

• Complete the diagram in Figure Complete the diagram in Figure 17.3, which shows the classes in 17.3, which shows the classes in com.oozinoz.check.canadacom.oozinoz.check.canada and their and their relation to classes and interfaces relation to classes and interfaces in in com.oozinoz.checkcom.oozinoz.check..

• Diagram on next slideDiagram on next slide

Challenge 17.1Challenge 17.1

Solution 17.1Solution 17.1

Challenge 17.2Challenge 17.2

• Complete the code for Complete the code for CheckFactoryCanada.java:CheckFactoryCanada.java:

package com.oozinoz.check.canada;package com.oozinoz.check.canada;

import com.oozinoz.check.*;import com.oozinoz.check.*;

public class CheckFactoryCanada extends CheckFactorypublic class CheckFactoryCanada extends CheckFactory

{{

// ??// ??

}}

Solution 17.2Solution 17.2

package com.oozinoz.check.canada;package com.oozinoz.check.canada;import com.oozinoz.check.*;import com.oozinoz.check.*;public class CheckFactoryCanada extends CheckFactorypublic 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(); elseelse return new CreditCheckOffline();return new CreditCheckOffline(); }} public ShippingCheck createShippingCheck()public ShippingCheck createShippingCheck() {{ return new ShippingCheckCanada();return new ShippingCheckCanada(); }}}}

Challenge 17.3Challenge 17.3

• Your system needs only one Your system needs only one factory object for Canada checks factory object for Canada checks and one for United States checks.and one for United States checks.

• Write CheckFactory.java, including Write CheckFactory.java, including static variables that make it easy static variables that make it easy for an application developer to for an application developer to access these factories:access these factories:

Challenge 17.3Challenge 17.3

package com.oozinoz.check;package com.oozinoz.check;

import ??import ??

import ??import ??

public abstract class CheckFactory {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 ??

}}

Solution 17.3Solution 17.3

package com.oozinoz.check;package com.oozinoz.check;import com.oozinoz.check.us.CheckFactoryUS;import com.oozinoz.check.us.CheckFactoryUS;import com.oozinoz.check.canada.CheckFactoryCanada;import com.oozinoz.check.canada.CheckFactoryCanada;

public abstract class CheckFactory {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();}}

Challenge 17.4Challenge 17.4

• Write down an argument Write down an argument supporting the decision to place supporting the decision to place each factory and its related classes each factory and its related classes in a separate package.in a separate package.

• Alternatively, argue that another Alternatively, argue that another approach is superior.approach is superior.

Solution 17.4Solution 17.4

• Each factory in separate packageEach factory in separate package– Organize software and developmentOrganize software and development– Country-specific packages independentCountry-specific packages independent– Add support for new countries easilyAdd support for new countries easily

• Single packageSingle package– Separation nice in theory, but overwrought Separation nice in theory, but overwrought

in practicein practice– Single package easier to see at once in IDESingle package easier to see at once in IDE– Simpler for only a few countriesSimpler for only a few countries

Challenge 17.5Challenge 17.5

• Write down two reasons why Write down two reasons why Oozinoz might want to provide Oozinoz might want to provide different look-and-feels in user different look-and-feels in user environments.environments.

Solution 17.5Solution 17.5

• One set of components for new One set of components for new users and one for power usersusers and one for power users

• Different display types/sizes may Different display types/sizes may warrant different standard warrant different standard componentscomponents

• Distinguish major release versionsDistinguish major release versions• Clearly mark beta versionClearly mark beta version

Challenge 17.6Challenge 17.6

• Draw a diagram of an abstract Draw a diagram of an abstract class that will create standard class that will create standard Oozinoz components.Oozinoz components.

Solution 17.6Solution 17.6

Comments?Comments?