





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






CS205-14 Design Patterns 1
no bad surprises also individual part of assign 2
example hand out today should help CS205-14 Design Patterns 2
(Gamma, Helms, Johnson,Vlissides)
mostly language independent 30-some patterns defined ongoing development by community
limited success thus fall
CS205-14 Design Patterns 3
isolate from knowledge of subclasses one goal of OO design/programming
system dependent classes when private state indicates correct class when possibilities determined at runtime
describes dates actual info/format depends on locality specific subclass needed depends on impl
Calendar could break encapsulation Calendar could return flag saying what class
CS205-14 Design Patterns 5
first breaks major reason for OO second limits subclasses to fixed set documented
pass in necessary info getCalendar() call in this example
CS205-14 Design Patterns 6
common pattern often static methods isolates knowledge/choice of subclasses
may return many different kinds of objects objects will be compatible with each other
CS205-14 Design Patterns 7
localize decision process
part of implementation (iterators) local version — platform/internationalization
size, density, spread of values can matter amount of change for video compression
CS205-14 Design Patterns 13
all variants instances of same abstract class abstract class provides necessary ops including creating from any other instance
CS205-14 Design Patterns 14
almost never start with state add as part of performance tuning
otherwise use factory method to choose subclass
CS205-14 Design Patterns 15
sometimes contained objects are also containers are the same type
describes tree like structures
myWindow (^) aComposite text1 text panel button1 button
CS205-14 Design Patterns 17
and probably elsewhere
container leaf
CS205-14 Design Patterns 18 note: not exactly existing java model
Component draw() Composite draw() addChild() remChild()
children parent 1 TextField^ Button client (^) uses Window contains CS205-14 Design Patterns 19
top of tree is composite has all tree behaviors
leaves and composites
example: moving graphics all children stay relative to parent
draw must draw every component composite draws its children leaves draw themselves
CS205-14 Design Patterns 25
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) { … } … }
CS205-14 Design Patterns 27
interpreter if expect new node types visitor if expect new actions
generally choose interpretter simpler design (=better) actions often better with node node has needed data do unto one’s self, not others
especially to add new behaviors
across many subclasses
across many superclasses all want additional behavior
CS205-14 Design Patterns 29
panels images lists text fields
don’t want 4 copies of scrollbar code
CS205-14 Design Patterns 30
wrap the class, rather than subclassing it
adds scrollbars to any JComponent rather than subclassing each one allows implementation to be shared
loses text-ness of text field now just a component still need a reference to text field itself
CS205-14 Design Patterns 31
everyone will present part first one on 10/15 (system design)
dissemination of info submission of assignments discussion board