design patterns - university of chicago€¦ · what are design patterns • solu%ons to specific...

Post on 23-Jul-2020

0 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Design patterns

What are design patterns

•  Solu%onstospecificproblemsinOOso3waredesign•  23pa:ernsin3categories

•  Crea%onal•  Structural

•  Composite•  …

•  Behaviorial•  Observer•  Interpreter•  …

Why are we studying them?

Observer

• Onetomanyrela%onship•  Themanyneedtoknowchangesin“one”immediately

•  Example•  Points&Shapes•  Map&loca%on-basedservices•  Agamecharacter&othergamecomponents•  …

Example

•  Ifaperson/subjectchangesitsstatus,howtoletallhis“subscriber”knows?

• Whattodowhenthereisonlyonesubscriber?• Whattodowhentherearemul%plesubscribersofdifferenttypes?• Whatifnewsubscribersareadded?•  Howtomakethecodeeasytomaintainandextend?

Class diagram

Example (location, location-related service) •  “loca%on”wouldbetheSubjectinpreviousslide•  “observer”wouldbethesuperclassofallthesub-classesthattrytoupdatethemselvesbasedontheloca%oninforma%on

The benefit of observer pattern

• Whennewtypesofobserversareadded,theprototypeandimplementa%onofthesubjectclassdoesn’tneedanychanges.

Other things to pay attention

• Don’tforgetthesubscribingandunsubscribingmethods• Pullno%fica%onvspushno%fica%on• WhatifIwanttodeleteasubject• Cananobserversubscribemul%plesubjects?

Composite pattern

•  Treehierarchy• Howdoyoubuildatree?

How to build a tree and traverse it?

structnode{structnode*le3;structnode*right;intval;intsum(){…}}

How to differentiate leaves and others?

structleaf{intval;intsum(){returnval;}}

How to accommodate different types of internal nodes? •  Examples

•  structnodeorstructleaf?•  Book•  Graphics

Class diagram

Apply composite pattern to tree

•  “Leaf”inpreviousslideistreeleaf•  “Composite”inpreviousslideisnon-leafnodesinatree

Interpreter

• Whatisaninterpreter•  Language,compiler

•  Example•  Booleanexpression

•  Abstractsyntaxtreea&&b||!caparserwillturnthisintoanabstractsyntaxtree,andthenaninterpreterwillevaluatethetree.Howtowriteaprogramtodothetree-basedevalua%on?

How to do addition & subtraction

• Howtorepresentanaddi%onexpression?•  Constant+Constant•  Constant+Constant+Constant

• Howtorepresentasubtrac%onexpression?

How to do addition & subtraction

• Howtorepresentanaddi%onexpression?•  Treeisagoodform

• Howtorepresentasubtrac%onexpression?•  Tree

•  Thechallenge:•  Anynodeintheabovetreecouldbeaconstant,anaddi%onexpression,orasubtrac%onexpression,etc.

Class diagram

Strategy Design Pattern Classescenteredonopera%ons,insteadofdata

Strategy

• Mul%plevariantsofonealgorithm• Differenttypesofobjectsonlydifferinginbehavior•  Thekeypartofaclassisitsmethod,NOTitsdata

•  Example:printer,sorter,comparator•  Themethodworksformul%pledatatypes

Example

• Printers•  Variousfontsize,indenta%on,capitaliza%on

Class diagram -- encapsulate algorithms into class

Alternative solutions

•  IfinC

•  Super-classonthedataside

•  TemplateinC++

Other examples •  Differentsor%ng•  Differentrendering•  …

Template

• Provideaskeletonforsimilaralgorithms•  Thekeyoftheclassiss%llopera%on,notdata

•  Example

Class diagram

Visitor

• Howtoaddaclassofopera%onsforavarietyofdataclasses?

•  Example•  Differentopera%onsforASTnodes•  Differentopera%onsforPerson(Female,Male)

Class diagram

Visitor •  Twoclasshierarchies:data&opera%ons

• Whatisitgoodat?•  Ifyouaddopera%ons(Visitorclasses),theinterfaceoftheElementclassesremainsunchanged

• Whatisitbadat?•  IfyouaddnewElementsub-class,significantchangesareneededfortheVisitorside

•  Double-dispatch•  Imaginetwodimensionsofafunc%oncall

•  Theexactalgorithm•  Thetypeofdatathisalgorithmworkson

•  Youwillgetchancetomakechoicealongbothdimensionsdynamically,usingvisitorpa:ern

Creational design patterns

Factory Method

•  Letsaclassdeferinstan%a%ontosubclasses•  NoneedtodecidewhichsubclassIwanttousesta%cally

•  Example•  Date(USstyle,Europestyle,Chinesestyle,…)• Window

Class diagram

Factorydesignpa:ernissomewhatsimilarwithStrategydesignpa:ern

When to use factory design pattern?

•  Thetypeofthesub-classisdeterminedatrun%me•  Thetypechangesveryinfrequentlyonceset

Abstract Factory

•  Forcrea%ngfamiliesofrelatedordependentobjectswithoutspecifyingtheirconcreteclasses

•  Examples•  Date,currency,data• Window,mouse,scrollbar,…

Abstractfactorydesignpa:ernissomewhatsimilarwithVisitordesignpa:ern

Class diagram

top related