csc 313 – advanced programming topics. what is the factory method? creation details hidden by...
TRANSCRIPT
LECTURE 16:ABSTRACT FACTORY PATTERN
CSC 313 – Advanced Programming Topics
What Is the Factory Method? Creation details hidden by AbstractCreator Does effective job of limiting concrete
knowledge
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
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
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
Decorator:Factory::Strategy:?
Often Have Related Strategy Skinnable apps can change look-and-
feel
Similar allocations needed for all widgets
Often Have Related Strategy Skinnable apps can change look-and-
feel
Similar allocations needed for all widgets
Often Have Related Strategy Skinnable apps can change look-and-
feel
Similar allocations needed for all widgets
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
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
Client View of Pattern
Clients use AbstractFactory & ignores details All of the factory methods declared by this
class
Abstract Factory Design
Common code & fields in AbstractFactory Can be either an abstract class or interface
Abstract Factory UML
Products are supertypes of useful objects But pattern will only return subtypes of
these
Abstract Factory UML
Client uses subclasses of AbstractFactory ConcreteFactorys perform actual
allocations
Composition over Inheritance Inheritance makes mixing options
easy
Nearly Real Example
Abstract factory generates related types Building car from parts would be real-life
example
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
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
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?