Docsity

Design Patterns: Understanding Object-Oriented Solutions, Quizzes of Software Engineering

Toggle sidebar
bg1
Design Patterns 1
CS205-14
administrivia
quiz back today
no bad surprises
also individual part of assign 2
more discussion on project at end of today
any questions on assign 3?
example hand out today should help
Design Patterns 2
CS205-14
initially developed by gang of four
(Gamma, Helms, Johnson, Vlissides)
capture experience of class design
mostly language independent
30-some patterns defined
ongoing development by community
hoped to develop common vocabulary
limited success thus fall
design patterns
Design Patterns 3
CS205-14
want to write code in terms of superclass
isolate from knowledge of subclasses
one goal of OO design/programming
but must know specific class for new
often painful to expose correct subclass
system dependent classes
when private state indicates correct class
when possibilities determined at runtime
factory - problem
Design Patterns 4
CS205-14
abstract class java.util.Calendar is example
describes dates
actual info/format depends on locality
specific subclass needed depends on impl
what class should be instantiated?
Calendar could break encapsulation
Calendar could return flag saying what class
factory - example
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Design Patterns: Understanding Object-Oriented Solutions and more Quizzes Software Engineering in PDF only on Docsity!

CS205-14 Design Patterns 1

administrivia

quiz back today

no bad surprises also individual part of assign 2

more discussion on project at end of today

any questions on assign 3?

example hand out today should help CS205-14 Design Patterns 2

initially developed by gang of four

(Gamma, Helms, Johnson,Vlissides)

capture experience of class design

mostly language independent 30-some patterns defined ongoing development by community

hoped to develop common vocabulary

limited success thus fall

design patterns

CS205-14 Design Patterns 3

want to write code in terms of superclass

isolate from knowledge of subclasses one goal of OO design/programming

but must know specific class for new

often painful to expose correct subclass

system dependent classes when private state indicates correct class when possibilities determined at runtime

factory - problem

abstract class java.util.Calendar is example

describes dates actual info/format depends on locality specific subclass needed depends on impl

what class should be instantiated?

Calendar could break encapsulation Calendar could return flag saying what class

factory - example

CS205-14 Design Patterns 5

both are unfortunate choices

first breaks major reason for OO second limits subclasses to fixed set documented

third choice is to create instance in method

pass in necessary info getCalendar() call in this example

factory - problem

CS205-14 Design Patterns 6

getCalendar called a factory method

common pattern often static methods isolates knowledge/choice of subclasses

factory classes contain many factory methods

may return many different kinds of objects objects will be compatible with each other

factory object is instance of factory class

factory patterns

CS205-14 Design Patterns 7

use factory method when multiple possibilities

localize decision process

use factory if subclass is hidden

part of implementation (iterators) local version — platform/internationalization

use factory class/object when multiple objects

must cooperate / be from same family

factory - when

want best representation of an instance

might depend on value of instance

size, density, spread of values can matter amount of change for video compression

or its recent/future usage

but class fixes representation once and for all

state - problem

CS205-14 Design Patterns 13

dynamically changing rep based on values

called state pattern

rep changes based on some context

all variants instances of same abstract class abstract class provides necessary ops including creating from any other instance

state pattern

CS205-14 Design Patterns 14

use state pattern only when substantially

different characteristics needed

almost never start with state add as part of performance tuning

usually only for mutable classes

otherwise use factory method to choose subclass

state -when

CS205-14 Design Patterns 15

many objects containers for smaller objects

sometimes contained objects are also containers are the same type

several patterns consider issues with these

composite

called composite pattern

describes tree like structures

composite

myWindow (^) aComposite text1 text panel button1 button

CS205-14 Design Patterns 17

you’ve all seen trees like these in 104

and probably elsewhere

problem is how to define classes

suggestion is define abstract class as tree node

at least 2 subclasses:

container leaf

composite pattern

CS205-14 Design Patterns 18 note: not exactly existing java model

composite example

Component draw() Composite draw() addChild() remChild()

children parent 1 TextField^ Button client (^) uses Window contains CS205-14 Design Patterns 19

many ways of expressing trees

composite pattern has advantages

top of tree is composite has all tree behaviors

everything has component behaviors

leaves and composites

composite pattern

sometimes want to deal with composite as top

of tree only

example: moving graphics all children stay relative to parent

sometimes want to see whole tree

draw must draw every component composite draws its children leaves draw themselves

many ways to look at visiting nodes

traversing composites

CS205-14 Design Patterns 25

visitor pattern part 1

public abstract class Component { abstract public void visit(CompAction ca); } public class Composite extends Component { public void visit(CompAction ca)) { foreach child { child.visit(ca) } } } public class Button extends Component { public void draw(CompAction ca) { ca.buttonAction(this); } } CS205-14 Design Patterns 26 public abstract class ComponentAction { abstract public void compAction(Component); abstract public void buttonAction(Button); … } public class DrawAction extends ComponentAction { public void compAction(Component) { error } public void buttonAction(Button) { … } … }

visitor part 2

CS205-14 Design Patterns 27

which one should you use?

interpreter if expect new node types visitor if expect new actions

if unsure (or both holds)

generally choose interpretter simpler design (=better) actions often better with node node has needed data do unto one’s self, not others

visitor vs. interpreter

subclasses common way to extend behavior

especially to add new behaviors

can usually re-use implementation of super

across many subclasses

what about re-using implementation of sub

across many superclasses all want additional behavior

decorator problem

CS205-14 Design Patterns 29

consider scrolling

many different kinds of displays in java

panels images lists text fields

want scrollable version of each

don’t want 4 copies of scrollbar code

decorator example

CS205-14 Design Patterns 30

need different mechanism to add behavior

wrap the class, rather than subclassing it

consider JScrollPane

adds scrollbars to any JComponent rather than subclassing each one allows implementation to be shared

not free

loses text-ness of text field now just a component still need a reference to text field itself

decorator solution

CS205-14 Design Patterns 31

project stuff

multiple deliverables for project

3 presentations

everyone will present part first one on 10/15 (system design)

also need written design docs then

final project due 12/

final project

course support software

must support

dissemination of info submission of assignments discussion board

plus product differentiator of your choice