Design Patterns Explained: From Analysis through Implementation

520 views

Published on

Ken Pugh takes you beyond thinking of design patterns as “solutions to a problem in a context.” Patterns are really about handling variations in your problem domain while keeping code from becoming complex and difficult to maintain as the system evolves. Ken begins by describing the classic use of patterns. He shows how design patterns implement good coding practices and then explains key design patterns including Strategy, Bridge, Adapter, Façade, and Abstract Factory. In small group exercises, learn how to use patterns to create robust architectures that can readily adapt as new requirements arise. Lessons from these patterns are used to illustrate how to do domain analysis based on abstracting out commonalities in a problem domain and identifying particular variations that must be implemented. Leave with a working understanding of what design patterns are and a better way to build models of your application domains.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
520
On SlideShare
0
From Embeds
0
Number of Embeds
38
Actions
Shares
0
Downloads
27
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Design Patterns Explained: From Analysis through Implementation

  1. 1. ! MI AM!Half(day!Tutorial! 11/11/2013!8:30!AM! ! ! ! ! ! "Design Patterns Explained: From Analysis through Implementation" ! ! ! Presented by: Ken Pugh Net Objectives ! ! ! ! ! ! Brought(to(you(by:( ! ! ! 340!Corporate!Way,!Suite!300,!Orange!Park,!FL!32073! 888(268(8770!H!904(278(0524!H!sqeinfo@sqe.com!H!www.sqe.com
  2. 2. ! ! !!Ken!Pugh! !!Net!Objectives! ! A fellow consultant with Net Objectives, Ken Pugh helps companies transform into lean-agile organizations through training and coaching. His special interests are in communication (particularly effectively communicating requirements), delivering business value, and using lean principles to deliver high quality quickly. Ken trains, mentors, and testifies on technology topics from object-oriented design to Linux/Unix. He has written several programming books, including the 2006 Jolt Award winner Prefactoring and his latest Lean-Agile Acceptance Test Driven Development: Better Software Through Collaboration. Ken has helped clients from London to Boston to Sydney to Beijing to Hyderabad. He enjoys snowboarding, windsurfing, biking, and hiking the Appalachian Trail. Reach Ken at ken.pugh@netobjectives.com!
  3. 3. Design'Pa*erns'Explained' Teams# info@netobjec+ves.com000 www.netobjec+ves.com 0 !  Great#designs#are#a#collabora8ve#effort# !  You#will#be#part#of#a#team#for#a#couple#exercises# !  Agile#teams#are#selfDorganizing## !  Form#yourself#into#three#to#five#person#teams# Pa*ern'Oriented' Design'' (Design'Pa*erns'Explained)' A#New#Perspec8ve#on#ObjectD Oriented#Design# POD#Half#Day#October#2012# # 1# #Copyright#©#2008#Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 2# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  4. 4. Design'Pa*erns'Explained' Introduc8ons# Ken#Pugh# !  Name# !  What#do#you#do?# !  Who#do#you#do#it#for?# !  Languages#used?## !  Years#programming?# !  What#paWerns#have#you#used?## !  Why#are#you#here?# !  What#do#you#want#out#of#the#course?# 3# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## Photo# Size:# ###Height:#2.25# Posi8on:## ##from#top#le_#corner# ##Horizontal#0.75## ##Ver8cal#1.# Picture##Style:#Simple#Black## ###Frame# # ken.pugh# @netobjec8ves.com# !  Fellow#Consultant# !  OOA&D,#Design#PaWerns,#Lean,#Scrum,#TestDDriven# Development# !  Over#2/5#century#of#so_ware#development# experience# !  Author#of#seven#books,#including:# –  0Prefactoring:0Extreme0Abstrac+on,0Extreme0 Separa+on,0Extreme0Readability00(2006#Jolt#Award)# –  0Interface0Oriented0Design0 –  Lean0Agile0Acceptance0TestIDriven0Development:0 BeKer0SoLware0Through0Collabora+on0 #16#October#2013# 4# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# No0code0goes0in0+ll0the0test0goes0on.0 A0journey0of0two0thousand0miles0begins0with0a0single0step.0 #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  5. 5. Design'Pa*erns'Explained' Lean for Executives Product Portfolio Management Business Product Owner "PaWernDOriented#Development"# Business ASSESSMENTS" CONSULTING" T RAINING " COACHING" Lean Enterprise Manage ment Team technical process ILAFYT Kanban / Scrum ATDD / TDD / Design Patterns 5# Pa*ern5oriented'development'is'matching'' 'the'forces'in'the'problem'' 'to'the'forces'in'pa*erns' Lean Management Project Management #Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 6# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  6. 6. Design'Pa*erns'Explained' Purpose#of#this#Course# The#Gang#of#Four# !  Present#a#powerful#way#of#analyzing#and#designing#so_ware# !  Incorporates:# !  Design#PaWerns## –  Gamma,#Helms,#Johnson,#Vlissides# –  Known#as#the#Gang#of#Four#(GOF)## –  Quali8es,#Principles,#and#Prac8ces## –  Design#PaWerns#(seen#in#a#different#way)## !  Based#on#“Timeless#Way#of#Building”# !  Mostly#about#design,#not#implementa8on# !  AWempts#to#change#our#paradigm#of#design# 7# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## –  Christopher#Alexander## #16#October#2013# 8# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  7. 7. Design'Pa*erns'Explained' Logis8cs# Learning#Styles# !  People#have#different#learning#styles# !  Please#phones#and#pagers#off#(or#to#vibrate)# !  Break#halfway## –  One#is#not#beWer#than#another# –  MisDmatches#cause#communica8on#problems# –  Please#ask#ques8ons#if#examples#are#not#detailed# enough# See:#hWp://www.netobjec8ves.com/resources/teamDissues/#for#more#informa8on# 9# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 10# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## ~2#16#October#2013# 16#October#2013#
  8. 8. Design'Pa*erns'Explained' Outline# Agenda:#PaWerns#Covered# !  Beginnings## !  Code#Quali8es## !  The#PaWerns# !  !  !  !  !  !  # Strategy# Template#Method# Bridge# Adapter# Façade# Abstract#Factory# # 11# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 12# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  9. 9. Design'Pa*erns'Explained' The#Pyramid## UML#Summary# Aggregation Different#ways#to#represent#classes. Composition A A Paradigm00 B PaKerns0 A aggregates B B is a member of A B A is composed of B A contains B type objects Wisdom0 Inheritance Principles0 Prac+ces0 Interface Implementation A A B B Dependency A Quali+es## B is derived from A A generalizes B From#Design#PaWerns#Explained:#A#New#Perspec8ve#on# #ObjectDOriented#Design.#Shalloway#and#TroW# 13# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 14# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# B implements A B A depends upon B A uses B Different#rela8onships#between#classes.##Don’t# worry#about#represen8ng#the#difference#between# aggrega8on#and#composi8on.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  10. 10. Design'Pa*erns'Explained' Sequence#Diagram#Summary## Beginnings ' 15# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# ### #16#October#2013# 16#October#2013#
  11. 11. Design'Pa*erns'Explained' Retrospec8on# Creeping#Changes## !  To#improve,##we#need#to#reflect# function() { Do_this; Do_that; Do_something_else; } –  What#worked# –  What#did#not#work# !  How#can#a#design#accommodate#change?# –  Not#an+cipate#change# 17# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 18# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  12. 12. Design'Pa*erns'Explained' Creeping#Changes#(2)## Creeping#Changes#(3)## function(a, b) { if (a) { if (b) { Do_another_thing:; //… } else { Do_one_more_thing; //.. } } else { Do_this; Do_that; Do_something_else; } } function(a) { if (a) { Do_another_thing: //… } else { Do_this; Do_that; Do_something_else; } } 19# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 20# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  13. 13. Design'Pa*erns'Explained' Spreadsheet#Conundrum# How#to#organize#?# 2000#rows# 10000#columns# 32#MB#Flash#Drive# # Access#by#individual#byte:## rowNumber#*##sizeofRow#+## ####columnNumber## Or## columnNumber#*#sizeofColumn##+# ####rowNumber# # How#will#you#store#it?# ##By#row?# ##By#column?# 21# Language / Word NAME ENGLISH GERMAN Name Namen CITY City Stadt POSTAL_CODE Zip #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 22# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## PC #16#October#2013# 16#October#2013#
  14. 14. Design'Pa*erns'Explained' Implementa8on#(1)# Implementa8on#(2)# String getLabelForNAME(Language l ) { if (l == ENGLISH) return “Name”; else if (l == GERMAN) return “Namen”; else throw Exception(“No translation for language” + l); } // Same for the other words 23# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## String getLabelForGERMAN(Word w) { if (w == NAME) return “Name”; else if (w == CITY) return “Stadt”; else if (w == POSTAL_CODE) return “PC”; else throw Exception(“No German translation for word ” + w); } // Same for the other languages #16#October#2013# 24# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  15. 15. Design'Pa*erns'Explained' Predictability# !  Cannot#predict#how#requirements#will#change# !  How##to#increase#predic8ve#abili8es?# –  High#quality#code#easier#to#change# Code'QualiAes' Paradigm00 PaKerns0 Wisdom0 Principles0 Prac+ces0 c# Quali+es## 25# ### #16#October#2013# 26# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  16. 16. Design'Pa*erns'Explained' Discussion#Exercise# Quali8es# !  Imagine#someone#brings#you# !  In#PaWernDOriented#Development# code#to#evaluate# !  What#do#you#look##for#to# decide#if#it#is#easy#to#change?## !  What#recommenda8ons# would#you#make?# 27# –  Focus#is#on:# Cohesion# Coupling# NonDRedundancy# Encapsula8on# And,#overall,#Testability0 #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 28# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  17. 17. Design'Pa*erns'Explained' Cohesion# Paradigm00 Strong#Cohesion## PaKerns0 Wisdom0 Principles0 Prac+ces0 c# Quali+es## !  Cohesion## –  “How#closely#opera8ons#in#a#rou8ne#[or#class]#are#related.”## !  No#"schizophrenic#classes”#doing#more#than#one#thing# !  No#"12Dpage#methods"# Paradigm00 PaKerns0 Wisdom0 Principles0 Prac+ces0 c# Quali+es## !  Class#Cohesion## –  Class#has#a#single#responsibility# –  Everything#"about"#fulfilling#that#responsibility# !  Method#Cohesion# –  Each#method#fulfills#only#one#aspect#of#class#responsibility# –  Prac8ce:#Programming#by#inten8on#(next#slide)# 29# *#Code%Complete,#Steve#McConnell,#1993,#p.#81.#This0concept0was0first0described0by0Larry0Constan+ne0in0 1975,0but0we0like0McConnell’s0defini+on0best.0 #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 30# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  18. 18. Design'Pa*erns'Explained' C#/Java/… C#/Java/… Programming#by#Inten8on# Alterna8ve#Methods#(Separa8on#of#Concerns)### "Sergeant" Method public void printReportOfEmployeesOfCustomer (String CustomerID) { Employee[] emps = getEmployees(CustomerID); printReport(CustomerID, emps); } private void printReport(String CustomerID, Employee [] emps) { sortEmployees(emps); printHeader(CustomerID); printFormattedEmployees(emps); printFooter(CustomerID); paginate(); } private void sortEmployees(Employee [] emps){ if(needsSorting(emps)) doSortEmployees(emps); } public void printReportOfEmployeesOfCustomer (String CustomerID) { Employee[] emps = getEmployees(CustomerID); if(needsSorting(emps)) sortEmployees(emps); printHeader(CustomerID); printFormattedEmployees(emps); "Private"* printFooter(CustomerID); Methods paginate(); } 31# *Note:#These#methods#may#not#be#literally#private,#as#we#may#need#to#make#some#of#them#public# or#protected#for#tes8ng.##But#we#treat0them0as0private#from#client#objects,#to#limit#coupling.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 32# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  19. 19. Design'Pa*erns'Explained' C#/Java/… C++ Programming#by#NonDInten8on# public void printReportOfEmployeesOfCustomer (String CustomerID) { // Get Employees Employee[] emps …Make series of calls to get employees // Sort Employees … Series of calls to sort // Print Header … Series of calls //… and so forth for lots of lines } 33# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## Programming#by#Inten8on# "Sergeant" Method void Report::printReportOfEmployeesOfCustomer (string CustomerID) { vector<Employee> emps = getEmployees(CustomerID); if(needsSorting(emps)) sortEmployees(emps); printHeader(CustomerID); printFormattedEmployees(emps); Private printFooter(CustomerID); Methods paginate(); } #16#October#2013# 34# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# *Note:#These#methods#may#not#be#literally#private,#as#we#may#need#to#make#some#of#them#public# or#protected#for#tes8ng.##But#we#treat0them0as0private#from#client#objects,#to#limit#coupling.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  20. 20. Design'Pa*erns'Explained' What#Programming#by#Inten8on#Gets#You# Paradigm00 Coupling## !  Method#Cohesion# !  Separa8on#of#concerns# PaKerns0 Wisdom0 Principles0 Prac+ces0 c# !  Coupling### Quali+es## –  “strength#of#connec8on#between#two#rou8nes#[or#classes]”# –  "Sergeant"#method#calls#other#methods# –  Private#methods##implement#details# !  Object#can#be## !  Clarity## –  Loosely#coupled## !  No#dependence#on#other#implementa8ons# !  (Also#called0inten+onal0coupling)0 –  Tightly#coupled## !  Objects#are#interdependent;# –  Clear#code#beWer#than#comments## !  Ease'in'finding/forming'certain'pa*erns' !  No#extra#work#is#required# –  Change#one#thing;#other#things#change#unpredictably# !  (Also#called#accidental0coupling)# *#Code%Complete,#Steve#McConnell,#1993,#p.#81.#This0concept0was0first0described0by0Larry0Constan+ne0in0 1975,0but0we0like0McConnell’s0defini+on0best.0 35# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 36# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  21. 21. Design'Pa*erns'Explained' Coupling#To#Data# Coupling#To#Other#Classes# Class# Global#Data# AWributes# Func8on# One# Func8on# Two# Func8on#Three# 37# HerClass# MyClass# YourClass# HisClass# Method#One# Hard#to#test# Need#to#instan8ate#many#classes# Too#coupled## Method#Two# Method#Three# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 38# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  22. 22. Design'Pa*erns'Explained' Paradigm00 No#Redundancy# Redundancy#D#Shalloway’s#Law# PaKerns0 Wisdom0 Principles0 Prac+ces0 !  "One#Rule#in#One#Place"# !  Example#of#viola8on:##Y2K# !  Redundancy#can#be#more#than#state## c# !  From#Al#Shalloway#(CEO#of#Net#Objec8ves)## Quali+es## !  “When#N#things#need#to#change#and#N>1,#Shalloway# will#find#at#most#ND1#of#these#things.”# –  E.g.#.#redundant#rela8onships# !  Redundancy#causes#maintenance#problems# !  Before#copying#and#pas8ng,#ask#why?# # !  Shalloway’s'Principle:#“Avoid#situa8ons#where# Shalloway’s#Law#applies”# # 39# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 40# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  23. 23. Design'Pa*erns'Explained' Paradigm00 Encapsula8on# Encapsulate#by#Policy,#Reveal#by#Need# PaKerns0 Wisdom0 Principles0 Prac+ces0 c# !  Hide## Quali+es## !  What#is#encapsulated#is# –  Data# –  Implementa8on# –  Decoupled# –  Freely#changeable# ! Never#know#what#might#want#change# !  Other#encapsula8on:# –  Type# !  Interfaces#hide#their#implemen8ng#classes# –  Design# !  Using#factory#decouples#clients#from#implemen8ng#classes# !  Easier#to#deDencapsulate#than#encapsulate#later# –  So#encapsulate## # !  Details#expanded#in#next#slides## 41# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 42# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  24. 24. Design'Pa*erns'Explained' Paradigm00 Abstrac8on# Dollar#Abstrac8on# PaKerns0 Wisdom0 Principles0 Prac+ces0 !  Dollar#as#double## Quali+es## –  double#interestRate;## –  double#balance;# –  double##interest#=#balance#*#interestRate#;# !  Abstrac8on#encompasses#encapsula8on# –  Think#about#what#you#want#done,#not#how#you#want#it#done## !  “When#You’re#Abstract,#Be#Abstract#All#the#Way”# !  Dollar#class### –  Dollar#mul8plyBy(Percentage#p)# –  Dollar#add(Dollar#other)# –  Dollar#mul8plyBy(Dollar#d);# –  Dollar#add(Percentage#p);## –  Never#use#a#primi8ve#(except#in#constructors)# !  Example#next#page# # Percentage#interestRate;# Dollar#balance;## Dollar#interest#=#balance.mul8plyBy(interestRate);## # 43# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 44# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  25. 25. Design'Pa*erns'Explained' Observa8on# Paradigm00 Redundancy# PaKerns0 Wisdom0 Principles0 Prac+ces0 c# Quali+es## !  Reversible#changes#are#o_en#not#equal#in#difficulty# !  Usually#easier#to#deDobjec8fy#for#performance#than#it#is# Class A Class B -Redundant Issue to#objec8fy#for#code#quality## !  Example:# -Redundant Issue Class A and Class B: –  Dollar#balance#"#double#balance;## –  double#balance##"#Dollar#balance;# Have redundant state or functionality Redundant issue inside each class How to resolve redundancy? 45# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 46# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  26. 26. Design'Pa*erns'Explained' It#Depends!*# Using#Inheritance# Inheritance Class A Class B -Redundant Issue Classes coupled to superclass -Redundant Issue Depends#on#rela8onship#between#classes# –  Conceptual?### !  Are#A#and#B#varia8ons#of#common#concept?# or# –  Rela8onship#of#consistent#use?### !  A#and#B#have#a#need#that#is#resolvable#in#a#common#way?# Conceptual SuperClass #Redundant Issue Class A Class B Concrete 47# *#©2007#All#Consultants#Everywhere# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 48# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  27. 27. Design'Pa*erns'Explained' Using#Delega8on# Comparing#These#Op8ons# Delega+on.## !  Inheritance:# –  May#not#be#possible### !  Classes#may#already#have#superclass## –  May#make#superclass#less#cohesive# Class A !  Delega8on:# Class C Redundant Issue –  Available#if#other#en88es#need#behavior# Class B !  Favor'delegaAon'over'inheritance''(GOF)'' Paradigm00 PaKerns0 Wisdom0 Principles0 Prac+ces0 Quali+es## 49# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 50# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  28. 28. Design'Pa*erns'Explained' Testability# Paradigm00 The#Role#of#Testability# PaKerns0 Wisdom0 Principles0 Prac+ces0 c# Paradigm00 PaKerns0 Quali+es## !  Difficult#to#unitDtest#code#is#o_en:# –  Tightly#Coupled# ! “I#cannot#test#this#without#instan8a8ng#half#the#system"# –  Weakly#Cohesive# ! #"This#class#does#so#much,#the#test#will#be#enormous#and# complex!"# !  Unit#tes8ng#is#good# !  If##you#don’t#do#unit#test## Wisdom0 Principles0 Prac+ces0 c# Quali+es## –  Always#ask#"if#I#were#to#test#this,#how#would#I#do#it?"# –  If#design#is#hard#to#test## ! Ask#"why#isn't#this#more#testable?"# !  It's#a#significant#trim#tab#(see#next#slide)# –  Redundant# ! #"I'll#have#to#test#this#in#mul8ple#places#to#ensure#it#works# everywhere"# 51# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 52# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  29. 29. Design'Pa*erns'Explained' Trim#Tabs# Standards#for#High#Quality#Code#(Review)# High#quality#code#(in#order#of#importance):# !  Testable'' !  Contains#no#redundancy'' !  Inten8onal#coupling,#strong#cohesion,#encapsulaAon' !  Clarity' "If you try to change the course of a supertanker by pushing at the bow you will not see any change. If you push on the rudder your chances of changing the course of the supertanker are guaranteed, but the amount of energy needed is still prohibitive. To be the most effective we need to push on the trim tabs, those small fins on the end of the rudder. –  Inten8onDrevealing#names# –  Adhere#to#a#coding#standard# By moving them, the rudder in turn moves and the tanker makes its turn.” R. Buckminster Fuller "Critical Path" 53# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## ~2#16#October#2013# 54# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  30. 30. Design'Pa*erns'Explained' Exercise## !  Exis8ng#Chip#object# A'Design'Exercise' ' –  Gets#status#of#hardware# –  Encrypts#status## –  Sends#status#to#monitoring#applica8on# !  Now#need:# Chip -ChipID:String +getAndSendStatus() -getStatus() -encrypt() -send() –  Different#encryp8on#algorithms## !  For#different#customers# !  Or#perhaps#different#8me## !  How#can#we#can#handle#this# varia8on?# Please#don't#turn#the#page#un8l# instructed#to#do#so…# Note:0I0am0not0asking0for0the0best0way0to0handle0this,0just0for0the0possible0ways 0 55# ### #16#October#2013# 56# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  31. 31. Design'Pa*erns'Explained' Blank#Slide# Blank#Slide# !  One#more# !  Another#inten8onally# uninten8onally#blank# slide# 57# blank#slide# !  Don’t#look#ahead# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 58# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  32. 32. Design'Pa*erns'Explained' Blank#Slide# AlternaAves' Approaches'To' Design'Exercise' !  S8ll#another#inten8onally# blank#slide# 59# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 60# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# ### #16#October#2013# 16#October#2013#
  33. 33. Design'Pa*erns'Explained' Different#Strategies#to#Follow# Ways#to#Handle#New#Func8on:#Switches# !  Best/Worst#Case## !  Chip#has#switch#based#on#value#given#by#Client# –  Everyone#can#upgrade#to#the#new#encryp8on# !  Copy#and#paste## – Pro:## ! Simple,#obvious#implementa8on# – Con:## ! If#new#encryp8on#algorithm# –  Start#with#fresh#code,#just#make#needed#changes# !  Use#a#switch## –  Add#switch#to#exis8ng#code#that#specifies#encryp8on# !  Inheritance## –  Specialize##class#containing#encryp8on#code# –  Override##encryp8on#method#to#use#new#encryp8on# – Must#maintain#Chip# ! Cohesion#in#Chip#is#weakened# ! May#get#“switch#creep”# !  Delega8on# –  Encryp8on#code#in#own#class## –  Different#versions#of#encryp8on#class.### –  Clients#refer#to#appropriate#encryp8on#object# – Switches#are#not#a#good#longDterm#solu8on# !  Func8on#pointers#in#C++;##delegates#in#C## 61# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 62# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  34. 34. Design'Pa*erns'Explained' Pseudocode Switch#Creep# switch (nation) {! !case US:! ! !// us version! !case MEXICO:! ! !// Mexican version! !case CANADA:! ! !// Canadian version! !! 63# Ways#to#Handle#New#Func8on:#Inheritance# switch (nation) {! !case US:! ! !// us version! !case MEXICO:! ! !// Mexican version! !case CANADA:! ! !// Canadian version! }! ! #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# !  Subclass#Chip:# –  Chip#has#original#encryp8on#(64#bit)# –  Subclass#is#Chip128E# Chip + getAndSendStatus() !  Or#Make#Chip#an#abstract#class# # encrypt() –  Implemen8ng#subclasses:# ! Encrypt#0##(no#encryp8on)# Chip0E Chip64E Chip128E # encrypt() # encrypt() # encrypt() ! Encrypt#64# ! Encrypt#128# –  Most#func8onality#inherited# ! Override#encrypt()# 64# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  35. 35. Design'Pa*erns'Explained' Problems#With#Inheritance# Combinatorial#Explosion# !  Inheritance#works##for#one#varia8on# Chip -ChipID + getAndSendStatus() # encrypt() # send() –  Not#for#two# !  E.g.#what#if#sending#also#varies?### !  Subclassing#is#sta8c## !  How#change#Chip#from#Encrypt64#to#Encrypt128?# Cohesion getting worse and worse Chip64E Chip128E # encrypt() # send() –  Need#to#transfer#internal#state## Chip64ETCP # send() # encrypt() # send() Chip64EFTP # send() Chip128ETCP # send() Chip128EFTP # send() Redundancies 65# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 66# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  36. 36. Design'Pa*erns'Explained' Delega8on# Delega8on#Scales# Paradigm00 PaKerns0 Wisdom0 Principles0 Prac+ces0 Quali+es## Chip -myEncrypt Sender Encryptor +encrypt() + getAndSendStatus() # encrypt() PGP64 +encrypt() 67# TCPIP PGP128 +send() +encrypt() #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 68# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# Encryptor Chip +send() +encrypt() -mySend -myEncrypt FTP +send() + getAndSendStatus() # encrypt() # send() PGP64 +encrypt() #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## PGP128 +encrypt() #16#October#2013# 16#October#2013#
  37. 37. Design'Pa*erns'Explained' Refactoring:##The#Tradi8onal#View# Refactoring#(1)# Original'code' !  Refactoring#usually#is#for#legacy#code# –  Seen#as#cleaning#up#bad#code# –  True,#but#not#complete# !  Refactor#code#to#issue#that#has#become0a0varia+on0 –  Improvement#in#light#of#new#requirements# 69# “Adventure”# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## hWp://benfry.com/distellamap/# #16#October#2013# 70# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# public class Chip { private String chipID; public Chip(String aChipID) { this.chipID = aChipID; } public void getAndSendStatus() { String status = getStatus(chipID); String encryptedStatus = encrypt(status); send(encryptedStatus); } private String getStatus(String chipID) { // get status from the real chip } private String encrypt(String status) { // encryption algorithm here } private void send(String status) { // sending behavior here } } #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  38. 38. Design'Pa*erns'Explained' Refactoring#(2)# Enhancement### Adding'new'encrypAon'is'enhancement,'not'refactoring' Delega8on# “Extract#Class”# and# “Extract#Interface”# // All elided code is unchanged from previous slide public class Chip { private String chipID; private Encrypt myEncrypt; public Chip(String aChipID) { this.chipID = aChipID; this.myEncrypt = new PGP64(); } private String encrypt(String status) { return myEncrypt.encrypt(status); } } 71# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 72# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  39. 39. Design'Pa*erns'Explained' Factory### Strategy#Injec8on##(1)# •  Constructor injection Encryptor getEncryptor(CustomerType customer) { switch (customer) { case C1: return new PGP64(); case C2: return new PGP128(); default: return new PGP0(); } } class Chip { private Encryptor enc; public Chip(Encryptor encrypt) { this.enc = encrypt; } private void encrypt() { this.enc.encrypt(); } public void getAndSendStatus() { encrypt() // and other calls } //In Chip public Chip(String aChipID, Customer customer) { this.chipID = aChipID; 73# this.myEncrypt = getEncryptor(customer); } #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 74# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  40. 40. Design'Pa*erns'Explained' Strategy#Injec8on##(2)# Strategy#Injec8on##(3)# •  Parameter injection •  Setter injection class Chip { public Chip() { } public void getAndSendStatus(Encryptor enc) { enc.encrypt(); // and other calls } } 75# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## class Chip { private Encryptor enc; public Chip() { } public void setEncryptor(Encryptor encrypt) { enc = encrypt; } public void getAndSendStatus() { enc.encrypt(); // and other calls } } #16#October#2013# 76# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  41. 41. Design'Pa*erns'Explained' Injec8on#With#Mul8ple#Strategies### Advice'from'the' Gang'of'Four' // Multiple strategies, constructor injection // Chip has constructor Chip(Encryptor e, Sender s) Chip getChip(CustomerType customer) { switch(customer) { case C1: return new Chip(new PGP64(), new TCPIP()); case C2: return new Chip(new PGP128(), new TCPIP()); default: return new Chip(new PGP0(), new FTP()); } } 77# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 78# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# ### #16#October#2013# 16#October#2013#
  42. 42. Design'Pa*erns'Explained' Gang#of#Four#Gives#Us#Guidelines*# The#OpenDClosed#Principle# !  So_ware#en88es#should#be:# Paradigm00 –  Open#for#extension# –  Closed#for#modifica8on## PaKerns0 Wisdom0 Principles0 Prac+ces0 !  Open/Closed'Principle' !  Design'to'interfaces' !  Favor'delegaAon'over'class'inheritance' !  Encapsulate'concept'that'varies'' !  In#short:# Quali+es## Paradigm00 PaKerns0 Wisdom0 c# Principles0 Prac+ces0 Quali+es## –  Design#modules#so#that#they#never#change## –  Add#new#modules#to#handle#changed#requirements# –  Not#always#achievable## !  Bertrand#Meyer:## # –  So_ware#en88es#(classes,#modules,#func8ons,#etc.)#should#be#open#for# extension,#but#closed#for#modifica8on”# !  Ivar#Jacobson#:### –  “All#systems#change#during#their#life#cycles.##This#must#be#borne#in#mind# when#developing#systems#expected#to#last#longer#than#the#first#version”# *'Gamma,#Helms,#Johnson,#Vlissides:#The#authors#of#Design%Pa2erns:%Elements%of%Reusable%Object<Oriented%Design % 79# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## A#good#ar8cle#on#the#OpenDClosed#Principle:#www.objectmentor.com/publica8ons/ocp.pdf# #16#October#2013# 80# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  43. 43. Design'Pa*erns'Explained' Design#to#Interfaces# Favor#Delega8on#Over#Inheritance# Paradigm00 PaKerns0 Wisdom0 Principles0 Prac+ces0 Relationship Quali+es## !  Define#interface#encapsula8ng#varia8on# !  Delegate#to#concrete#class#implemen8ng#interface# Interface B Chip -myEncrypt Impl1 Impl2 Impl3 Relationship Impl4 Impl5 Delegation The "Strategy Pattern" PGP128 PGP64 +encrypt() +encrypt() 1.  Decoupling concepts 2.  Defer decisions until runtime Not#n→m#rela8onships#between#implementa8ons# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## Encryptor +encrypt() + getAndSendStatus() # encrypt() Want#1→1#or#1→many#rela8onships#between#abstrac8on# 81# PaKerns0 Wisdom0 Principles0 Prac+ces0 Quali+es## InterfaceA Paradigm00 3.  Small performance hit #16#October#2013# 82# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  44. 44. Design'Pa*erns'Explained' Treat#Conceptually#Similar#Things#the#Same#Way# Find#What#Varies#and#Encapsulate#It# Paradigm00 !  Deal#with#things#at#an#abstract#level# !  Encapsulate#that#which#varies# PaKerns0 !  GoF#meant#a#varying#anything:0 Wisdom0 Principles0 Prac+ces0 –  Varying#design# –  Varying#object#crea8on# –  Varying#rela8onships#(1D1,#1Dmany)# –  Varying#sequences#and#workflows# –  Etc…# Quali+es## Client AbstractionA Impl4 Impl1 Impl2 Impl3 Closed is abstraction Open are implementations 83# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 84# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  45. 45. Design'Pa*erns'Explained' This#Advice#Promotes#Quality# Decoupled# Design#PaWerns#Are#Examples0 Reusable# # !  Each#paWern:# (eliminates#Redundant# Implementa8on) Cohesive# 85# –  Example#of#a#design#following#general#advice# –  Discovered,#not#invented# –  Example#in0a0given0context# Encapsulated# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 86# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  46. 46. Design'Pa*erns'Explained' The#Strategy#PaWern# !  Issue:# The'Strategy' Pa*ern ' –  Need#different#behavior#at#different#8mes# Paradigm00 PaKerns0 Wisdom0 Principles0 Prac+ces0 Quali+es## !  GoF#Intent:## –  Define#a#family#of#algorithms,#encapsulate#each#one,#and# make#them#interchangeable## –  Algorithm#vary#independently#from#clients#using#it#*# # 87# ### #16#October#2013# 88# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# *Design%Pa2erns,%Elements%of%Reusable%Object<Oriented%So?ware,#Gamma,#Helm,#Johnson,#Vlissides# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  47. 47. Design'Pa*erns'Explained' The#Canonical#Strategy#PaWern# Handling#Varia8ons# Context Strategy + request(Strategy) Client + algorithm() !  Factory#creates#different#objects#having#common# interface##(polymorphism)## –  Factory#is#cohesive##D##only#deals#with#instan8a8on# Java/C# // in Client: Strategy myStrategy= Factory.getStrategy() . . . myContext.request( myStrategy) // in Context: request ( Strategy myStrategy) { myStrategy.algorithm() } StrategyA + algorithm() StrategyB !  Client#decoupled#from#actual#objects# + algorithm() C++ 89# // in Client: Strategy *myStrategy= Factory.getStrategy() . . . myContext.request( myStrategy) // in Context Context::request ( Strategy myStrategy) { myStrategy->algorithm() } #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 90# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  48. 48. Design'Pa*erns'Explained' C#/Java Strategy#PaWern#with#a#Factory# Code#Examples#for#this#Approach# class Client () { Encryptor myEncrypt; myEncrypt= Factory.getEncryptor(); // get proper derivation to use Chip myChip= new Chip(); myChip.getAndSendStatus(myEncrypt); *# } class Chip () { //... public void getAndSendStatus( Encryptor anEncrypt) { //... anEncrypt.doEncrypt(s); // ... } } // Encrypt is an interface or abstract class with a doEncrypt method. // Have different classes implement this, each with a doEncrypt method. 91# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 92# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## ~2#16#October#2013# 16#October#2013#
  49. 49. Design'Pa*erns'Explained' C++ Code#Examples#for#this#Approach# What#Happens#Now#with#Changing#Requirements# !  If#new#encryp8on#requirement:## public void main () { Encryptor * myEncrypt; myEncrypt= Factory::getEncryptor(); // get proper derivation to use Chip myChip; myChip->getAndSendStatus( myEncrypt); } –  1.)#Create#new#class#implemen8ng#encryp8on# –  2.)#Modify#Factory#to#return#new#class# –  3.)#Nothing#else!# // in Chip.h class Chip () { public void getAndSendStatus( Encryptor *); } // in Chip.cpp ... void Chip::getAndSendStatus( Encryptor *anEncrypt) { ... anEncrypt->doEncrypt(s); ... } } // Encrypt is an interface or abstract class with a doEncrypt method. // Have different classes implement this, each with a doEncrypt method. 93# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## ~2#16#October#2013# 94# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  50. 50. Design'Pa*erns'Explained' Creeping#Changes#(Review)# Switch#Version#(1)# enum WhatToDo {Do_original, Do_one_more_thing}; function(a, b) { if (a) { if (b) { Do_another_thing:; //… } else { Do_one_more_thing; //.. } } else { Do_this; Do_that; Do_something_else; } } 95# Do_another_thing, class DoSomething { static void process(WhatToDo wtd) { switch(wtd) { case Do_original: Do_this; Do_that; Do_something_else; break; case Do_another_thing: Do_another_thing; //.. break; case Do_one_more_thing: Do_one_more_thing; //… break; } } #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 96# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  51. 51. Design'Pa*erns'Explained' Switch#Version#(2)# Switch#Version#with#Func8ons### class DoSomething { static void process(WhatToDo wtd) { switch(wtd) { case Do_original: DoOriginal(); break; case Do_another_thing: DoAnotherThing(); break; case Do_one_more_thing: DoOneMoreThing(); break; } } // Separate what to do from doing it WhatToDo factory(a, b) { if (a){ if (b) return Do_another_thing;; else return Do_one_more_thing; } else return Do_original; } class Client { WhatToDo wtd = factory(a, b); DoSomething.process(wtd); } 97# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 98# static void doOriginal() { Do_this; Do_that; Do_something_else; } static void doAnotherThing() { Do_another_thing; //.. } static void DoOneMoreThing() { Do_one_more_thing; //… } Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  52. 52. Design'Pa*erns'Explained' Convert#To#Strategy# Convert#To#Strategy#(2)# !  Separate#what#to#construct#from#using#it## class Do_original implements DoSomething { void process() Do_this; Do_that; Do_something_else; } }; class Do_another_thing implements DoSomething { void process() { Do_another_thing; //… } }; class Do_one_more_thing implements DoSomething { void process() { Do_one_more_thing; //… } }; – Separate#policy#from#implementa8on# – Create#factory#to#return#implementa8on#of# interface# # interface DoSomething{ void process(); }; 99# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 100# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  53. 53. Design'Pa*erns'Explained' Convert#To#Strategy#(2)# Select#Strategy#with#a#Table# DoSomething factory(a, b) { if (a){ if (b) return new Do_another_thing(); else return new Do_one_more_thing(); } else return new Do_original(); A# Return## true# Do_another_thing# True# false# Do_one_more_thing# False# true# Do_original# False# } B# True# False# Do_original# class Client { public void some_method() { DoSomething ds = factory(a, b); ds.process(); } } 101# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 102# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  54. 54. Design'Pa*erns'Explained' The#Template#Method#PaWern# The'Template' Method'Pa*ern' !  Issue:# –  Abstract#common#theme#from#cases#with#different# implementa8ons# !  GoF#Intent:## –  Define#skeleton#of#an#algorithm#in#an#opera8on,#deferring# some#steps#to#subclasses.### –  Template#Method#lets#subclasses#redefine#certain#steps#of#an# algorithm#without#changing#the#algorithm’s#structure*# Paradigm00 PaKerns0 Wisdom0 Principles0 Prac+ces0 Quali+es## !  Analogy## –  Hollywood#–#“Don’t#call#us,#we’ll#call#you”## 103# ### ~2#16#October#2013# 104# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# *Design%Pa2erns,%Elements%of%Reusable%Object<Oriented%So?ware,#Gamma,#Helm,#Johnson,#Vlissides# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## ~2#16#October#2013# 16#October#2013#
  55. 55. Design'Pa*erns'Explained' Print#Report## Commonali8es#/#Variabili8es# Commonality# class Report { public void printReportOfEmployeesOfCustomer (String CustomerID) { Employee[] emps = getEmployees(CustomerID); if(needsSorting(emps)) sortEmployees(emps); printHeader(CustomerID); printFormattedEmployees(emps); printFooter(CustomerID); paginate(); } //.. Implementation methods here } 105# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## printHeader(CustomerID); printFooter(CustomerID); Variabili8es# printHeader(CustomerID); // Brief Header // Full Header printFooter(CustomerID); // Brief Footer // Full Footer #16#October#2013# 106# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## ~2#16#October#2013# 16#October#2013#
  56. 56. Design'Pa*erns'Explained' Template##PaWern# Template#Method#Code#Example# Report abstract class Report { public void printReportOfEmployeesOfCustomer (String customerID) { // … printHeader(customerID); printFooter(customerID); } abstract protected void printHeader(ID id); abstract protected void printFooter(ID id); } class BriefHeaderReport extends Report { protected void printHeader(ID id) { //. Code here } protected void printFooter(ID id) { //.. Code here } } +printReportOfEmployeesOfCustomer() #getEmployees(CustomerID); #sortEmployees(); #printHeader(); #printFormattedEmployees(); #printFooter(); #paginate(); BriefHeaderReport FullHeaderReport #printHeader(); #printFooter(); 107# #printHeader(); #printFooter(); #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 108# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  57. 57. Design'Pa*erns'Explained' "Canonical"#Template#Method# Strategy##PaWern# Report AbstractTemplate Client +TemplateMethod() #Operation1() #Operation2() +printReportOfEmployeesOfCustomer() TemplateMethod: … Operation1() … Operation2() … #getEmployees(CustomerID); #sortEmployees(); #printHeader(); #printFormattedEmployees(); Header #printFooter(); printHeader(); #paginate(); printFooter(); ConcreteClassA ConcreteClassB #Operation1() #Operation2() #Operation1() #Operation2() BriefHeader FullHeader printHeader(); printFooter(); 109# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## ~2#16#October#2013# 110# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #printHeader(); printFooter(); #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  58. 58. Design'Pa*erns'Explained' Exercise# Template#Example#in#MFC# !  There#could#be#a#separate#strategy#for#both#header#and# footer.### !  What#are#the#tradeoffs#between#the#Template#Method# and#the#two#versions#of#the#Strategy#paWern#(header/ footer#in#one,#header/footer#separate)## Applica8on## Windows#System## ac8vate# Window# # ac8vate()##{draw();}# draw();### close()# minimize()# //#fi_y#more#(with#default# implementa8on)## MyWindow# # draw()#{#//#mydrawing;}## 111# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 112# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  59. 59. Design'Pa*erns'Explained' A#PaWern#in#Carpentry# !  Two#carpenters#are#building#a#chest#of#drawers# !  One#says#to#the#other:#“should#we#build#the#joint#by# Design'Pa*erns' Paradigm00 PaKerns0 Wisdom0 Principles0 Prac+ces0 Quali+es## 113# ### #16#October#2013# first#cuÑng#down,#then#cuÑng#up#at#a#45#degree# angle...”# !  “then#back#down#and#back#up## # #the#other#way,#then#back#down”# !  “And#keep#this#up#un8l#done”# !  This#is#“doveDtail#joint”# # 114# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  60. 60. Design'Pa*erns'Explained' DoveDTail# A#Miter#Joint# Commonly#used#in#picture#frames# 115# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 116# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  61. 61. Design'Pa*erns'Explained' PaWerns#Work#At#All#Levels# Common#Reasons#to#Study#PaWerns# !  ReDuses#others’#experience# Dovetail' Forces' Design' Miter' Strong,#HighDCost,# Decora8ve,#Humidity# Tolerant# Weak,#Cheap,#Fast,#Good# for#Picture#Frames# !  Designs#improved#for#modifiability# Interlaced#joint# Single#AbuÑng#Cut# !  Use#common#terminology# –  some8mes#results#in#quick#solu8on# –  Embrace#change# –  Improve#and#elevate#communica8on# ImplementaAon' Cut#teeth,#.5"#long,#.4"# 45#degree#angle#cut# deep,#match#teeth#to#gaps# aWached#with#staple#or# other#spanning#connector# 117# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# !  PaWerns#are#great#examples#of#powerful#OO# 118# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  62. 62. Design'Pa*erns'Explained' What#Are#PaWerns?# !  PaWern:# –  Collec8on#of#proven#prac8ces#for#a#recurring#problem#in#a#given#context# !  Design#paWerns## AbstracAon ' –  Describe#rela8onships#between#en88es#(objects)#in#a#problem#domain# !  PaWerns# –  Handle#varia8ons#in#behavior# –  Use#interfaces# Paradigm00 !  PaWerns#have#forces## PaKerns0 –  Contextual#forces#–#to#determine#what#paWern#to#use# –  Implementa8on#forces#–#how#to#create#the#paWern## –  Consequen8al#forces#–#what#happens#when#paWern#is#used## Wisdom0 Principles0 Prac+ces0 Quali+es## !  PaWerns#emphasize#importance#of#abstrac8ons# –  …but#do#not#address#how#to#iden8fy#the#right#ones…# 119# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 120# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# ### #16#October#2013# 16#October#2013#
  63. 63. Design'Pa*erns'Explained' Consider#These# “It0Depends!”# !  As#specific#objects:## –  Different# !  As#a#concept#(abstrac8on):# –  All#Cleaning#Tools# !  Are#there#other#abstrac8ons#with#different# commonality?## !  Which#abstrac8ons#are#useful?#### Mop'''''''''''''''''''''''''Broom''''''''''''''Sponge' # The#same?# # Different?# 121# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 122# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  64. 64. Design'Pa*erns'Explained' Various#Concepts# Cleaning#Tool# cleanSomething()# Mop# Broom# Floor#Cleaning#Tool# cleanFloor()# Sponge# Mop# Broom# High#Abstrac8on## Water#Cleaning#Tool# removeWater()# Mop# Sponge# !  Higher#abstrac8ons#are#longer#life## –  (But#can#be#less#useful)# !  Example:# –  Abstrac8on### !  Automobile## ItemsForSale# price()# ItemsToStore# size()# –  Prepare#for#following## –  Follow#a#course### !  Implementa8ons:# Mop# Broom# Sponge# Mop# Broom# –  Turn#on#with#key##/###Push#a#buWon## –  Steer#with#wheel,#speed#with#pedals#/###Use#a#joys8ck### Sponge# All#Yellow#Items# 123# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 124# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  65. 65. Design'Pa*erns'Explained' What#Is#the#Right#Abstrac8on?## CommonalityDVariability#Analysis# Paradigm00 !  The#United#Kingdom#"could#be#seen#as#economy#by# PaKerns0 economists,#a#society#by#sociologists,#a#threaded#chunk# of#nature#by#conserva8onists,#a#tourist#aWrac8on#by# some#Americans,#a#military#threat#by#rulers#of#the# Soviet#Union,#and#the#green,#green#grass#of#home#to# the#more#roman8c#of#us#Britons".# Wisdom0 c# Principles0 Prac+ces0 Quali+es## !  Abstrac8on##focuses#on#commonality#(general)# !  DeDemphasize#variable#details##(specifics)#.# !  Acknowledgement:## !  Flood#and#Carson# –  Commonality#–#Variability#Analysis#in#James#Coplien’s#Mul8D paradigm#Design#for#C++# !  "Why#is#a#light#bulb#like#Doonesbury?"# –  Neither#one#can#whistle# 125# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 126# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# His#thesis#is#on#line#at:# hWp://www.netobjec8ves.com/files/CoplienThesis.pdf# His#thesis#is#on#line#at#hWp://www.netobjec8ves.com/ (link#is#caseDsensi8ve)# download/CoplienThesis.pdf# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  66. 66. Design'Pa*erns'Explained' Commonali8es#and#Variability# !  Commonali8es#can#define#basic#domain#concepts# The'Bridge' Pa*ern' (abstrac8ons)# –  Recognized#from#experience# –  Learned#through#analysis#(abstracted)# !  Variability#is#varia8on#of#commonality#(abstrac8on)## 127# *'MulB<Paradigm%Design%in%C++,#Jim#Coplien# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 128# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# ### #16#October#2013# 16#October#2013#
  67. 67. Design'Pa*erns'Explained' The#Bridge#PaWern# Rectangle#Drawing#Program# Paradigm00 PaKerns0 Wisdom0 Principles0 Prac+ces0 Client Quali+es## !  GoF#Intent:## Rectangle + draw() # drawLine(x1,y1,x2,y2) –  DeDcouple#an#abstrac8on#from#its#implementa8on#so#that#the# two#can#vary#independently#*## !  First#an#example## *Design%Pa2erns,%Elements%of%Reusable%Object<Oriented%So?ware,#Gamma,#Helm,#Johnson,#Vlissides# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 130# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# V2Rectangle # drawLine(x1,y1 x2,y2) DP1 + draw_a_line(x1,y1, x2,y2) + draw_an_arc(x, y, r, a1, a2) 129# V1Rectangle # drawLine(x1,y1 x2,y2) DP2 + drawline(x1,x2,y1,y2) + drawarc(r, x, y, a1, a2) #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  68. 68. Design'Pa*erns'Explained' C# Java Code#Example# abstract class Rectangle { private double_x1, _y1, _x2, _y2; public Rectangle(double x1, double y1, double x2, double y2){ .. } void public Draw () { DrawLine(_x1,_y1,_x2,_y1); DrawLine(_x2,_y1,_x2,_y2); DrawLine(_x2,_y2,_x1,_y2); DrawLine(_x2,_y1,_x1,_y1); } abstract void DrawLine( double x1, double y1, double x2, double y2); } class V1Rectangle : Rectangle { void override DrawLine( double x1, double y1, double x2, double y2) { DP1.draw_a_line( x1,y1,x2,y2); } } class V2Rectangle : Rectangle { void override DrawLine(double x1, double y1, double x2, double y2) { DP2.drawline( x1,x2,y1,y2); } } 131# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# Code#Example# abstract class Rectangle { private double_x1, _y1, _x2, _y2; public Rectangle(double x1, double y1, double x2, double y2){ .. } void public draw () { drawLine(_x1,_y1,_x2,_y1); drawLine(_x2,_y1,_x2,_y2); drawLine(_x2,_y2,_x1,_y2); drawLine(_x2,_y1,_x1,_y1); } abstract void drawLine( double x1, double y1, double x2, double y2); } class V1Rectangle extends Rectangle { drawLine( double x1, double y1, double x2, double y2) { DP1.draw_a_line( x1,y1,x2,y2); } } class V2Rectangle extends Rectangle { drawLine(double x1, double y1, double x2, double y2) { DP2.drawline( x1,x2,y1,y2); } } 132# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  69. 69. Design'Pa*erns'Explained' C++ Code#Example# Get#New#Requirements# !  Now#have#Circles# !  Handle#similarly## class V1Rectangle : public Rectangle { … } class V2Rectangle : public Rectangle { … } void Rectangle::draw () { drawLine(_x1,_y1,_x2,_y1); drawLine(_x2,_y1,_x2,_y2); drawLine(_x2,_y2,_x1,_y2); drawLine(_x1,_y2,_x1,_y1); } void V1Rectangle::drawLine (double x1, double y1, double x2, double y2) { DP1::draw_a_line( x1,y1,x2,y2); } void V2Rectangle::drawLine (double x1, double y1, double x2, double y2) { DP2::drawline( x1,x2,y1,y2); } 133# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## –  Make##V1Circle#and##V2Circle# !  Decouple#client#from#specific#shapes## –  Derive#specific#shapes#from#base#class# #16#October#2013# 134# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  70. 70. Design'Pa*erns'Explained' C# Handling#New#Shape#Class# interface Shape { public void draw(); } abstract class Rectangle : Shape { private double _x1,_y1,_x2 _y2; public override void draw() { drawLine(_x1, _y1, _x2, _y1); drawLine(_x2, _y1, _x2, _y1); drawLine(_x2, _y2, _x1, _y2); drawLine(_x1, _y2, _x1, _y1); } abstract void drawLine( double x1, double y1, double x2, double y2); } } class V1Rectangle : Rectangle { public override void drawLine( double x1, double y1, double x2, double y2){ DP1.draw_a_line(x1,y1, x2, y2); } } Shape Client +draw() Circle Rectangle +draw() V1Rectangle + draw() V2Rectangle # drawLine(x1, y1, x2, y2) # drawLine(x1, y1, x2, y2) DP1 + draw_a_line(x1, y1, x2, y2) + draw_an_arc(x, y, r, a1, a2) 135# V1Circle V2Circle #drawArc(x,y,r,a1,a2) # drawArc(x,y,r,a1,a2) DP2 + drawline(x1, x2, y1, y2) + drawarc( r,x, y, a1,a2) #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## Code#Example# #16#October#2013# 136# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# class V2Rectangle : Rectangle { public override void drawLine( double x1, double y1, double x2, double y2){ DP2.drawline(x1, x2, y1, y2); } } abstract class Circle : Shape { private double _x, _y, _r; public override void draw () { drawArc( _x, _y, _r, 0, 360); } abstract void drawArc( double x, double y, double r, double a1, double a2); } class V1Circle : Circle { protected override void drawArc(double x, double y, double r, double a1, double a2){ DP1.draw_an_arc(x,y,r,a1,a2); } } class V2Circle : Circle { protected override void drawArc(double x, double y, double r, double a1, double a2){ DP2.drawarc(_r,_x,_y,a1,a2); } } #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  71. 71. Design'Pa*erns'Explained' Java class V2Rectangle extends Rectangle { public override void drawLine( double x1, double y1, double x2, double y2){ DP2.drawline(x1, x2, y1, y2); } } abstract class Circle implements Shape { private double _x, _y, _r; public draw () { drawArc( _x, _y, _r, 0, 360); } abstract void drawArc( double x, double y, double r, double a1, double a2); } class V1Circle extends Circle { protected void drawArc( double x, double y, double r, double a1, double a2){ DP1.draw_an_arc(x,y,r,a1,a2); } } class V2Circle extends Circle { protected void drawArc( double x, double y, double r, double a1, double a2){ DP2.drawarc(_r,_x,_y,a1,a2); } } #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# interface Shape { public void draw(); } abstract class Rectangle implements Shape { private double_x1,_y1,_x2,_y2; public void draw() { drawline(_x1, _y1, _x2, _y1); drawline(_x2, _y1, _x2, _y1); drawline(_x2, _y2, _x1, _y2); drawline(_x1, _y2, _x1, _y1); } abstract void drawline( double x1, double y1, double x2, double y2); } class V1Rectangle extends Rectangle { public void drawline( double x1, double y1, double x2, double y2){ DP1.draw_a_line(x1, y1, x2, y2); } } 137# C++ Code#Example# Code#Example# void Shape::draw () {} void Rectangle::draw () { drawLine(_x1,_y1,_x2,_y1); drawLine(_x2,_y1,_x2,_y2); drawLine(_x2,_y2,_x1,_y2); drawLine(_x1,_y2,_x1,_y1); } void V1Rectangle::drawLine ( double x1, double x2, double y1, double y2) { DP1::draw_a_line(x1,y1,x2,y2); } void V2Rectangle::drawLine ( double x1, double x2, double y1, double y2) { DP2::drawline(x1,x2,y1,y2); } 138# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# void Circle::draw () { drawArc( _x, _y, _r, 0, 360); } void V1Circle::drawArc( double x, double y, double r, double a1, double a2) { DP1::draw_an_arc(x, y, r, a1,a2); } void V2Circle::drawArc ( double x, double y, double r, double a1, double a2) { DP2::drawarc( r, x, y, a1, a2); } #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  72. 72. Design'Pa*erns'Explained' Redundant#Rela8onships# Did#We#Resolve#the#Redundancy?# Shape Client +draw() Circle Rectangle +draw() + draw() 6264# V1Rectangle V2Rectangle # drawLine(x1, y1, x2, y2) # drawLine(x1, y1, x2, y2) DP1 + draw_a_line(x1, y1, x2, y2) + draw_an_arc(x, y, r, a1, a2) 139# V1Circle V2Circle #drawArc(x,y,r,a1,a2) # drawArc(x,y,r,a1,a2) DP2 + drawline(x1, x2, y1, y2) + drawarc( r,x, y, a1,a2) #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 140# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  73. 73. Design'Pa*erns'Explained' Problems#with#this#Approach# Intent#Makes#Sense#Now# !  Doesn’t#scale#well# !  GoF#Intent:## –  Another#Shape#"#3#*#2#implementa8ons# –  Number#implementa8ons#=## #######number#of#Shape#types#8mes#number#of#drawing#programs# !  Redundant# –  Drawing#with#each#shape#involves#duplica8on# –  Redundancies#between#V1Rectangle#and#V2Rectangle,#etc…# !  Confusing#and#complex# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## !  Abstrac8on:## –  Main#enAty#that#client#is#aware#of.# –  E.g.#Shape# !  Implementa8on:## –  Way#that#main#en8ty#fulfills#one#aspect#of#its#responsibili8es.## –  E.g.##drawing#behavior#–#Drawing# !  In#other#words:# –  Poor#cohesion#makes#classes#hard#to#follow# 141# –  DeDcouple#an#abstrac8on#from#its#implementa8on#so#that#the#two#can# vary#independently#*## –  Way#to#add#new#Shapes#OR#new#Drawing#Programs#without#affec8ng#the# other# #16#October#2013# 142# *Design%Pa2erns,%Elements%of%Reusable%Object<Oriented%So?ware,#Gamma,#Helm,#Johnson,#Vlissides# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  74. 74. Design'Pa*erns'Explained' Discovering#the#Bridge#PaWern# We#Have#the#Following#Commonali8es# !  Discover#the#Bridge#paWern#by:# –  Find#commonali8es#and#how#they#vary## –  Represent#common#concepts#with#abstrac8ons#(interfaces)# –  Make#concrete#class#for#each#varia8on## !  Then:# !  Shape#class#=#abstrac8on#with#varying#behavioral# implementa8ons# !  Commonality#Variably#Analysis#separates#issues#into#isD a#rela8onships# –  Observe#how#abstrac8ons#relate# –  Pick#rela8onship#with#beWer#code#quali8es# Shape +draw() 143# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 144# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# Drawing +drawLine() +drawCircle() +drawArc()0 #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  75. 75. Design'Pa*erns'Explained' Derive#Varia8ons0 Determine#Remaining#Rela8onships0 Drawing Shape +drawLine() +drawCircle() +drawArc()0 +draw() Rectangle Shape Rectangle Circle +draw() +drawLine() +drawCircle() +draw() V1Drawing Circle +drawLine() +drawArc()# +drawCircle() +draw() +draw() V1Drawing V2Drawing +drawLine() +drawCircle() V2Drawing +drawLine() +drawArc()# +drawCircle() +draw() Drawing Which Way? +drawLine() +drawCircle() Each side should "Design to Interfaces” 145# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 146# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  76. 76. Design'Pa*erns'Explained' Use#Quali8es#to#Define#Rela8onship# Another#Quality#D#Asser8veness## Paradigm00 PaKerns0 !  Ways#the#two#sides#can#relate:# Wisdom0 Principles0 Prac+ces0 c# –  Shapes#use#drawing#programs# –  Drawing#programs#draw#shapes# –  Third#object##gets#shape#and#drawing#program## Quali+es## !  Asser8veness### –  Don’t#ask#ques8ons#of#an#object## –  Assert#that#an#object#should#do#something# !  Which#way#is#beWer?# –  Score#with#quali8es# –  BeWer#quali8es#"#beWer#design## 147# !  Example#on#next#slide# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 148# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  77. 77. Design'Pa*erns'Explained' Inquisi8ve# Asser8ve# class Date { int getMonth(); int getDay(); int getYear(); } Date d = new Date(); String output = d.getMonth() + “/” + d.getDay() + “/” + d.getYear(); 149# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# class Date { String getAsString() { ] } Date d = new Date(); String output = d.getAsString(); # 150# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  78. 78. Design'Pa*erns'Explained' Scoring#–#Fill#It#In# Shapes#use#DP# DP#given#Shape# Shapes#Using#Drawing#Programs# 3rd#Module# : Client Loose#Coupling# : Rectangle : DP1 1: draw Strong#Cohesion# 2: drawLine No#Redundancy# 3: drawLine Encapsula8on# 4: drawLine Testable# 5: drawLine Asser8ve# Focus# 151# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 152# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  79. 79. Design'Pa*erns'Explained' Drawing#Programs#Using#Shapes# : Client : DP1 Control#Program#Using#Shapes# : Rectangle : Client 1: draw( Shape s) : Control Prgm : Rectangle : DP1 1: draw( Shape, DP) 2: What kind of shape? 2: what kind of shape? 3: I'm a rectangle 3: I'm a rectangle 4: Give me your line 1 info 4: give me your line 1 info 5: Draw it 5: drawLine 6: Give me your line 2 info 6: give me your line 2 info 7: Draw it 7: drawLine 8: Give me your line 3 info 8: give me your line 3 info 9: Draw it 9: drawLine 10: Give me your line 4 info 10: give me your line 4 info 11: drawLine 11: Draw it 153# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 154# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  80. 80. Design'Pa*erns'Explained' Scoring# Add#the#Rela8onship# Shapes#use#DP# Loose#Coupling# DP#given#Shape# 3rd#Module# Yes# DPs#coupled#to# Shapes# Module#coupled#to# Shapes# Strong#Cohesion# Yes# DPs#concerned#with# Shapes# Yes# Knowledge#of#shapes# duplicated#in#DPs# Knowledge#of#shapes# duplicated#in#CP# Encapsula8on# implementa8on# and#type# implementa8on#only# implementa8on#only# Testable# N#+#M# N#*#M# N#+#M# Asser8ve# Yes# No# No# Focus# Yes# Shapes#spread#out#a# liWle# Yes# +drawLine() +drawArc() +draw() Rectangle +draw() Drawing 1 1..1 Weak:#Shape#and# Draw#stuff# No#Redundancy# Shape Circle +draw() V1Drawing +drawLine() +drawArc() drawing.drawLine()# # V2Drawing +drawLine() drawArc() DP1 DP2 +draw_a_line() +draw_an_arc() +drawline() +drawarc() Shapes using DPs wins! 155# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 156# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  81. 81. Design'Pa*erns'Explained' C# Illustra8ng#the#Separa8on# 1..1 +draw() Rectangle +draw() Circle +draw() Abstraction Drawing 1 Shape +drawLine() +drawArc() V1Drawing V2Drawing +drawLine() +drawArc() +drawLine() +drawArc() DP1 DP2 +draw_a_line() +draw_an_arc() +drawline() +drawarc() Implementation 157# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## Example#Implementa8on## class Client { interface Shape { public static void Main public void draw(); } (String argv[]) { Shape s1; class Rectangle : Shape { Shape s2; private Drawing _dp; Drawing dp; // See comments below to see Rectangle (Drawing dp, // what gets returned double x1, double y1, dp= Factory.getDP(); double x2, double y2) { s1= Factory.getShape(dp); _dp = dp; dp= Factory.getDP(); _x1= x1; _y1= y1; s2= Factory.getShape(dp); s1.draw(); _x2= x2; _y2= y2;} s2.draw(); public override void draw () { } } _dp.drawLine(_x1, _y1, _x2, _y1); // Factory: _dp.drawLine(_x2, _y1, _x2, _y2); // 1st call: getDP returns V1Drawing // 1st call: getShape returns _dp.drawLine(_x2, _y2, _x2, _y1); // new Rectangle(dp,1,1,2,2); _dp.drawLine(_x2, _y1, _x1, _y1); nd call: getDP returns V2Drawing // 2 } } nd call: getShape returns // 2 // new Circle( dp,2,2,4); #16#October#2013# 158# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  82. 82. Design'Pa*erns'Explained' C# class Circle : Shape { private Drawing _dp; Circle (Drawing dp, double x, double y, double radius) { _dp = dp; _x= x; _y= y; _radius= radius; } public override void draw () { _dp.drawArc(_x,_y,_radius, 0, 360); } } 159# C# Example#Con8nued# interface Drawing { void drawLine ( double x1, double y1, double x2, double y2); void drawArc(double x, double y,double radius, double fromAngle, doubletoAngle);} class V1Drawing : Drawing { public override void drawLine ( double x1, double y1, double x2, double y2) { DP1.draw_a_line( x1, y1, x2, y2); } public override void drawArc ( double x, double y, double radius, double fromAngle, double toAngle){ DP1.draw_an_arc( x, y,radius, fromAngle,toAngle); } } #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# Example#Con8nued# // With abstract base class abstract class Shape { private Drawing _dp; class Rectangle : Shape { Rectangle : base(dp) ( Drawing dp, Shape (Drawing dp) { double x1, double y1, _dp= dp; double x2, double y2) } _x1= x1; _y1= y1; abstract void draw (); protected void drawLine ( _x2= x2; _y2= y2; double x1, double y1, } double x2, double y2) { public override void draw _dp.drawLine( x1, y1, x2, y2);} drawLine(_x1, _y1, _x2, protected void drawArc ( drawLine(_x2, _y1, _x2, double x, double y, double radius, double fromAngle, drawLine(_x2, _y2, _x2, double toAngle){ drawLine(_x2, _y1, _x1, _dp.drawArc( x,y,radius, } fromAngle, toAngle); } } { () { _y1); _y2); _y1); _y1); } 160# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  83. 83. Design'Pa*erns'Explained' C# Java Example#Con8nued# class V2Drawing : Drawing { public override void drawLine (double x1,double y1, double x2, double y2){ DP2.drawline( x1, x2, y1, y2);} public override void drawArc ( double x, double y, double radius, double fromAngle, double toAngle) { DP2.drawarc(radius, x, y, fromAngle, toAngle); } } class DP1 { static void draw_a_line ( double x1, double y1, double x2, double y2) { // draw_a_line implementation given to us} static void draw_an_arc( double x, double y, double radius, double from_angle, double to_angle) { // draw_an_arc implementation given to us } } class DP2 { static void drawline ( double x1, double y1, double x2, double y2) { // drawline implementation given to us } static void drawarc( double radius, double x, double y, double fromangle, double toangle) { // drawarc implementation given to us } } 161# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# Example#Implementa8on## class Client { interface class Shape { public static void main void draw (); (String argv[]) { } Shape s1; Shape s2; class Rectangle implements Shape{ Drawing dp; private Drawing _dp; // See comments below to see Rectangle (Drawing dp, // what gets returned double x1, double y1, dp= Factory.getDP(); double x2, double y2) { s1= Factory.getShape(dp); _dp = dp; dp= Factory.getDP(); _x1= x1; _y1= y1; s2= Factory.getShape(dp); _x2= x2; _y2= y2;} s1.draw(); public void draw () { s2.draw(); }} _dp.drawLine(_x1,_y1,_x2,_y1); // Factory: _dp.drawLine(_x2,_y1,_x2,_y2); // 1st call: getDP returns V1Drawing _dp.drawLine(_x2,_y2,_x2,_y1); // 1st call: getShape returns _dp.drawLine(_x2,_y1,_x1,_y1); // new Rectangle(dp,1,1,2,2); } } // 2nd call: getDP returns V2Drawing // 2nd call: getShape returns // new Circle( dp,2,2,4); 162# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  84. 84. Design'Pa*erns'Explained' Java class Circle implements Shape { private Drawing _dp; Circle (Drawing dp, double x, double y, double radius) { _dp = dp; _x= x; _y= y; _radius= radius; } public void draw () { _dp.drawArc(_x,_y,_radius, 0, 360); } } 163# Java Example#Con8nued# interface Drawing { void drawLine ( double x1, double y1, double x2, double y2); void drawArc(double x, double y, double radius, double fromAngle,double toAngle);} class V1Drawing implements Drawing { public void drawLine ( double x1, double y1, double x2, double y2) { DP1.draw_a_line(x1,y1,x2,y2); } public void drawArc ( double x,double y,double radius, double fromAngle, double toAngle){ DP1.draw_an_arc( x, y,radius, fromAngle, toAngle); } } #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# Example#Implementa8on##With#Abstract# // with abstract class abstract class Shape { private Drawing _dp; Shape (Drawing dp) { _dp= dp; } abstract void draw (); protected void drawLine ( double x1, double y1, double x2, double y2) { _dp.drawLine( x1, y1, x2, y2); } protected void drawArc ( double x, double y, double radius, double fromAngle, double toAngle){ _dp.drawArc( x, y, radius, fromAngle, toAngle); } } 164# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# class Rectangle extends Shape { Rectangle (Drawing dp, double x1, double y1, double x2, double y2) { super( dp); _x1= x1; _y1= y1; _x2= x2; _y2= y2; } public void draw () { drawLine(_x1, _y1, _x2, _y1); drawLine(_x2, _y1, _x2, _y2); drawLine(_x2, _y2, _x2, _y1); drawLine(_x2, _y1, _x1, _y1); } } class Circle extends Shape { Circle (Drawing dp, double x, double y, double radius) { super( dp); _x= x; _y= y; _radius= radius; } public void draw () { drawArc(_x,_y,_radius, 0, 360); } } #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  85. 85. Design'Pa*erns'Explained' Java C++ Example#Con8nued# class V2Drawing implements Drawing { public void drawLine ( double x1, double y1, double x2, double y2) { DP2.drawline( x1, x2, y1, y2);} public void drawArc ( double x, double y, double radius, double fromAngle, double toAngle) { DP2.drawarc(radius, x, y, fromAngle, toAngle); } } class DP1 { static void draw_a_line ( double x1, double y1, double x2, double y2) { // draw_a_line implementation given to us } static void draw_an_arc( double x, double y, double radius, double from_angle, double to_angle) { // draw_an_arc implementation given to us } } class DP2 { static void drawline ( double x1, double y1, double x2, double y2) { // drawline implementation given to us } static void drawarc( double radius, double x, double y, double fromangle, double toangle) { // drawarc implementation given to us } } 165# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# Example#Implementa8on# void main (String argv[], int arc) { Shape *s1; Shape *s2; Drawing *dp; dp= Factory::getDP(); s1= Factory::getShape(dp); dp= Factory::getDP(); s2= Factory::getShape(dp); s1->draw(); s2->draw(); } NOTE: Memory management has not been tested and the INCLUDE statements are not being shown. 166# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# class Shape { public: Shape (Drawing *dp); virtual void draw(); protected: drawLine( double x1, double y1, double x2, double y2); drawArc( double x, double y, double rad, double fromAngle, double toAngle); private: Drawing *_dp; }; Shape::Shape (Drawing *dp) { _dp= dp; } void Shape::drawLine( double x1, double y1, double x2, double y2) { _dp->drawLine( x1, y1, x2, y2); } void Shape::drawArc( double x, double y, double radius, double fromAngle, double toAngle){ _dp->drawArc( x, y, radius, fromAngle, toAngle); } ! ! #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  86. 86. Design'Pa*erns'Explained' C++ class Rectangle : public Shape { public: Rectangle (Drawing *dp, double x1, double y1, double x2, double y2); private: double _x1, _y1, _x2, _y2; }; Rectangle::Rectangle (Drawing *dp, double x1, double y1, double x2, double y2) : Shape(dp) { _x1=x1; _y1=y1; _x2=x2; _y2=y2; } void Rectangle::draw drawLine(_x1, _y1, drawLine(_x2, _y1, drawLine(_x2, _y2, drawLine(_x2, _y1, } 167# C++ Example#Con8nued# () { _x2, _x2, _x2, _x1, _y1; _y2); _y1); _y1); # # # class Circle : public Shape { public: Circle ( Drawing *dp, double x, double y, double radius); private: double _x, _y, _radius; }; Circle::Circle (Drawing *dp, double x, double y, double radius) : Shape(dp) { _x= x; _y= y; _radius= radius; } void Circle::draw () { Shape::drawArc( _x,_y,_radius,0,360); } #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# Example#Con8nued# class Drawing { public: virtual void drawLine (double x1,double y1, double x2,double y2) =0; virtual void drawArc (double x1, double y1, double radius, double fromAngle, double toAngle)=0; }; class V2Drawing : public Drawing { class V1Drawing : public Drawing { public: public: void drawLine ( void drawLine ( double x1, double x1, double y1, double y1, double x2, double y2); double x2, double y2); void drawArc ( double x1, void drawArc ( double x, double y, double y1, double radius, double radius, double fromAngle, double fromAngle, double toAngle); double toAngle); }; }; void V2Drawing::drawLine ( void V1Drawing::drawLine (double x1, double x1,double y1, double y1, double x2, double y2) { double x2,double y2){ DP1::draw_a_line( x1, y1, x2, y2); DP2::drawline( x1, x2, y1, y2); } } void V1Drawing::drawArc ( void V2Drawing::drawArc ( double x, double y, double radius, double x,double y, double radius, double fromAngle, double toAngle){ double fromAngle, double toAngle){ DP1::draw_an_arc(x,y,radius, DP2::drawarc( radius, x, y, fromAngle, toAngle); fromAngle, toAngle); } } 168# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  87. 87. Design'Pa*erns'Explained' C++ Example#Con8nued# Where#is#varia8on#coming?# class DP1 { public: static void draw_a_line ( double x1, double y1, double x2, double y2); static void draw_an_arc( double x, double y, double radius, double fromAngle, double toAngle); }; Drawing Program/ Shape Line class DP2 { public: static void drawline ( double x1, double x2, double y1, double y2); static void drawarc ( double radius, double x, double y, double fromAngle, double toAngle); }; DP1 DP2 Rectangle // Implementations given to us Circle 169# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 170# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  88. 88. Design'Pa*erns'Explained' Look#For#Abstrac8ons# Drawing#Another#Way# !  Commonali8es#could#be## DrawingPrimi8ve## –  Func8ons# –  Data## Shape# DrawingPrimi8ve[]#getPrimi8ves();## Line# !  Alterna8ve#interfaces#may#produce#different#design## !  Example#on#next#slide## Arc# In#DrawingProgram:#### draw(Shape#s)#{# ##DrawingPrimi8ve#[]#dp#=#s.getDrawingPrimi8ves();## ##//#Now#draw#each#one## ##}# # What#are#advantages#/#disadvantages?# 171# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 172# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  89. 89. Design'Pa*erns'Explained' Drawing#Another#Way#(2)# Drawing#Another#Way#–#One#Class##(3)# interface Shape { DrawingPrimitive [] getPrimitives(); } class Rectangle implements Shape { double x1; double y1; double x2; double y2; DrawingPrimitive [] getPrimitives() { DrawingPrimitive [] dp = { new Line(x1,y1,x1,y2), new Line(x1,y1,x2,y1), new Line(x2,y1,x2,y2), new Line(x1,y2,x2,y2)} return dp; } 173# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## class Shape { Shape (DrawingPrimitive [] primitives) { this.primitives = primitives; } DrawingPrimitive [] primitives; DrawingPrimitive [] getPrimitives() { return primitives }; } // Each object has its own primitives Shape rectangle({ new Line(x1,y1,x1,y2), new Line(x1,y1,x2,y1), new Line(x2,y1,x2,y2), new Line(x1,y2,x2,y2)}); #16#October#2013# 174# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  90. 90. Design'Pa*erns'Explained' How#Drawing#Usually#Works#(op8onal)# Who’s#In#Charge?## Drawing#(Graphics#Device#Interface)#draws#specific#shapes#(not# derived#from#a#Shape)## interface GDI { drawRectangle(Rectangle r) drawCircle(Circle c) drawLine(Line l) drawPolyline(Line [ ] lines ) } !  Class#PhoneNumber# –  Dial#(Phone)# !  Class#Phone# –  Dial(PhoneNumber)# # class Screen implements GDI //What are advantages / disadvantages? 175# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 176# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  91. 91. Design'Pa*erns'Explained' Who’s#In#Charge#(2)?## Who’s#In#Charge#(3)#?# !  Interface#Dialable## !  One#more#thing#to#dial:### –  DialableString#getStringToDial();# !  Class#AccountNumber# !  Class#AccountNumber#implements#Dialable#{# –  Dial#(Phone)# #DialableString#getStringToDial()#{##//..##}## #}# !  Class#PhoneNumber#implements#Dialable## !  Class#Phone# –  Dial(PhoneNumber)# –  Dial(AccountNumber)## !  Class#Phone# –  Dial(Dialable)# # # 177# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 178# # Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  92. 92. Design'Pa*erns'Explained' Bridge#PaWern#as#an#Example## of#the#GoF#Approach# Canonical#Bridge#PaWern# Abstraction (1)  Find0what0varies0(the0abstrac+ons0and00implementa+ons)0and## (2)  Encapsulate0it.0 (3)  Favor00object0delega+on0over0classIinheritance0# Implementation + operation() + opImp1() + opImp2() Abstraction Concrete1 + operation() C oncrete2 ImpA operat ion() { im p.opIm p1() } operation() { imp.opImp2() } ImpB + opImp1() + opImp2() + opImp1() + opImp2() (2)'encapsulates'variaAon ' Concrete1 Implementation + opImp1() + opImp2() (2)'encapsulates'variaAon ' (1) ' (1) ' ImpA operat ion() { im p.opIm p1() } 179# C oncrete2 (3) ' #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 180# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# operation() { imp.opImp2() } ImpB + opImp1() + opImp2() + opImp1() + opImp2() #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  93. 93. Design'Pa*erns'Explained' The#Adapter#PaWern# Paradigm00 !  Issue:# The'Adapter' Pa*ern' PaKerns0 –  Class#has#right#stuff## !  But#wrong#interface# Wisdom0 Principles0 Prac+ces0 Quali+es## !  GoF#Intent:# –  #Convert#class#interface#into#the#one#client#expects## –  #Allows#classes#with#incompa8ble#interfaces#to#work#together# 181# ### #16#October#2013# 182# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# *Design%Pa2erns,%Elements%of%Reusable%Object<Oriented%So?ware,#Gamma,#Helm,#Johnson,#Vlissides# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  94. 94. Design'Pa*erns'Explained' Our#Problem# What#We#Have# Shape XX_Circle + setLocation() + getLocation() + display() + fill() + setColor() + undisplay() Point 183# Line Square + display() + fill() + undisplay() + display() + fill() + undisplay() + set_location() + get_location() + display_it() + fill_it() + set_its_color() + undisplay_it() + display() + fill() + undisplay() #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 184# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  95. 95. Design'Pa*erns'Explained' What#We#Want# How#to#Implement#the#PaWern# Shape !  Want#XX_Circle#to#behave#like#a#Shape# + setLocation() + getLocation() + display() + fill() + setColor() + undisplay() –  No#polymorphism#with#XX_Circle:# XX_CIRCLE = Shape Point XX_Circle’s#interface# 185# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# Square Circle XX_Circle + display() + fill() + undisplay() !  Author#of#XX_Circle#not#willing#or#able#to#change# Line + display() + fill() + undisplay() + display() + fill() + undisplay() + display() + fill() + getLocation() + setLocation() + setColor() + undisplay() + set_location() + get_location() + display_it() + fill_it() + set_its_color() + undisplay_it() 186# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  96. 96. Design'Pa*erns'Explained' C# Java Example# class Circle : Shape { … private XX_circle pxc; … public Circle () { pxc= new XX_circle(); } Example# class Circle extends Shape { … private XX_circle pxc; … public Circle () { pxc= new XX_circle(); } public void override display() { pxc.display_it(); } public void display() { pxc.display_it(); } } 187# } #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 188# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  97. 97. Design'Pa*erns'Explained' C++ Example# Adap8ng# IN HEADER class Circle : public Shape { private: XX_circle *pxc; } // May have some state to keep IN CONSTRUCTOR instantiate and initialize XX_circle. Have it refer to pxc IN CODE void Circle::display () { pxc->display_it(); } 189# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# class Circle implements Shape { private XX_circle pxc; private Color color; setColor(Color c) { this.color = c; } fill() { pxc.fill_it(color):; } } 190# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  98. 98. Design'Pa*erns'Explained' General#Approach# !  Client#works#with#other#classes#without#"knowing"# which#one# –  Create#an#interface# –  Adapt#each#class#to#implement#that#interface# The'Façade' !  Adap8ng##is#also#called#"wrapping"# 191# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 192# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# ### #16#October#2013# 16#October#2013#
  99. 99. Design'Pa*erns'Explained' The#Façade#PaWern# Using#a#Complex#System# Paradigm00 PaKerns0 Wisdom0 Principles0 Prac+ces0 !  If#your#group#had#to#use#parts#of#a#complex#system,# Quali+es## !  Want#:# would#you:# –  To#use#a#subDset#of#a#complex#system# –  Or#to#provide#OO#access#to#a#nonDOO#system# A.  Have#everyone#learn#how#to#use#it?# B.  Have#one#person#create#an#interface#to#use?# !  GoF#Intent*:## !  If#you#picked#B,#what#that#person#created#would#be#a# Façade# –  Provide#a#unified#interface#to#a#set#of#interfaces#in#a# subsystem## –  Facade#defines#a#higherDlevel#interface#that#makes#the# subsystem#easier#to#use# 193# *Design%Pa2erns,%Elements%of%Reusable%Object<Oriented%So?ware,#Gamma,#Helm,#Johnson,#Vlissides# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 194# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  100. 100. Design'Pa*erns'Explained' Insula8ng#Ourselves#From#the#Complex#System# Façade#Example#(1)# !  #Example# Façade# StoreStringInFile(String filename, String data); // calls open, write, close String RetrieveStringFromFile(String filename); // calls open, read, close !  Error#handling#(try,#catch,#etc.)#in#the#implementa8on#code# !  Allows#easy#mock## –  Read/write#to#memory,#rather#than#a#file## # 195# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 196# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  101. 101. Design'Pa*erns'Explained' Façade#Example#(2)# Standard#Façade#vs.#Encapsula8ng#Facade# LiWle#more#complete# !  Standard#Façade#provides# SimpleFileStorageFacade# ####SelectStorageDirectory(String#directory)## ####StoreStringInFile(String#filename,#String#data)#:#boolean# ####RetrieveStringfromFile(String#filename)#:#String# File# //#Read,#write,#open,#close# //#Excep8on#handling## interface# –  Clients#may#use#underlying# system#directly# !  Encapsula8ng#Façade# restricts#access#to#the# underlying#system## Directory# //#change#working#directory# –  Clients#must#use#Façade# SimpleFileStorageFacadeMock# ###SelectStorageDirectory(String#directory)## ###StoreStringInFile(String#filename,#String#data)#:#boolean# #//#stores#in#memory#–#maybe#a#map#?## ###RetrieveStringfromFile(String#filename)#:#String# #//#retrieves#from#memory## 197# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 198# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  102. 102. Design'Pa*erns'Explained' Comparing#Façade#With#Adapter# Client ? Adapter#and#Façade# Pre-Existing !  Use##Adapter#and#Façade# which pattern? Façade# Adapter# Pre5ExisAng,'or'Foreign' classes?' Yes# Yes# Is'there'an'exisAng' interface'we'must'adhere' to?' No# Yes# Are'we'trying'to'enable' polymorphism?' No# Usually# Are'we'creaAng'a'new' interface'for' simplificaAon?' Yes# No# Yes,#Façades# tend#to#be#large# Not#usually# Is'statefullness'a'concern?' 199# –  Defer#Worrying#About#Your#Interface# –  Make#interfaces#of#classes#to#be#the#way#you#want# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## !  Design#by#context# –  #First#figure#out#what#you#want## –  Then#adapt#(or#simplify#with#a#Facade)#the#interface#to#that# #16#October#2013# 200# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  103. 103. Design'Pa*erns'Explained' Microso_#Founda8on#Classes#(MFC)#Façade## Façade###Usage## Applica8on# MFC#Classes## ###Window## ###Dialog## Windows#SDK## ###Win#98## Windows#SDK## ###Windows#2000# Your#system# Windows#SDK## ###Windows#7## Façade## Third#party#library## Note:#NonDencapsula8ng#facade# 201# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 202# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  104. 104. Design'Pa*erns'Explained' Strategy#Revisited# !  Abstract#Factory#is#strategy#of#strategies## !  Here’s#an#example# The'Abstract' Factory' 203# ### #16#October#2013# 204# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  105. 105. Design'Pa*erns'Explained' Switch#to#Strategy(1)# Switch#to#Strategy#(2)# To#a#strategy# // Just a switch, not a strategy enum Country {USA, MEXICO, CHINA}; functionOne (Country c){ switch(c) { case USA: Switch#in#one#func8on# doOneUSA(); break; case MEXICO: doOneMexico(); break; case CHINA: doOneChina(); break; } }; 205# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## interface Strategy { void doFunctionOne(); }; class USAStrategy implements Strategy { void doFunctionOne() { doOneUSA(); }}; class MexicoStrategy implements Strategy { void doFunctionOne(){ doOneMexcio(); } }; // .. And so on #16#October#2013# 206# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# //FACTORY Strategy getStrategy(Country c){ switch(c) case USA: return new USAStrategy(); case MEXICO: return new MexicoStrategy(); case CHINA: return new ChinaStrategy(); break; } }; // USAGE: void functionOne (Country c){ Strategy s = getStrategy(c); s.doFunctionOne();} #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  106. 106. Design'Pa*erns'Explained' Switch#to#Strategy#(3)# functionTwo (Country c){ switch(c) { case USA: doTwoUSA(); break; case MEXICO: doTwoMexico(); break; case CHINA: doTwoChina(); break; } }; 207# How#to#organize#?# County/ Function FunctionOne Now#a#2nd#switch## USA MEXICO FunctionTwo FunctionThree #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 208# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  107. 107. Design'Pa*erns'Explained' Switch#to#Strategy#(4)# Add#to#strategy# interface Strategy { void doFunctionOne(); void doFunctionTwo(); }; class USAStrategy implements Strategy { doFunctionOne() { doOneUSA(); } doFunctionTwo() { doTwoUSA(); }; 209# Switch#to#Strategy#(5)# class MexicoStrategy implements Strategy { void doFunctionOne() { doOneMexcio(); } void doFunctionTwo() { doTwoMexcio(); } }; // .. And so on interface Strategy { void doFunctionOne(); void doFunctionTwo(); //…. void doFunctionNintyNine(); }; void functionTwo (Country c){ Strategy s = getStrategy(c); s.doFunctionTwo();} #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# Now#99##func8ons## added#to#strategy# class USAStrategy implements Strategy { void doFunctionOne() { doOneUSA(); } void doFunctionTwo() { doTwoUSA(); }; //…. void doFunctionNinetyNine() { 210# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  108. 108. Design'Pa*erns'Explained' Switch#to#Strategy#(5)# Break#into#related#strategies# interface StrategyA { { // Related functions void doFunctionOne(); void doFunctionTwelve(); }; interface StrategyFactory { StrategyA getStrategyA(); StrategyB getStrategyB(); ///… class USAStrategyA implements StrategyZ getStrategyZ(); StrategyA { }; void doFunctionOne() { class USAStrategyFactory() doOneUSA(); implements StrategyFactory { } StrategyA getStrategyA() { void doFunctionTwelve() { return new USAStrategyA(); doTwelveUSA(); } } }; StragegyB getStrategyB(){ return new USAStrategyB(); } //.. And so forth 211# }; #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# Switch#to#Strategy#(6)# Now#get#the#strategy#factory# StragetyFactory getInstanceStrategyFactory(Country c){ switch(c) { case USA: new USAStrategyFactory( (); break; case MEXICO: new MexicoStrategyFactory( (); break; case CHINA: new ChinaStrategyFactory( (); break; } }; 212# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  109. 109. Design'Pa*erns'Explained' Switch#to#Strategy#(7)# The#Abstract#Factory#PaWern# Usage:# !  GoF#Intent:## void functionTwelve(Country c) { StrategyFactory sf = getInstanceStrategyFactory(c); StrategyA sa = sf.getStrategyA(); sa.doFunctionTwelve(); } –  Need#to#create#families#of#objects#for#par8cular#situa8ons.## –  Provide#interface#for#crea8ng#families#of#related#or# dependent#objects## !  Without#specifying#their#concrete#classes# // Instead of previous operation void functionTwelve(Country c){ Strategy s = getStrategy(c); s.doFunctionTwelve();} Design0PaKerns:0Elements0of0Reusable0ObjectIOriented0SoLware#by#Gamma,#Helm,#Johnson,#Vlissides.#AddisonD Wesley#Professional.#1995.# 213# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 214# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  110. 110. Design'Pa*erns'Explained' Abstract#Factory:#Contextual#Forces# !  Mo8va8on:## Not'an'Ending,' But'a'Beginning ' –  Instan8ate#families#of#objects# !  Encapsula8on:## –  Number#of#cases# –  Concrete#types#of#all#services# –  Issue#that#case#varies## 215# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 216# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# ### #16#October#2013# 16#October#2013#
  111. 111. Design'Pa*erns'Explained' Purpose#of#this#Course#(Revisited)# Summary# !  Present#a#powerful#way#of#designing#applica8on#architectures# !  Incorporates:# –  –  –  –  217# Code#Quali8es## Design#PaWerns# CommonalityDVariability#Analysis# Contextual#Design#(a#la#Alexander)# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# !  Design#in#context## !  Code#quali8es##as#basis#for#paWerns## !  Favor#delega8on#over#inheritance# !  Encapsulate#variability# !  What#else?## 218# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  112. 112. Design'Pa*erns'Explained' info@netobjec+ves.com000 www.netobjec+ves.com 0 Supplemental' Thank#You# # Please#Fill#Out#Evalua8ons# 219# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 220# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2008#Net#Objec8ves.#All#Rights#Reserved.## #16#October#2013# 16#October#2013#
  113. 113. Design'Pa*erns'Explained' Another#Issue#of#Perspec8ve# !  PaWerns,#e.g.#strategy,#bridge## –  Create#context#for#factory## Client !  Factories## Shape –  Only#construct#objects,#do#not#use# objects# Factories' +centerX:int +centerY:int Paradigm00 Circle Wisdom0 Principles0 Prac+ces0 Square +diameter:int PaKerns0 +width:int +height:int Quali+es## Factory +make():Shape 221# ### #16#October#2013# 222# Copyright#©#2007#Net#Objec8ves.#All#Rights#Reserved.# #Copyright#©#2013##Net#Objec8ves.#All#Rights#Reserved.## ~2#16#October#2013# 16#October#2013#

×