CSC 313 – Advanced Programming Topics. What Is the Factory Method? Creation details hidden by...

20
LECTURE 16: ABSTRACT FACTORY PATTERN CSC 313 – Advanced Programming Topics

Transcript of CSC 313 – Advanced Programming Topics. What Is the Factory Method? Creation details hidden by...

Page 1: CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.

LECTURE 16:ABSTRACT FACTORY PATTERN

CSC 313 – Advanced Programming Topics

Page 2: CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.

What Is the Factory Method? Creation details hidden by AbstractCreator Does effective job of limiting concrete

knowledge

Page 3: CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.

Factory Method Intent

Use incomplete types as the declared type of Local variables, fields, parameters … Everywhere, since change might occur anywhere

Since it must be concrete, avoid new command Replace with factory pattern Design is then much easier to add new classes Limits changes required when modify existing

classes

Page 4: CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.

Factory Method Use/Avoid

Use a factory method pattern when you Create object, but can choose class

instantiated Hide subclasses of abstract type from

client Must do complex allocation of object

stack Multiple related allocations is also

important Factory patterns so far define 1 factory

method Create 1 type of object (though many

subtypes) Forced linking or chaining allocations

impossible

Page 5: CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.

Decorator:Factory::Strategy:? Factory Method great for decorator

pattern Inheritance-based design of classes in

this pattern Core concept will be wrapped by many

decorators Returns single instance of abstract

supertype But pattern not helpful for Strategy

Pattern Works for single strategy, since need

only 1 type Often allocate & use many strategies,

however Factory needed for each type of

strategy, however

Page 6: CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.

Decorator:Factory::Strategy:?

Page 7: CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.

Often Have Related Strategy Skinnable apps can change look-and-

feel

Similar allocations needed for all widgets

Page 8: CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.

Often Have Related Strategy Skinnable apps can change look-and-

feel

Similar allocations needed for all widgets

Page 9: CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.

Often Have Related Strategy Skinnable apps can change look-and-

feel

Similar allocations needed for all widgets

Page 10: CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.

Sets of Choices

Instantiate sets of components to look good Different set defined by each look-and-feel Instantiate only from set defined by

components To change skins, must switch set allocating

from More places need this than skinnable

apps Each game level generates class of

enemies Quacking & flying behaviors in SimUDuck Printing & transmitting behavior of

messages in IM Colors & fonts used in a presentation

Page 11: CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.

Abstract Factory Pattern

Abstract Factory defines set of methods Each method is factory for different type

of object Use this pattern to allocate family of

objects Every component within a look-and-feel Level’s collection of snipers, tanks, &

grues Quacking & flying behavior for the ducks

created Abstract Factory is composition of

factories Group related ideas so will be allocated

together

Page 12: CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.

Client View of Pattern

Clients use AbstractFactory & ignores details All of the factory methods declared by this

class

Page 13: CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.

Abstract Factory Design

Common code & fields in AbstractFactory Can be either an abstract class or interface

Page 14: CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.

Abstract Factory UML

Products are supertypes of useful objects But pattern will only return subtypes of

these

Page 15: CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.

Abstract Factory UML

Client uses subclasses of AbstractFactory ConcreteFactorys perform actual

allocations

Page 16: CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.

Composition over Inheritance Inheritance makes mixing options

easy

Page 17: CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.

Nearly Real Example

Abstract factory generates related types Building car from parts would be real-life

example

Page 18: CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.

Abstract Factory Vitals

Family of related classes from 1 instance Pattern is also a composition of factory

methods Related or interdependent classes

allocated together Unlike factory method, instantiates

multiple types Skinnable & visual systems clearest

example Scrollbars, tabs, menus, …: many shared

concepts Concrete implementation changes with

each look

Page 19: CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.

Abstract Factory Vitals

Family of related classes from 1 instance Pattern is also a composition of factory

methods Related or interdependent classes

allocated together Unlike factory method, instantiates

multiple types Skinnable & visual systems clearest

example Scrollbars, tabs, menus, …: many shared

concepts Concrete implementation changes with

each look Clearly identified if classes used must

be related

Page 20: CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.

For Next Lecture

Lab #4 available on Angel Lab will be due before lab in two weeks (Fri.

3/9)

Read pages 169-178 in book How can we make sure only 1 end boss? What is plural of apocalypse? How to code

this? How are design patterns & Highlander alike?