Download presentation
Presentation is loading. Please wait.
Published byClare Kennedy Modified over 9 years ago
1
CSC 313 – Advanced Programming Topics
2
What Is the Factory Method? Creation details hidden by AbstractCreator Does effective job of limiting concrete knowledge
3
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
4
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
5
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
6
Decorator:Factory::Strategy:?
7
Often Have Related Strategy Skinnable apps can change look-and-feel Similar allocations needed for all widgets
8
Often Have Related Strategy Skinnable apps can change look-and-feel Similar allocations needed for all widgets
9
Often Have Related Strategy Skinnable apps can change look-and-feel Similar allocations needed for all widgets
10
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
11
Abstract Factory Pattern
12
Client View of Pattern Clients use AbstractFactory & ignores details All of the factory methods declared by this class
13
Abstract Factory Design Common code & fields in AbstractFactory Can be either an abstract class or interface
14
Abstract Factory UML Product s are supertypes of useful objects But pattern will only return subtypes of these
15
Abstract Factory UML Client uses subclasses of AbstractFactory ConcreteFactory s perform actual allocations
16
Composition over Inheritance Inheritance makes mixing options easy
17
Nearly Real Example Abstract factory generates related types Building car from parts would be real-life example
18
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
19
Abstract Factory Vitals
20
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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.