![Page 1: Prof. Hertz (as told by xkcd.com). Computer Science 313 – Advanced Programming Topics](https://reader036.vdocuments.mx/reader036/viewer/2022081503/56649eb05503460f94bb59a0/html5/thumbnails/1.jpg)
Prof. Hertz (as told by xkcd.com)
![Page 2: Prof. Hertz (as told by xkcd.com). Computer Science 313 – Advanced Programming Topics](https://reader036.vdocuments.mx/reader036/viewer/2022081503/56649eb05503460f94bb59a0/html5/thumbnails/2.jpg)
Computer Science 313 – Advanced Programming Topics
![Page 3: Prof. Hertz (as told by xkcd.com). Computer Science 313 – Advanced Programming Topics](https://reader036.vdocuments.mx/reader036/viewer/2022081503/56649eb05503460f94bb59a0/html5/thumbnails/3.jpg)
What Is the Factory Method? AbstractCreator “makes” AbstractProduct When concreteness cannot be avoided, hide
it
![Page 4: Prof. Hertz (as told by xkcd.com). Computer Science 313 – Advanced Programming Topics](https://reader036.vdocuments.mx/reader036/viewer/2022081503/56649eb05503460f94bb59a0/html5/thumbnails/4.jpg)
Factory Method Intent
Use interfaces & abstract classes for Local variables, fields, parameters, static
fields, … Anywhere type is listed
Within your code, avoid new command Replace with factory pattern Simplifies adding new classes to design Limits changes required when modify existing
classes
![Page 5: Prof. Hertz (as told by xkcd.com). Computer Science 313 – Advanced Programming Topics](https://reader036.vdocuments.mx/reader036/viewer/2022081503/56649eb05503460f94bb59a0/html5/thumbnails/5.jpg)
Factory Method Use/Avoid
Pattern rocks when Choosing one subclass from many
options Hiding subclass actually instantiated Choosing within family of related classes
Factory method sucks for multiple choices Defines exactly one factory method Only creates 1 type of object Horrible if there are multiple related
choices
![Page 6: Prof. Hertz (as told by xkcd.com). Computer Science 313 – Advanced Programming Topics](https://reader036.vdocuments.mx/reader036/viewer/2022081503/56649eb05503460f94bb59a0/html5/thumbnails/6.jpg)
Decorator:Factory::Strategy:? Factory method great for decorator
pattern Decorator pattern uses inheritance Wrapped by unknown number of
decorators Returns single instance of specific type
But often not good for strategy Good when using single strategy Instances often use several strategies at
once Needs factory method instance for each Gives Matt multiple chances to screw up
![Page 7: Prof. Hertz (as told by xkcd.com). Computer Science 313 – Advanced Programming Topics](https://reader036.vdocuments.mx/reader036/viewer/2022081503/56649eb05503460f94bb59a0/html5/thumbnails/7.jpg)
Often Have Related Strategy
![Page 8: Prof. Hertz (as told by xkcd.com). Computer Science 313 – Advanced Programming Topics](https://reader036.vdocuments.mx/reader036/viewer/2022081503/56649eb05503460f94bb59a0/html5/thumbnails/8.jpg)
Sets of Choices
Must instantiate different sets of components Each look-and-feel has own set Which instantiated defined by this set Components shared between some
“skins” Situation exists in many applications
Enemies generated within each level in a game
Quacking & flying behaviors in SimUDuck
Presentation’s background, fonts & colors
![Page 9: Prof. Hertz (as told by xkcd.com). Computer Science 313 – Advanced Programming Topics](https://reader036.vdocuments.mx/reader036/viewer/2022081503/56649eb05503460f94bb59a0/html5/thumbnails/9.jpg)
Abstract Factory Pattern
Abstract factory defines set of methods Each method is a factory for different
type Family of objects created by abstract
factory Every component within a look-and-feel Level’s collection of ogres, archers,
boats & grues Colors & symbols used at each bullet
level Abstract factory is composition of
factories Groups related ideas
![Page 10: Prof. Hertz (as told by xkcd.com). Computer Science 313 – Advanced Programming Topics](https://reader036.vdocuments.mx/reader036/viewer/2022081503/56649eb05503460f94bb59a0/html5/thumbnails/10.jpg)
Abstract Factory UML
Clients use AbstractFactory only Can be abstract class or interface
![Page 11: Prof. Hertz (as told by xkcd.com). Computer Science 313 – Advanced Programming Topics](https://reader036.vdocuments.mx/reader036/viewer/2022081503/56649eb05503460f94bb59a0/html5/thumbnails/11.jpg)
Abstract Factory UML
Products are superclasses of useful objects Types returned to the client
![Page 12: Prof. Hertz (as told by xkcd.com). Computer Science 313 – Advanced Programming Topics](https://reader036.vdocuments.mx/reader036/viewer/2022081503/56649eb05503460f94bb59a0/html5/thumbnails/12.jpg)
Abstract Factory UML
Instances subclasses of AbstractFactory Methods instantiate objects
![Page 13: Prof. Hertz (as told by xkcd.com). Computer Science 313 – Advanced Programming Topics](https://reader036.vdocuments.mx/reader036/viewer/2022081503/56649eb05503460f94bb59a0/html5/thumbnails/13.jpg)
Composition over Inheritance Inheritance makes mixing options
easy
![Page 14: Prof. Hertz (as told by xkcd.com). Computer Science 313 – Advanced Programming Topics](https://reader036.vdocuments.mx/reader036/viewer/2022081503/56649eb05503460f94bb59a0/html5/thumbnails/14.jpg)
Nearly Real Example
Abstract factory generates related types Could use for parts incorporated into a
car
![Page 15: Prof. Hertz (as told by xkcd.com). Computer Science 313 – Advanced Programming Topics](https://reader036.vdocuments.mx/reader036/viewer/2022081503/56649eb05503460f94bb59a0/html5/thumbnails/15.jpg)
Abstract Factory Vitals
Instantiates entire family of related classes Can be composed from factory method
patterns Useful when classes are related or
interdependent Unlike factory method, instantiates
multiple types Often used in skinnable & visual
systems Have many concepts: scrollbars, tabs,
menus Each concept is implemented in different
ways Which classed used are directly related
![Page 16: Prof. Hertz (as told by xkcd.com). Computer Science 313 – Advanced Programming Topics](https://reader036.vdocuments.mx/reader036/viewer/2022081503/56649eb05503460f94bb59a0/html5/thumbnails/16.jpg)
For Next Lecture
Lab #4 available on web/Angel Due before next lab (Tues. 2/26)
Midterm #1 in class on Monday Will be open-note, open-book, & open-
reports Reports handed out in class Friday
Readings on web discuss when to optimize Can we predict when it will be
noticeable?
Who is this ?