prof. hertz (as told by xkcd.com). computer science 313 – advanced programming topics

Post on 13-Jan-2016

230 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Prof. Hertz (as told by xkcd.com)

Computer Science 313 – Advanced Programming Topics

What Is the Factory Method? AbstractCreator “makes” AbstractProduct When concreteness cannot be avoided, hide

it

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

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

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

Often Have Related Strategy

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

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

Abstract Factory UML

Clients use AbstractFactory only Can be abstract class or interface

Abstract Factory UML

Products are superclasses of useful objects Types returned to the client

Abstract Factory UML

Instances subclasses of AbstractFactory Methods instantiate objects

Composition over Inheritance Inheritance makes mixing options

easy

Nearly Real Example

Abstract factory generates related types Could use for parts incorporated into a

car

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

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 ?

top related