cisc 323 intro to software engineering
play

CISC 323 Intro to Software Engineering Week 6: Design Patterns CISC - PowerPoint PPT Presentation

CISC 323 Intro to Software Engineering Week 6: Design Patterns CISC 323 Intro to Software Engineering Lecture 6-1 Introduction to Design Patterns


  1. � ✫ ✽ ✵ ✧ ✼ ✭ ✪ ✶ ✧ ✭ ✧ ✮ ❀❁ ✴✾ ✫ ✫ ✧ ✴ ✮ ✾ ✼ ❂ ❈ ❉ ✪ ✿ ❆ ✽ ✧ ✼ ✶ ✮ ✧ ✥ ✫ ✁ ✧ ✴ ✮ ✿ ✱ ❀ ✽ ✴ ✼ ✪ ❀ ✧ ✼ ✽ ✮ ✾ ❈ ❇ ✫ ❝ ❚ ❪ ❙ ❳❨ ❱ ❳❨ ❩ ❛ ❲ ❜ ❩ ❳❨ ❳❨ ❩ ❞ ❩ ❳ ❜ ❩ ❡ ❩ ❳ ❜ ❚ ❩ ◗❘ ❋ ❋ ● ● ❇ ❍■ ■ ❊ ❇ ❏ ❑ ❆ ▲ ❈ ◆ ▼ ❊ ❏ ❏ ❍ ❇ ❍■ ■ ❊ ❇ ❏ ✸ ✫ ✴ ☎✆ ✖ ✗ ✗ ✘ ✛ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✒ ✓ ✒ ✥ ✕ ☞ ✧ ☎ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☛ ✔ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✩ ✛ ✫ ✧ ✸ ✧ ✻ ✲ ✧ ✫ ✮ ✼ ✧ ✱ ✼ ✽ ✺ ✧ ✫ ✮ ✪ ✫ ✸ ✴ ✫ ✸ ✧ ✧ ✳ ✫ ✭ ✧ ✫ ✧ ✬ ✧ ✭ ✧ ✱ ✧ ✴ ✫ ✸ ✧ ✮ ✹ ✶ (2) publish = raiseEvent Exchange Publisher Rate $Cdn cost of connectAsTarget (1) subscribe = Subscriber hotel Event-Connector ✗✤✣ Implementation ✘✚✙ ❴✤❡ ✪★✫ ❙✤❫ ◗❭❬ ✮✰✯ ✵✷✶ ❯✷❲ ✵✷✶ ❅✤❊ ❯✷❲ ❅✤❆❇ ❯✷❱ ❃✤❄ ✵✷✶ ❯✷❲ ✵✷✶ ❙★❚ ✪★✫ ✦★✧ ✺✤✾ ✲✤✳ ❖✤P ❴✤❵

  2. � ☎✆ ✖ ✁ ✗ ✘ ✛ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✗ ☞ ☛ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✔ ✡ ☎ ✠ ✞ ✏ ✒ ✓ ✠ ✑ ✎✒ ✎ ✍ ✌ ☞ Code for Target (Subscriber) Interface // Target implements this method. It is called by the // event connector whenever a new event is fired. public interface EventTarget { public void performEvent (EventObject e); } ✗✤✣ ✘✚✙

  3. � ☎✆ ✖ ✗ ✘ � ✛ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✗ ☞ ☛ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✔ ✠ ✡ ☎ ✆ ✞ ✏ ✒ ✠ ✎✒ ✑ ✓ ✎ ✍ ✌ ☞ Code for New Event Type import java.util.EventObject; public class ExchangeRateChangedEvent extends EventObject { private float newExchangeRate; public int getNewExchangeRate () { return newExchangeRate; } public ExchangeRateChangedEvent ( Object source, float newRate) { super (source); newExchangeRate = newRate; } } ✗✤✣ ✘✚✙

  4. � ☎✆ ✖ ✗ ✗ ✘ ✛ ✞ � ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✙ ☞ ☛ ✂ ✄ ☎ ✔ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☎✆ ✞ ☞ ✌ ✍ ✠ ✎ ✏ ✑ ✎✒ ✓ ✒ EventConnector (Publisher) Code public class EventConnector { //There are zero or more targets represented as a vector private Vector targets = new Vector(); // When a new event is raised, the "performEvent" method of // each target is invoked public void raiseEvent (EventObject e) { for (int i=0; i<targets.size(); i++) { EventTarget t = (EventTarget) targets.elementAt (i); t.performEvent (e); } } //Each target must register itself as a target of this //connector. public void connectAsTarget (EventTarget targetComponent) { targets.add(targetComponent); } } ✗✤✣ ✘✚✙

  5. ✓ � ☎✆ ✖ ✗ ✗ ✘ ✂ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✛ ☞ ☛ ✄ ☎ ☎✆ ✝ ✞ ✔ ✆ ✠ ✡ ☎ ✟ ✞ ☞ ✌ ✍ ✎ ✏ ✠ ✑ ✎✒ ✓ ✒ More on Publish/Subscribe Basis of the Event-based Architectural Style • • Greatly improves modifiability and extendibility of systems – At the expense of interaction complexity – which may make analysis more difficult • Used in most (all) graphical user interface frameworks • Also very popular in development environments (e.g. IBM Eclipse) where a variety of tools can be easily integrated ✗✤✣ ✘✚✙

  6. � ☎✆ ✕ ✖ ✖ ✁ ✚ ✞ ✘ ✛ ✄ ☎ ✞ ☞ ✄ ✘ ✚✜ ✚ ✠ ✞ ✚ ✒ � ✒ ✔ ✗ ☞ ☛ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✓ ✠ ✡ ☎ ✆ ✞ ✏ ✒ ✠ ✎✒ ✑ � ✎ ✍ ✌ ☞ Sequence Diagram Publisher Subscriber1 Subscriber2 connectAsTarget connectAsTarget raiseEvent (change in state initiated by outside source or Publisher) performEvent performEvent ✖✣✢ ✗✙✘

  7. � ☎✆ ✕ � ✖ ✗ ✚ ✞ ✘ ✛ ✄ ☎ ✞ ☞ ✄ ✘ ✚✜ ✚ ✠ ✞ ✚ ✒ � ✒ ✔ ✖ ☞ ☛ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✓ ✡ ☎ ✠ ✞ ✏ ✒ � ✠ ✑ ✎✒ ✎ ✍ ✌ ☞ Where do Design Patterns Come From? • Actually mined from code – General rule -- design pattern must be seen in at least three real systems before it is considered to be a design pattern ✖✣✢ ✗✙✘

  8. � ✻ ✗ ✏ ✂✷ ✏ ✸ ✕ ✹ ✺ ✑ ✠ ✗ ☎ ✂ ✼ ✂ ✠ ✂✽ � ✠ ✑ ✑ ✏ ✍ ✿ ✏ ✖ ✏ ✚ ✣ ✒ ✛✬ ✭ ✮ ✚ ✯ ✛ ✥ ✰ ✪ ✱ ✕ ✰ ✲ ✚✳ ✱ ✧ ✜ ✚ ✣ ✦ ✚ ✶ � ✞ ❀ ✫ ✹ ✕ ❄❅ ✍ ✕ ✖ ❆ ✿ ✺ ✏ ✿ ✞ ✖ ✑ ✺ ✹ ✒ ✕ ✖ ✍ ✏ ✿ ✠ ☎ ✑ ✑ ✕ ❁ ✏ ✼ ✗ ✏ ✂✷ ✏ ✂ ❀ ✒ ✠ ✿ ✿ ✂ ❃ ✖ ❂ � ✖ ✕ ✿ ✞ ✠ ✂ ✍ ✿ ✕ ✚ ✦ ✚ ✞ ✖ ✖ ✗ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✄ ✔ ✘ ✚✜ ✚ ✠ ✞ ✚ ✒ � ✒ � ✪ ✕ ☞ ✡ ☛ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ✞ ☞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ � ✒ ✓ ✞ ✗ ✕ ✔ ✛ ✔ ✕ ✗ ✣ ✖ ✠ ✖ ✍ ✕ ✠ ✘ ✑ ☎ ✗ ✗ ☎ ✍ ✏ ✗ ✞ ✦ ✌ ✂ ✩ ★ ✛ ✣ ✧ ✚ ✦ ✞ ✠ ☛ ☛ ✑ ✍ ✡ note: this text uses C++/Smalltalk, UML-like notation pages for each design pattern listed on web & slides ✦✵✴ ✞✓✖ Bahrami mentions design patterns, no details pages for publish/subscribe pattern: 293-303 Reading ✞✓✖ Supplementary text for this topic: ✖✣✢ ✑✓✒ ✎✝✏ ✗✙✘ ✤✝✥ ✌☎✝✆ ✜✝✢ ✡✠☞☛ ☎✝✖ ✙✝✚ ✟✄✠ ☎✝✆ ✁✄✂ ✾✓✆ • • • • •

  9. CISC 323 Intro to Software Engineering Lecture 6-2 Adapter, Façade, Abstract Factory, and Flyweight Design Patterns

  10. � ☎✆ ✕ ✖ ✖ ✁ ✚ ✞ ✘ ✛ ✄ ☎ ✞ ☞ ✄ ✘ ✚✜ ✚ ✠ ✞ ✚ ✒ � ✒ ✔ ✗ ☞ ☛ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✓ ✆ ✡ ☎ ✠ ✞ ✏ ✒ � ✠ ✑ ✎✒ ✎ ✍ ✌ ☞ Adapter Design Pattern [Gamma et al., pp 139-150] • Motivation – Sometimes a class implements functionality similar to what an application requires, but not the correct interface for that application – E.g. We wish to implement a BookList class implementing a list of text book used in a course int getNumBooks() Book getBook (int n) void addBook (Book newBook) – These operations are very similar to the operations in the predefined Vector class ✖✣✢ ✗✙✘

  11. � ☞ ✌ ✝ ✞ ✌ ☞ ✆ ☞ ✌ ☞ ✝ ✍ ☛ ✝ ✌ ✞✟ ✆✝ ✘ ✁ ✂ ✙ ✠ ✁ ✌ ☞ ✒ ✓ ✝ ✖ ✗ ☞ ✝ ✏ ✞ ✠ ✝ ✞ ☞ ✠ ✟ ✝ ✒ ✏ ✝ ✑ ✏ ✎ ✒ � ✒ ✞ � ✎✒ ✑ ✏ ✎ ✍ ✌ ☞ ✠ ☛ ✓ ☎ ✡ ✠ ✆ ✟ ✞ ✝ ☎✆ ☎ ✄ ✒ ☞ ✚ ☎ ✞ ✠ ✚ ✟ ✚✜ ✘ ✄ ✞ ☎✆ ✄ ☞ ✛ ✘ ✞ ✚ ✏ ✗ ✖ ✖ ✕ ✔ ✌ Adapter Design Pattern – Adapt Vector to BookList interface – Implement BookList from scratch ✖✣✢ ✓✕✔ ✗✙✘ – Use Vector ✠☎✡ Options: ✂☎✄ •

  12. � ✒ ✠ ☞ ✝ ✤ ✞ ✠ ✞ ✌ ✝ ✔ ✟ ✔ ✗ ✁ ✒ � ✒ ✚ ✁ ✏ ✞ ✚ ✑ ✌ ✠ ✏ ✔ ✑ ✝ ☞ ✑ ✔ ✖ ✞ ✌ ✒ ✝ ✟ ✔ ✗ ✁ ✝ ✠ ✞ ✞ ☛ ✎✒ ✑ ✏ ✎ ✍ ✌ ☞ ✠ ✞ ☎ ✚✜ ✡ ✠ ✆ ✟ ✞ ✝ ☎✆ ☎ ✄ ✂ � ✒ ✓ ✞ ✘ ✄ ✞ ☎✆ ☎ ✄ ✛ ☞ ✘ ✚ ✗ ✖ ✖ ✕ ✔ ☞ Two ways of adapting the Vector ✖✣✢ ✗✙✘ – Object adapter Adapter – Class adapter •

  13. � ☎✆ ✕ ✖ ✖ ✗ ✚ ✞ ✘ ✛ ✄ ☎ ✞ ✂ ✄ ✘ ✚✜ ✚ ✠ ✞ ✚ ✒ � ✒ ✔ ☞ ☞ ☛ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ✞ ✓ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ � ✒ ✖✣✢ Class Adapter ✗✙✘

  14. � ☎✆ ✕ ✂ ✖ ✗ ✚ ✞ ✘ ✛ ✄ ☎ ✞ ☞ ✄ ✘ ✚✜ ✚ ✠ ✞ ✚ ✒ � ✒ ✔ ✖ ☞ ☛ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✓ ☎ ✡ ✞ ✏ ✒ � ✠ ✑ ✎✒ ✎ ✍ ✌ ☞ Class Adapter BookList.java public interface BookList { public int getNumBooks(); public Book getBook(int n); public void addBook(Book newBook); } ✖✣✢ ✗✙✘

  15. import java.util.Vector; public class BookListClassAdapter extends Vector implements BookList { public int getNumBooks() { return size(); } public Book getBook(int n) { BookListClassAdapter.java return (Book) elementAt(n); } public void addBook(Book newBook) { for (int i=0; i<getNumBooks(); i++) { Book b=getBook(i); if (b.isSameBook (newBook)) { b.setQuantity (b.getQuantity() + newBook.getQuantity()); return; } } add (newBook); } }

  16. � ☎ ✔ ✕ ✖ ✁ ✗ ✚ ✞ ✘ ✛ ✄ ☎✆ ☞ ✞ ✄ ✘ ✚✜ ✚ ✠ ✞ ✚ �✒ � ☞ ✖ ✓ ☎ ✂ ✄ ☎ ☎✆ ✝ ✞ � ✆ ✠ ✡ ✟ ☛ ✎ ✞ ✎ ✑ ✏ �✒ ✍ ✌ ☞ ✠ SampleClient.java // This code simply shows how a book list is instantiated public class SampleClient { public static void main (String [] args) { Book b = new Book (…); BookList bl = new BookListClassAdapter (); bl.addBook (b); System.out.println (“Book list has ” + bl.getNumBooks() + “ books”); } } ✖✣✢ ✗✙✘

  17. �✒ � ✄ ✆ ✄ ✆ ✁ � �✒ ✟ ✚ ✞ ✠ ✂ ✚✜ ✘ ✄ ✂ ✡ ☎✆ ✞ ★ ✥ ✤ ✢✣ ✙ ✝ ✡ ☛ ✠ ✞ ✂ ✒ ✑✄ ✌✍ ☞ ✞ ✚ ☎ ☛ ✑ ✏ ✎ ✍ ✌ ☞ ✠ ✞ ☎ �✒ ✡ ✠ ✆ ✟ ✞ ✝ ☎✆ ☎ ✄ ✄ ✎ � ✔ ✛ ✘ ✞ ✚ ✗ ✖ ✖ ✕ ✩ ☞ ✓ ☞ Class Adapter • Adapter simply inherits operations of Vector • Clients use BookList interface, not BookListClassAdapter directly ✓✏✔✕ ✚✜✛ ✦✏✧ ✖✘✗ ✟✏✎ ✂☎✄ ✝☎✞ ✠☎✄ ✖✣✢ ✗✙✘

  18. � ✄ ✓ ✔ ✕ ✕ ✖ ✙ ✝ ✗ ✚ ✂ ✄☎ � ✝ ✂ ✗ ✙✛ ✙ ✟ ✝ ✙ �✑ � ☛ ☛ ✒ ✄ ✁ ✂ ✄ ✄☎ ✆ ✝ ✞ ☎ ✟ ✠ ✡ � ✝ ✟ ☛ ☞ ✌ ✍ ✎ ✏ ✍ �✑ General Form of Class Adapter ✕✢✜ ✖✘✗

  19. � ✄ ✓ ✔ ✕ ✕ ✖ ✙ ✝ ✗ ✚ ✂ ✄☎ � ✝ ✂ ✗ ✙✛ ✙ ✟ ✝ ✙ �✑ � ☛ ☛ ✒ ✄ ✁ ✂ ✄ ✄☎ ✆ ✝ ✞ ☎ ✟ ✠ ✡ � ✝ ✟ ☛ ☞ ✌ ✍ ✎ ✏ ✍ �✑ ✕✢✜ Object Adapter ✖✘✗

  20. import java.util.Vector; public class BookListObjectAdapter implements BookList { Object Adapter private Vector books = new Vector(); public int getNumBooks() { BookListObjectAdapter.java return books.size(); } public Book getBook(int n) { return (Book) books.elementAt(n); } public void addBook(Book newBook) { for (int i=0; i<getNumBooks(); i++) { Book b=getBook(i); if (b.isSameBook (newBook)) { b.setQuantity (b.getQuantity() + newBook.getQuantity()); return; } } books.add (newBook); } }

  21. � ✄ ✔ ✕ ✕ ✁ ✙ ✝ ✗ ✚ ✂ ✄☎ ☛ ✝ ✂ ✗ ✙✛ ✙ ✟ ✝ ✙ �✑ � ✓ ✖ ☛ ✡ ✂ ✄ ✄☎ ✆ ✝ ✞ ✒ ✟ ✠ ✄ ☎ ✝ ✎ ✟ �✑ ✍ ✏ � ✍ ✌ ☞ ☛ SampleClient.java // This code simply shows how a book list is instantiated public class SampleClient { public static void main (String [] args) { Book b = new Book (…); BookList bl = new BookListObjectAdapter (); bl.addBook (b); System.out.println (“Book list has ” + bl.getNumBooks() + “ books”); } } ✕✢✜ ✖✘✗

  22. � ✄ ✓ ✔ ✕ ✕ ✖ ✙ ✝ ✗ ✚ ✂ ✄☎ � ✝ ✂ ✗ ✙✛ ✙ ✟ ✝ ✙ �✑ � ☛ ☛ ✒ ✄ ✁ ✂ ✄ ✄☎ ✆ ✝ ✞ ☎ ✟ ✠ ✡ � ✝ ✟ ☛ ☞ ✌ ✍ ✎ ✏ ✍ �✑ General Form of Object Adapter ✕✢✜ ✖✘✗

  23. � ✄ ✔ ✕ ✕ ✁ ✙ ✝ ✗ ✚ ✂ ✄☎ ☛ ✝ ✂ ✗ ✙✛ ✙ ✟ ✝ ✙ �✑ � ✓ ✖ ☛ ✡ ✂ ✄ ✄☎ ✆ ✝ ✞ ✒ ✟ ✠ ✄ ☎ ✝ ✎ ✟ �✑ ✍ ✏ � ✍ ✌ ☞ ☛ Relation to Quality Attributes • Modifiability – Adapter class provides clean interface to clients of adapted object • Reusability – Takes advantage of adapted class for reuse • Correctness – Takes advantage of tested code ✕✢✜ ✖✘✗

  24. �✁ ☎ ✕ ✖ ✂ ✗ ✚ ✞ ✘ ✛ ✄ ☎✆ ☞ ✞ ✄ ✘ ✚✜ ✚ ✠ ✞ ✚ �✒ � ✔ ✖ ☞ ☛ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✓ ✡ ☎ ✠ ✞ ✏ � ✠ ✎ ✑ �✒ ✎ ✍ ✌ ☞ The Façade Pattern [Gamma et al., pp 185-193] • Motivation – Sometimes part of a system is itself implemented as a subsystem, consisting of a set of objects – Rather than revealing subsystem structure to rest of system, use façade object to provide interface to subsystem as a whole ✖✣✢ ✗✙✘

  25. � ☎ ✁ ✕ ✖ ✖ ✗ ✚ ✞ ✘ ✛ ✄ ☎✆ ☞ ✞ ✄ ✘ ✚✜ ✚ ✠ ✞ ✚ �✒ � ☞ ✔ ✓ ☎ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ � ✡ ☛ ✞ �✒ ✎ ✑ ✏ ✎ ✍ ✌ ☞ ✠ Facade ✖✣✢ subsystem classes General Idea client classes ✗✙✘

  26. � ✏ ✌ ✎ ✌ ✕ ✎ ✠ ✍ ✞ ☞ ☞✌ ✍ ✎ ✏ ✑ ✛☞✌ ✝ ✞ ✖ ✜ ✝ ✞ ✙ ✑ ✌ ✚ ✎ ✎ ✝ ✛☎ ☛ ✂ ✍ ✞ ☞ ✑ ✞ ✗ ☞ ✑ ✌ ✒ ✏ ✖ ✛☎ ✘ ✎ ✠ ✍ ✞ ☞ ✞ ✙ ✑ ✌ ✚ ✎ ✍ ✍ ✝ ✕ ✕ ✚ ✎ ✍ ✛☎ ✝ ✑ ✎ ✌ ✎ ✌ ✏ ✎ ☎ ✑ ✝ ✑ ✎ ✌ ✎ ✚ ✕ ✎ ✠ ✍ ✞ ☞ ✌ ✙ ✑ ✠ ✎ ✌ ✎ ✌ ✞ ✝ ✠ ✝ ✞ ✢ ✕ ✎ ✍ ✞ ✞ ☞ ✞ ✖ ✣ ☎ ✕ ✑ ✏ ✑ ✝ ✑ ☞ ✎ ✠ ☞✌ ✚✜ ✗ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚ ✖ ✠ ✞ ✚ �✒ � ✁ ✂ ✞ ☎ ✍ ☞ ✖ ✕ ✍ ✞ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✠ ✔ ☞ ✌ ✍ ✎ ✏ ✑ ✎ �✒ � ✓ ☞ ☞ ☞✌ ✝ ✎ ✑ ✠ ✍ ✞ ☞ ✞ ☎ ✒ ☞✌ ✞ ✝ ✠ ✝ ✞ ✝ ☞✌ ✍ ✎ ☛ ✑ ✌ ✖ ✂ ✍ ☞ ☎ ✒ ✕ ✎ ✂ ✎ ☛ ✑ ✌ ☎ ✒ ☞✌ ✕ ✞ ✠ ✍ ✖ ☞ ✞ – A student information system provides access to The Façade Pattern ✏✆☞ – Implemented via three classes ✑✔✓ ✄✆☞ ☛✆☞✌ ✖✣✢ ☛✆☞✌ ✏✆☞ ✗✙✘ ✑✔✓ functions on ☛✆☞✌ ✑✔✓ ✝✡✠ ✝✟✞ Example ☛✆☞✌ ✄✆☎ ✄✆☎ ✄✆☞ ✝✡✠ •

  27. � ☎✆ ✕ ✖ ✖ ✗ ✚ ✁ ✘ ✛ ✄ ☎ ✞ ☞ ✄ ✘ ✚✜ ✚ ✠ ✞ ✚ ✒ ✁ ✒ ✔ ✞ ☞ ☛ ✂ ✄ ☎ ☎✆ ✓ ✞ ✟ ✆ ✠ ✡ ☎ ✝ ✞ ☞ ✌ ✍ ✠ ✎ ✏ ✑ ✎✒ ✁ ✒ Student Information System Classes class Courses { Courses.java … public StudentList getEnrollment (CourseId c) { … } … } class Students { Students.java … public CourseList getCourses (StudentId s) { … } } class Facilities { Facilities.java … public LectureHallId getLectureHall (CourseId c) { … } } ✖✣✢ ✗✙✘

  28. � ☎✆ ✕ ✖ ✖ ✗ ✚ ✞ ✘ ✛ ✄ ☎ ✞ ☞ ✄ ✘ ✚✜ ✚ ✠ ✞ ✚ ✒ ✁ ✒ ✒ ✔ ☞ ☛ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ✓ ☎ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✁ ✒ Student Information System without Façade Student Information Subsystem ✖✣✢ ✗✙✘

  29. � ☎✆ ✕ ✖ ✖ ✗ ✚ ✞ ✘ ✛ ✄ ☎ ✞ ☞ ✄ ✘ ✚✜ ✚ ✠ ✞ ✚ ✒ ✁ ✒ � ✔ ☞ ☛ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ✓ ☎ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✁ ✒ Student Information System with Façade Student Information Subsystem ✖✣✢ ✗✙✘

  30. � ☎✆ ✕ ✖ ✖ ✗ ✚ ✂ ✘ ✛ ✄ ☎ ✞ ☞ ✄ ✘ ✚✜ ✚ ✠ ✞ ✚ ✒ ✁ ✒ ✔ ✞ ☞ ☛ ✄ ☎ ☎✆ ✝ ✓ ✟ ✆ ✠ ✡ ☎ ✞ ✞ ✒ ☞ ✌ ✍ ✠ ✎ ✏ ✑ ✎✒ ✁ Façade Implementation StudentInformationSystem.java public class StudentInformation System { Courses cs; Students ss; Facilities fs; … public StudentList getEnrollment (CourseId c) { return cs.getEnrollment (c); } public CourseList getCourses (StudentId s) { return ss.getCourses (s); } public LectureHallId getLectureHall (CourseId c) { return fs.getLectureHall (c); } } ✖✣✢ ✗✙✘

  31. � ✎ ✓ ✆ ✓ ✕ ✆ ✔ ✞✓ ✠ ✒ ✠ ✑ ✞ ✠ ✏ ✄ ✟ ✞ ✞ ✟ ✌ ✆ ✞ ✍ ✞ ✌ ✟ ☞ ✆ ☛ ✄ ✆ ✠ � ✜ ✘ ✘ ✠ ✎ ✠ ✡ ✆ ✢ ✌ ✌ ✞ ✌ ✏ ✆ ✍ ✍ ✓ ✄ ☛ ✡ ✓ ✘ ✘✙ ✗ ✠ ✆ ☞ ✟ ✌ ✠ ✛ ✡ ✆ ✡ ✍ ✗ ✖ ✖ ✕ ✔ ☞ ☞ ✓ ✒ ✁ ✎✒ ✑ ✏ ✎ ✌ ✠ ✟ ✂ ✄ ☎ ☎✆ ✝ ✞ ✆ ☞ ✠ ✡ ☎ ☛ ✞ ✠ ✚ ✞ ✘ ✠ ✞✟ ✝ ✆ ✁ ✒ ✛ ✒ ✚ ✞ ✁ ✚ ✞ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ Relation to Quality Attributes • Modifiability – By removing external links to subsystem components, impact of changes to subsystem components limited to subsystem itself • Performance – Slight performance cost of extra call indirection through façade ✂☎✄ ✖☎✗ ☞☎✚ ✖✣✢ ✗✙✘

  32. �✁ ✪ ❑ ✮ ◆ ✧ ✦ P ✰ ✱ ✷ ✰ ✳ ✬ ❖ ✬ ✫ ◆ ✭ ❑ ✥ ✬ ✱ ▼ P ✰ ✱ ✪ ✷ ✳ ✬ ❖ ✪ ✱ ✰ ✯ ❑ ✶ ✬ ❑ ✭ P ✧ ▲ ✰ ❋ ✾ ❀ ❁ ❊ ✿ ✼ ❈ ❃ ✼ ✽ ✾ ✽ ✾ ❀ ❀ ❈ ✿ ❀ ✽ ❁ ❑ ✬ ✭ ✮ ❏ ✮ ✪ ✳ ✯ ✦ ✲ ✽ ✧ ✱ ✭ ✦ ✮ ✧ ✱ ✥ ❊ ✥ ◆ ❊ ✥ ✦ ✶ ✬ ✫ ✯ ❑ ✪ ✧ ✬ ✫ ✪✭ ◆ ✰ ✮ ◆ ✭ ✦ ❙ ✷ ✬ ✰ ✬✭ ✬ P ✮ ✫ ✲ ❑ ✪ ✧ ✰ ✸ ✮ ❑ ✱ ✬ ✯ ✳ ✧ ✱ ▼ ❑ ✪ ✰ ✪ ✭ ✱ ✲ ✧ ✦ ✭ ❑ ✰ ✷ ✪ ✱ ✰ ✪ ✭ ❑ ◆ ▼ ✪ ✬ ✰ ✯ ✬ ✬ ✭ ✮ ✪ ❑ ✳ ✭ ✱ ✳ ✬ ✰ ✬ ✩ ✥ ✭ ✰ ✪ ✰ ✰ ✂ ❁ ✽ ✬✭ ✩ ✬ ✭ ✯ ✪ ✬ ✭ ✮ ✩ ✰ ✫ ✪ ✩ ✥ ✒ ✓ ✒ ✛ ✬ ✱ ✠ ✰ ✫ ✾ ✰ ✬ ✧ ✬ ✴ ✳ ✬ ✫ ✩ ✥ ✬✭ ✭ ✭ ✱ ✫ ✲ ✞ ✛ ✯ ☎ ✏ ✎ ✍ ✌ ☞ ✠ ✞ ☛ ✡ ✎✒ ✠ ✆ ✟ ✞ ✝ ☎✆ ☎ ✄ ✑ ✓ ✛✢ ✞ ✙ ✄ ✞ ☎✆ ☎ ✄ ✜ ✙ ✛ ✒ ✘ ✗ ✗ ✖ ✕ ☞ ☞ ✔ ✵ ✱ ✫ ✺✻ ❍ ✾ ❋ ✿ ✻ ✾ ✽ ✼ ✹ ❁ ❂ ✻ ✾ ✽ ❋ ❊ ✽ ✾ ● ❋ ✽ ❁ ✼ ❊ ■ ❊ ✾ ❈ ❀ ❈ ❋ ❁ ✹ ❅ ✻ ❋ ✻ ✿ ❄ ✽ ✻ ✼ ❈ ✬ ❋ ✽ ✾ ❄ ✾ ❃ ❂ ❁ ✽ ✿❀ ✻ ✽ ❁ ✼ ✺✻ ✹ ✸ ✬ ✫ ✷ ✦ ✶ ❁ ❅ ✻ ❁ ❆ ❃ ❊ ✽ ✿ ✼ ❈ ✾ ❀ P ❈ ❉ Another Example: SavitchIn ✗✤✣ ✦★✧ ✦★▼ ✿❇❆ ✘✚✙ Input with Java API: SavitchIn: ✬❘◗ ✦★✧ • •

  33. � ✛ ✧ ✦ ✥ ✒ ✓ ✒ ✞ ✪ ✠ ✛ ✁ ✛✢ ✙ ✄ ✞ ★✩ ✫ ☎ ✲✳ ✩ ✦ ✶ ✵ ✴ ✰ ✰ ✫ ✬✭ ✪ ✪ ✯ ✫✮ ★ ✧ ✫ ☎✆ ✰ ✄ ✠ ✓ ✎✒ ✑ ✏ ✎ ✍ ✌ ☞ ✞ ✜ ☛ ☎ ✄ ✡ ✠ ✆ ✟ ✞ ✝ ✒ ☎✆ ☎ ☞ ✙ ✞ ✛ ✂ ✘ ✗ ✗ ✖ ✕ ☞ ✔ Categories of Patterns • The patterns we have seen so far address structural problems in programs – Adapter -- structures programs to aid reuse – Façade -- localizes references to a subsystem, aiding modifiability • Also interesting are – Creational patterns -- help in object creation – Behavioural patterns -- help manage runtime behaviour of program ★✱✰ ✗✤✣ ✘✚✙

  34. � ✽ ✺ ✴ ✻ ✥ ✧ ✩ ✪ ✹ ✫ ✰ ✪ ✫ ✬ ✮ ✦ ✸ ✫ ✰ ✁ ✪ ✯✱ ✬ ✰ ✪ ✴ ✴ ✬ ✵ ✰ ✶ ✧ ✷ ✪ ✰ ✬ ✲ ✰ ✪ ❇ ✾ ✧ ✷ ✬ ✬ ✭ ✶ ✿ ✧ ✧ ✩ ✯ ✵ ✧ ✪ ✾✿ ✧ ✧ ✩ ✫ ✴ ✯ ❁ ✭ ✮ ✵ ❀ ✪ ✯✱ ✬ ✪ ✯ ✰ ☞ ✑ ✎✒ ✓ ✒ ✔ ☞ ✕ ✎ ✖ ✗ ✗ ✘ ✛ ✯ ✏ ✍ ✜ ✟ ✂ ✄ ☎ ☎✆ ✝ ✞ ✆ ✌ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✙ ✞ ✄ ✒ ✩ ✧ ✬ ✭ ✮ ✥ ✬ ☎ ✒ ✫ ✓ ✛ ✫ ☎✆ ✞ ✄ ✭✧ ✙ ✛✢ ✛ ✫ ✠ ✞ ✪ Abstract Factory [Gamma et al., pp. 87-95] • Motivation – Imagine you have a book ordering system in which you need to maintain a list of books ✮✳✲ ✦★✧ ✰❃❂ ❄❆❅ ✼✦★✧ – Initially, you wish to maintain a simple Vector – However, after the system has been delivered, you decide you want a data structure that also permits books to be represented in sorted order ✗✤✣ ✘✚✙

  35. import java.util.Vector; public class BookListClassAdapter extends Vector implements BookList { public int getNumBooks() { return size(); } Here is an initial implementation of a public Book getBook(int n) { BookList return (Book) elementAt(n); } public void addBook(Book newBook) { for (int i=0; i<getNumBooks(); i++) { Book b=getBook(i); if (b.isSameBook (newBook)) { b.setQuantity (b.getQuantity() + newBook.getQuantity()); return; } } add (newBook); } }

  36. �✁ ❀ ✱ ✰ ✬ ❀ ❆ ✶ ✬ ✱ ✴ ✳ ✳ ❄ ✻ ✬ ❀ ✳ ✶✷ ✯ ❊ ✪ ✻ ❀ ✳ ✱ ✳ ❄ ✸ ✶ ✳ ❀ ✳ ✻ ✻ ❅ ✯ ✳ ❀ ✳ ✵ ❉ ❇ ✶ ✬ ✱ ✸ ❅ ✶ ✴ ✱ ✬ ✮ ❂ ✸ ✱ ✰ ✯ ✶ ✬ ✸ ✰ ✮ ✶ ✿ ✻ ✴ ✴ ✯ ✰ ✮ ✶ ✿ ❅ ✶ ✯ ✯ ✰ ❆ ✶ ✯ ✵ ✸ ✸ ✮ ❆ ✵ ❁ ✱ ❀ ✳ ❅ ✱ ❉ ✳ ❄ ✯ ❀ ✱ ✳ ❁ ✯ ✳ ❀ ✮ ✶ ✱ ❋ ❋ ✴ ✯ ✳ ❀ ✷ ✬ ❀ ❆ ✸ ✶ ✳ ❀ ✳ ✴ ✴ ✻ ❁ ✮ ✶ ✯ ✶ ✳ ✸ ✶ ✱ ✯ ✵ ❆ ✸ ✱ ❉ ✴ ❁ ✳ ✰ ❃ ✬ ❀ ✻ ✱ ✬ ✸ ✳ ✻ ✻ ✱ ❅ ✵ ✸ ✱ ❉ ✴ ✳ ✱ ✫ ✺ ✬ ✬ ✹ ✻ ✬ ✴ ✶ ❀ ✶ ❅ ✸ ✶ ✱ ❀ ✯ ✳ ❆ ❆ ✯ ❂ ✱ ✸ ✰ ✯ ✶ ✬ ✱ ✸ ✮ ✶ ✿ ✻ ✱ ✶✷ ✂ ✯ ✺ ✬ ✬ ✹ ✬ ✸ ✳ ✶✷ ✵ ✱ ✮ ✳✴ ✲ ✱ ✰ ✮✯ ✪ ★✩ ✥ ✫ ✴ ✓ ✥ ✳ ✯ ✿❀ ✬ ✾ ✪ ✩ ★✽ ✼ ✰✻ ✸ ✳ ✴ ✸ ✱ ✴ ✴ ✯ ✰ ✮ ✒ ✒ ✬ ☛ ✎✒ ✑ ✏ ✎ ✍ ✌ ☞ ✠ ✞ ☎ ✒ ✡ ✠ ✆ ✟ ✞ ✝ ☎✆ ☎ ✄ ✓ ✔ ✛ ✄ ✞ ✠ ✛ ✛✢ ✙ ✄ ✞ ☎✆ ☎ ✜ ☞ ✙ ✞ ✛ ✘ ✗ ✗ ✖ ✕ ☞ ✸ ✮ ✹ ✵ ✸ ✴ ❂ ✴ ❃ ✿ ✴ ❀ ✳ ✸ ❁ ✬ ✯ ❀ ✷ ✬ ❀ ❆ ❀ ✳ ✵ ✳ ✴ ✬ ❁ ✵ ✮ ✸ ✯ ✵ ✸ ✬ ✴ ❇ ✯ ✬ ❀ ✷ ✬ ❀ ❆ ✳ ❁ ✯ ✴ ✻ ✸ ✳ ✬ ✯ ✺ ✬ ✯ ✹ ✪ ✳ ✰ ❃ ✰ ✱ ✱ ✯ ❄ ✯ ✳ ❃ ✸ ✬ ✫ ❁ ❂ ✴ ✬ ✺ ✬ ✫ ✶ ✱ ✱ ❅ ✳ ✴ ✴ ✿ ✸ ✳ ❃ ❂ ✯ ❁ ✸ ✶ Adding sorting behavior to the BookList class – Option 1: Modify BookList so that every time a book is entered, the Vector is sorted ❁✴❈❇ Abstract Factory ✗✤✣ ✘✚✙ ✫✭✬ ✦✤✧ •

  37. � ☎✆ ✕ ✁ ✖ ✗ ✚ ✞ ✘ ✛ ✄ ☎ ✞ ☞ ✄ ✘ ✚✜ ✚ ✠ ✞ ✚ ✒ ✁ ✒ ✔ ✖ ☞ ☛ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✓ ✡ ☎ ✠ ✞ ✏ ✒ ✁ ✠ ✑ ✎✒ ✎ ✍ ✌ ☞ Abstract Factory • Option 2: – Create a BookList interface with the required functionality of maintaining a list of books – Create an abstract factory class that creates an instance of the correct kind of BookList ✖✣✢ ✗✙✘

  38. � ✢ ☞ ✧ ☞ ✡ ☛ ✏ ☛ ✠ ✙ ✡ ✩ ✔ ✂ ✦ ✟ ✝ ✪✫ ✥ ✟ ✂ ✦ ✂ ☎ ✚ ✧ ✞ ☞ ✦ ✒ ☎ ✆ ✝ ✞ ✟ ✠ ✓ ☛ ✕ ✓ ✠ ✠ ✧ ✔ ✚ ☞ ★ ☞ ✒ ☛ ✙ ✍ ✆✝ ✟ ✟ ✓ ☎ ✆✝ ✞ ✟ ✔ ✍ ✙ ★ ✠ ✘ ☞ ✓ ✤ ✍ ✚ ✙ ✙ ✜ ✗ ✓ ✙ ✍ ☛ ✬ ✂ ✤ ✏ ✒ ✘ ✘ ✓ ✓ ✙ ✍ ☛ ✬ ✎ ✗ ✡ ☛ ✦ ✠ ✙ ✡ ✏ ✚ ✠ ✢ ✯ ✣ ✆ ✰ ✂ ✂ ✥ ✞ ✖ ✖ ✗ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✄ ✔ ✘ ✚✜ ✚ ✠ ✞ ✚ ✒ ✁ ✒ ✂ ☎ ✕ ☞ ✞ ☛ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ✞ ☞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✁ ✒ ✓ ✆✝ � ✓ ✙ ✟ ✘ ✙✔ ☞ ✍ ✏ ☛ ✠ ✙ ✡ ✓ ✎ ✍ ✠ ✏ ✚ ✠ ✓ ☛ ✙ ✎✛ ✙ ✙ ✜ ✓ ✢ ✍ ☞ ✗ ✒ ✍ ✓✔ ✖ ☞ ✒ ✠ ✎ ✠ ☞ ✓ ☛ ✕ ☞ ✍ ☞ ☞ ✖✣✢ BookList variants ✗✙✘ ☛✮✭ ✠✄✡ ✁✄✂ ✎✑✏ ✔✓✲✱ ☛✌☞ ✁✄✂ ✠✄✡ ✣✄✤ ✁✄✂ ✠✄✡ ☛✌☞ ✣✄✤ ✁✄✂ ✎✑✏

  39. �✁ ☎✆ ✖ ✗ ✗ ✘ ✂ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✛ ☞ ☛ ✄ ☎ ☎✆ ✝ ✞ ✔ ✆ ✠ ✡ ☎ ✟ ✞ ✒ ☞ ✌ ✍ ✠ ✎ ✏ ✑ ✎✒ ✓ Problems with Changing Implementations • Problem: If we change from the VectorBookList to SortingVectorBookList implementation, need to ensure that wherever we create a book list, we replace “new VectorBookList() ” with “ new SortingVectorBookList() ” • This could be a large change – What if source to classes creating book lists is not available? ✗✤✣ ✘✚✙

  40. � ☎✆ ✖ ✗ ✗ ✘ ✛ ✞ ✙ ✜ ✄ ☎ ✞ � ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ☞ ☞ ✔ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✗✤✣ Abstract Factory ✘✚✙

  41. � ✡ ✌ ✫ ☎ ☛ ✠ ☞ ☎ ✠ ✱ ✓ ✓ ☞ ✥✭ ✗ ✜ ✍ ✕ ✧ ✠ ✟ ✕ ✍ ☎ ☛ ✎ ✱ ✠ ✠ ✡ ✰ ✟ ✍ ✌ ✡ ✤ ✬ ✟ ✟ ✞ ✠ ✓ ✝ ✟ ☎ ✌ ☛ ✠ ✄ ✌ � ✡ ☎ ✏ ✠ ✯ ✜ ✜ ✛ ✙✚ ✘ ✗ ✮ ✁ ✛ ☎ ✙✚ ✘ ✗ ☎ ✱ ✕ ✍ ☎ ✝ ✍ ☎ ✝ ☞ ✓ ☎ ✌ ✠ ✟ ✌ ✡ ✥✭ ✗ ✜ ✤ ✧ ✬ ✓ ✠ ✛ ✜ ✛ ✙✚ ✘ ✗ ✥ ✗ ✤ ✝ ✦ ✷ ✕ ✍ ✓ ☎ ✡ ✜ ✙✚ ☞ ✡ ✍ ✫ ✝ ☞ ✵ ✌ ☎ ✗ ✟ ✟ ✍ ✡ ✌ ✴ ✝ ✕ ✘ ✘ ✠ ✗ ✥ ✗ ✜ ✤ ✦ ✷ ✢ ✙✚ ☎ ✟ ☎ ✶ ✁ ✜ ✛ ✎ ✌ ✝ ✒ ✞ ☎ ✂ ✁ ✒ ✓ ✛ ✟ ✞ ✠ ✛ ✛✢ ✙ ✄ ✞ ☎ ✠✡ ☎ ✓ ☎ ✟ ✠ ✡ ✍ ✌ ✡ ☛ ☎ ✏ ✎ ✍ ✌ ☎✝ ☞ ☎✆ ✄ ✌ ✠ ☞ ✠ ✞ ☛ ☎ ✡ ✆ ✍ ✟ ✞ ✝ ☎✆ ☎ ✄ ✂ ✌ ✎ ✜ ✖ ✙ ✞ ✛ ✘ ✗ ✗ ✕ ✏ ☞ ☞ ✔ ✒ ✓ ✎✒ ✑ ✠ ✜ ☎ ☎ ✤ ✦✧ ✜ ✦ ✠ ✗ ✘ ✙✚ ✛ ✥✭ ✗ ✜ ★✩ ✪ ✌ ✌ ✜ ✬ ✜ ✛ ✙✚ ✘ ✗ ☎ ✄ ✄ ✌ ✕ ✍ ✫ ✍ ✤ ✥ ✓ ✘ ✝ ☞ ✝ ✓ ✌ ✠ ✝ ✡ ☎ ✍ ✕ ✠ ✗ ✄ ✙✚ ✛ ☛ ☛ ✠ ✁ ✡ ✌ ✧ ☞ ✮ ✗✤✣ Abstract Factory ✄✆☎ ✘✚✙ ☞✔✓ ✌✒✑ ✌✒✯ ✄✆☎ ✖✔✗ ✖✔✗ ✄✆☎ ✌✒✰ ✜✣✢ ✖✔✗ ✖✔✗ ☞✳✲ ✖✔✗ ✌✒✯ ☞✔✓ ✄✆☎✝ ☞✔✓ ✄✆☎ ✖✔✗ ✖✔✗ ✖✔✗

  42. BookListFactory BookListFactory.java public interface BookListFactory { public BookList createBookList (); } class VectorBookListFactory implements BookListFactory { public BookList createBookList () { return new VectorBookList (); } VectorBookListFactory.java } class Client { Client.java … BookListFactory blf; … BookList b = blf.createBookList(); }

  43. � ☎✆ ✖ ✗ ✗ ✘ ✛ ✁ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✞ ☞ ☛ ✂ ✄ ☎ ☎✆ ✔ ✞ ✟ ✆ ✠ ✡ ☎ ✝ ✞ ✏ ✠ ✒ ✎✒ ✑ ✓ ✎ ✍ ✌ ☞ Extending to Multiple Factories uses BookListFactory Client uses <<interface>> VectorBookListFactory SortingVectorBookListFactory BookList creates creates VectorBookList SortingVectorBookList The client is provided with an object of type BookList . This object may be either a VectorBookList or a SortingVectorBookList , depending on which factory was used. ✗✤✣ ✘✚✙

  44. � ☎✆ ✖ ✗ ✗ ✘ ✛ � ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✞ ☞ ☛ ✂ ✄ ☎ ☎✆ ✔ ✞ ✟ ✆ ✠ ✡ ☎ ✝ ✞ ✏ ✠ ✒ ✎✒ ✑ ✓ ✎ ✍ ✌ ☞ Extending to Multiple Factories uses BookListFactory Client uses <<interface>> VectorBookListFactory SortingVectorBookListFactory BookList creates creates VectorBookList SortingVectorBookList The client is provided with an object of type BookList . This object may be either a VectorBookList or a SortingVectorBookList , depending on which factory was used. ✗✤✣ ✘✚✙

  45. �✁ ☎✆ ✖ ✗ ✗ ✘ ✛ ✞ ✂ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✙ ☞ ☛ ✄ ☎ ☎✆ ✔ ✞ ✟ ✆ ✠ ✡ ☎ ✝ ✞ ✏ ✠ ✒ ✎✒ ✑ ✓ ✎ ✍ ✌ ☞ Extending to Multiple Factories uses BookListFactory Client uses <<interface>> VectorBookListFactory SortingVectorBookListFactory BookList creates creates VectorBookList SortingVectorBookList The client sees an object of type BookListFactory , so does not need to know whether it is using a VectorBookListFactory or a SortingVectorBookListFactory ✗✤✣ ✘✚✙

  46. General Form of Abstract Factory

  47. � ☎✆ ✖ ✓ ✗ ✘ ✛ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✗ ☞ ☛ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✔ ✡ ☎ ✠ ✞ ✏ ✒ ✓ ✠ ✑ ✎✒ ✎ ✍ ✌ ☞ Relating to Quality Attributes • Modifiability – Increased through use of indirection in creation process – Creator does not need to know exactly what type of object is being created ✗✤✣ ✘✚✙

  48. �✁ ☎✆ ✕ ✖ ✖ ✂ ✚ ✞ ✘ ✛ ✄ ☎ ✞ ☞ ✄ ✘ ✚✜ ✚ ✠ ✞ ✚ ✁ ✒ ✁ ✔ ✗ ☞ ✞ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✓ ✠ ☎ ☛ ✡ ✠ ✑ ✁ ✒ ☞ ✎ ✁ ✏ ✎ ✍ ✌ The Flyweight Pattern [Gamma et al., pp 195-206] • Sometimes objects too expensive for applications where large numbers of objects required • Flyweight objects provide cheaper object mechanism • Illustrates abstract factory pattern ✖✣✢ ✗✙✘

  49. � ☎ ✔ ✕ ✖ ✖ ✗ ✚ ✞ ✘ ✛ � ☎✆ ☞ ✞ ✄ ✘ ✚✜ ✚ ✠ ✞ ✚ ✁ ✒ ✁ ☞ ✄ ✓ ☛ ✁ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ✂ ✞ ✑ ✒ ✁ ✎ ✏ ✎ ✠ ✍ ✌ ☞ Flyweight • In a text editor, wish to represent a set of rows, and within each row a set of columns • Each row in turn contains a set of characters • Logically, each character is a p p a r e n t an object • However, a document of tens of thousands of characters would require tens of thousands of objects ✖✣✢ ✗✙✘

  50. � ✎ ✗ ✗ ✖ ✄☎ ✆ ✟ ✄ ☞ ✝ ☎ ✍ ✕ ✍ ✏ ✆ ✝ ☞ ✆ ☎ ✤ ★ ✦✩ ✪ ✮ ✬ ✪ ✯ ✰ ✟ ☎ ✱✬ ✄ ✆ ☎ ☞ ✟ ✄ ☞ ✍ ✄ ✆ ✟ ✖ ✄ ✍ ✗ ✛ ✘ ✠ � ✆ ✟ ☎ ✒ ✟ ✌ ✝ ✠ ✟✣ ✍ ✄ ✮ ✲ ✝ ✍ ☎ ✝ ✠ ✟☎ ✟ ✑ ✟ ✌ ✟✣ ✝ ✌ ✆ ✍ ✑ ✑ ✝ ✌ ✟ ✆ ✖ ✍ ✄☎ ✍ ✄ ✚ ✝ ✒ ✆ ✟☎ ✝ ✰ ✱ ✩ ✯ ✱✶ ✯ ✷ ✶ ✸ ✹ ✯ ✱ ✩ ✻ ✶ ✯ ✎ ✓ ✆ ✌ ✖ ✄☎ ✖ ☞ ☎ ✆ ✟ ✣ ✽ ☎ ✆ ☞ ✟ ✏ ✠ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✚ ✎ ✖ ✚ ✁ ✒ ✁ ✁ ✂ ✄☎ ✟ ☞ ✌ ✝ ✟ ✍ ✗ ✖ ✝ ✠ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ☞ ✕ ✌ ✍ ✎ ✏ ✑ ✎ ✁ ✒ ✁ ✓ ☞ ☞ ✔ ✄ ✞ ✍ ☎ ✒ ✍ ✕ ✆ ✝ ✒ ✝ ☎ ✏ ✝ ✖ ☞ ✟ ✟ ✗ ✗ ✝ ✠ ✟ ✎ ✚ ✝ ✎ ✍ ☞ ✔ ✆ ✝ ✏ ✍ ✆ ✝ ☞ ✆ ✒ ✝ ✚ ✑ ✘ ✌ ✓ ☞ ✗ ✢ ✒ ✟ ✌ ✟ ☞ ✓ ✌ ✆ ☞ ✒ y z m t x l n w k e v i h u r g t a s f p e r p d q c p a ✖✣✢ b o a n ✗✙✘ ✳✵✴ Flyweight ✆✞✝ ✖✜✛ ✕✗✙✘ ✆✞✝ ✆✞✝ ✹✺✶ ✆✞✝ ✫✭✬ ✠☛✡ ✖✜✛ ✖✜✛ ✥✧✦ ✆✞✝ ✔✭✼ ✕✗✙✘

  51. � ✲✬ ✯ ✺ ✹ ✷ ✲✷ ✯ ✩ ✰ � ✮ ✷ ✪ ✬ ✮ ✪ ✦✩ ★ ✤ ✑ ✒ ✺ ✲ ✚ ✺ ✯ ✲ ✷ ✻ ✩ ✲ ✷ ✺ ✯ ✹ ✩ ✷ ✸ ✦✩ ★ ✤ ✯ ✲ ✷ ✻ ✑ ✳ ✝ ✟ ☛ ✓ ✑ ✒ ✍✑ ✏ ✎ ✍ ✌ ☞ ✟ ✝ ✔ ✡ ✄ ✠ ✟ ☎ ✞ ✝ ✆ ✄☎ ✄ ✂ ✁ ☛ ☛ ✕ ✚ ✖ ✖ ✘ ✗ ✂ ✚ ✝ ✘ ✝ ✛ ✄☎ ✄ ✚✜ ✂ Flyweight • Intrinsic state: data that can be shared by many instances of object ✫✭✬ ✴✶✵ ✥✧✦ ✯✱✰ ✯✱✸ • Extrinsic state: specific to a particular instance a p p a r e n t ✥✧✦ a b c d e f g h i l m k n o p q r s t u v x y z w ✖✣✢ ✗✙✘

  52. � ☎✆ ✖ ✁ ✗ ✘ ✛ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✗ ☞ ☛ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✔ ✡ ☎ ✠ ✞ ✏ ✒ ✓ ✠ ✑ ✎✒ ✎ ✍ ✌ ☞ Flyweight Implementation create (intrinsicData) • Client creates flyweight instances using flyweightFactory • Factory requires intrinsic data as parameter -- e.g., parameter is “a” to create instance of character “a” • Factory returns flyweight object with this intrinsic data • Client must maintain extrinsic data -- e.g., character font, position ✗✤✣ ✘✚✙

  53. � ☎✆ ✖ ✗ ✗ ✂ ✛ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✘ ☞ ☛ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✔ ✠ ☎ ✡ ✞ ✏ ✒ ✓ ✠ ✑ ✎✒ ✎ ✍ ✌ ☞ Relation to Quality Attributes • Performance – Systems with tens/hundreds of thousands or more of objects can be prohibitively expensive in memory use – Flyweight objects reduce this expense • Modifiability – Encapsulation of system functionality no longer as clear – Separation of intrinsic/extrinsic state into different objects ✗✤✣ ✘✚✙

  54. CISC 323 Intro to Software Engineering Lecture 6-3 Composite, Iterator, and Builder Design Patterns

  55. � ☎✆ ✖ ✁ ✗ ✘ ✛ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✗ ☞ ☛ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✔ ✡ ☎ ✠ ✞ ✏ ✒ ✓ ✠ ✑ ✎✒ ✎ ✍ ✌ ☞ Composite Design Pattern [Gamma et. al. pp.163-173] • Intent – To compose object into tree structures that represent part-whole hierarchies – Lets clients treat individual objects and compositions of objects uniformly ✗✤✣ ✘✚✙

  56. � ✓ ✞ ✖ ✝ ✌✑ ✆ ✘ ✓ ☎ ✠ ✝ ✓ ✍ ✡ ✗ ☎ ✌ ☎ ✆✝ ✌ ✟ ☎ ✍ ✟ ☎ ✑ ✡ ✆ ☎ ✓ ✌ ✟ ✠✡ ✕ ✑ ✆ ✗ ✤ ✔ ✌ ✍ ✆ ✝ ✓ ✆ ✡ ✆ ✑ ✖ ✓ ✠ ✆ ✗ ☎ ✌ ☎ ✆ ☎ ✍ ✆ ✌ ✏ ✆ ✂ ☎ ✆ ✠ ✆✝ ✓ ✆ ✞ ✍ ☎ ✑ ✗ ☎ ✌ ✌ ✑ ☛ ☎ ✞ ✞ ✑ ✌ ✒ ✑ ✝ ✢ ✔ ✑ ☎ ✍ ☎ ☎ ✓ ✗ ☎ ✆ ✧ ✑ ✠ ✟ ✆ ✖ ✘ ✑ ✗ ☎ ✦ ✝ ✟ ✖ ✌ ✌ ✓ ✝ ✭ ✴ ✱ ✸ ✶ ✵ ✮ ✱ ✰ ✭ ✩ ✌ ★ ✝ ✟ ✓ ☎ ✞ ✝ ✓ ☎ ☎ ✆ ✝ ✟ ✞ ✆ ✍ ✆ ✌ ✏ ✟ ☎ ✥ ✗ ✍ ✌ ✆ ✒ ✓ ✍ ✞ ✌ ✆ ✖ ✏ ✌ ✓ ✌✟ ☛ ✞ ✆ ✗ ☎ ☎ ✑ ✗ ☎ ✝ ✆ ✟ ✓ ☎ ✓ ✌ ✟ ✠✡ ✟ ✞ ✓ ✗ ✆ ✌ ✍ ✌✑ ✆ ✍ ✏ ☎ ✞ ✏ ☞ ✒ ✆ ☛ ✠✡ ✞✟ ✆✝ ☎ ✄ ✂ ✁ ✆ ✏ ✓ ☛ ✌ ☎ ✆✝ ✝ ✗ ✠✡ ✟ ✞ ✆ ✠✡ ✓ ✓ ✌ ✄ ✟ ☎ ✏ ✟ ☎ ☛ ✒ ✒ ✕ ☛ ✎✒ ✑ ✏ ✎ ✍ ✌ ☞ ✠ ✞ ☎ ✒ ✡ ✠ ✆ ✟ ✞ ✝ ☎✆ ☎ ✄ ✓ ✔ ✛ ✄ ✞ ✠ ✛ ✛✢ ✙ ✄ ✞ ☎✆ ☎ ✜ ☞ ✙ ✞ ✛ ✘ ✗ ✗ ✖ ✕ ☞ ✔ ✟ ✟ ✑ ✆ ✚ ✒ ✟ ✍ ✆ ✝ ✓ ☎ ☎ ✝ ✟ ✞ ✖ ✝ ✖ ✌ ☎ ✞ ✌ ✆ ✓ ☎ ✄ ✟ ☎ ✌ ✟ ✠ ✄ ✆✝ ✖ ✘ ✆ ☎ ✆✝ ✍ ✆ ✄ ✄ ✓ ✞ ✑ ✚ ✆ ✒ ✍ ✏ ☎ ✞ ✏ ✌ ✆ ✌ ✗ ✠ ☎ ✌ ✆ ✌ ✏ ☎ ✑ ✗ ☎ ✆ ✌ ✆ ✏ ☎ ✠ ✍ ✡ ✓ ☎ ✓ ✑ ✆ ✌ ✆ ✟ ✓✙✘ ✓✙✘ Solution: Use the composite design pattern ☎✎✍ ✒✣✟ ✗✤✣ ☛✜✛ ✶✺✹ ✮✎✷ ☛✜✛ ☎✎✍ ✘✚✙ Motivation ✪✙✷ ☎✎✍ ✮✎✴ ✲✬✳ ✮✎✯ Problem: ☎✎✍ ✪✬✫ ☎✎✍ • •

  57. � ☎✆ ✖ ✗ ✁ ✘ ✛ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✗ ☞ ☛ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✔ ✠ ✡ ☎ ✆ ✞ ✏ ✒ ✠ ✎✒ ✑ ✓ ✎ ✍ ✌ ☞ Applicability • Use the Composite pattern if… – You want to represent part-whole hierarchies of objects. – You want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly. ✗✤✣ ✘✚✙

  58. � ✜ ✖ ✖ ✘ ✍✩ ★ ✧ ✣ ✣ ✢ ✚ ✜ ✖ ✢ ✔ ✢ ✜ ✛ ✙✚ ✘ ✓ ✗ ✖ ✔ ✒ ✏ ✒✓ ✍ ✏ ★ ✌ ★ ✮ ★ ✱ ✏ ✚ ✢ ✛ ✍✰ ★ ✒✕ ✢ ✥ ✍✯ ✮ ✍✪ ★ ✭ ✏ ✙ ✬ ✜ ✎ ✍ ★ ✚ ✕ ✙ ✢ ✞ ✔✕ ✖ ✒ ✙ ✞ ✛ ✟ ✘ ✗ ✗ ✡ ✕ ☞ ☞ ✔ ✓ ✄ ✆ ✎✒ ✑ ✏ ✎ ✍ ✌ ☞ ✠ ✡ ☎ ✠ ✜ ✞ ☛ ✞ ✠ ✂ ✄ ✆ ✁ ✒ ✓ ✒ ✝ ✛ ✄ ☎ ☎ ☎✆ ✞ ✄ ✙ ✞ ✛✢ ☎✆ ✛ ✠ Example: Swing Components • Component ✎✑✏ ✂☎✄ ☛☎☞ ✝✟✞ ✙✤✣ ✖✦✥ • Leaf (atomic components) Given a frame containing multiple panes, each with multiple buttons/labels/checkboxes… ✫✤✕ - Call paint() method on frame causes everything to be painted. • Composite (top-level Swing - Changing the look and feel on the containers) frame is likewise propagated down. ✗✤✣ ✘✚✙

  59. �✁ ✛ ✒✓ ✏ ✔✕ ✔ ✖ ✣ ✫ ✢ ✧ ✛ ✔ ✱ ✜ ✫ ✚ ✥ ✥ ✚ ✛ ✫ ✜ ✣ ✕ ✏ ✩ ✖ ✪ ★ ✔ ✕ ✥ ✜✏ ✏ ✜✏ ✏ ✥ ✩ ✚ ✛ ✫ ✜ ✔ ✔ ✣ ✢ ★ ★ ✂ ✏ ✕ ★ ✥ ✫ ✥ ✘ ✏ ✛ ✧ ✢ ✕ ✰ ✙ ✣ ✔✕ ✛ ✥ ✕ ✩ ✒✓ ✖ ✖ ✕ ✜ ✢ ✥ ✥ ✕ ✔ ✮ ✛ ✖ ✔ ✔✕ ✏ ✒✓ ★ ✢ ✔ ✣ ✖ ✜ ✕ ✦ ✙ ✏ ✕ ✔ ✖ ✢ ✚ ✘ ✓ ✛ ✖ ✛ ✏ ✚ ✜ ✣ ✖ ✔ ✒✕ ✕ ✒✓ ✛ ✫ ★ ✣ ✖ ✔ ✔✕ ✫ ✥ ✣ ✕ ✔ ✏ ✛ ✖ ✢ ✥ ✓ ✕ ✏ ✩ ✕ ✖ ✢ ✚ ✛ ✔ ✛ ✰ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ★ ✕ ✜ ✜ ✚ ✢ ✥ ✕ ✣ ✖ ✛ ✔ ✖ ✕ ✣ ✥ ✢ ✄ ✙ ✕ ✌ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✍ ✞ ☞ ✛ ✘ ✗ ✗ ✖ ✕ ☞ ✎ ✔ ✒ ✓ ✎✒ ✑ ✏ ✜ ✥ ✥ ✘ ✥ ✓ ✥ ✏ ✥ ✥ ✏ ✒ ✛ ✧ ✢ ✫ ✏ ✙ ✛ ✛ ✔✕ ✖ ✏ ✒✓ ✜✏ ✔ ✛ ✣ ✜ ✖ ✕ ✦ ✙ ✏ ✕ ✧ ✛ ✣ ✕ ✛ ✜ ✒✓ ✜✏ ✔ ✛ ✣ ✖ ✕ ✣ ✦ ✙ ✏ ✚ ✚ ✢ ✥ ✥ ✏ ✛ ✖ ✕ ✰ ★ ✏ ✛ ✔ ✣ ✎✑✏ ✫✤✕ ✗✤✣ Participants ✘✚✙ ✚ ✩✭✬ ✫✤✕ Component Composite Client Leaf • • • •

  60. � ☎✆ ✖ ✗ ✗ ✘ ✛ ✂ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✞ ☞ ☛ ✄ ☎ ☎✆ ✝ ✔ ✟ ✆ ✠ ✡ ☎ ✞ ✞ ✏ ✠ ✒ ✎✒ ✑ ✓ ✎ ✍ ✌ ☞ Structure Component 0..* 0..* Client operation() addComponent() removeComponent() getChild() Composite Leaf +children operation() addComponent() operation() removeComponent() getChild() ✗✤✣ ✘✚✙

  61. � ✎✏ ✁ ✓ ✏ ☞ ✒ ✄ ✟ ✑ ✎ ✎ ✄ ✆✍ ✕ ✌ ☞ ☛ ✄ ✡ ✠ ✄ ✟ ✞ ✝ ✆ ✗✘ ☛ � ✎ ✄ ✟ ✎ ✏ ✑ ✌ ✒ ✕ ☞ ✎ ✄ ✟ ✑ ✆ ✟✚ ✕ ✝ ☞ ☛ ✄ ✡ ✠ ✄ ✟ ✞ ✁ ✍ ✖ ✏ ✗ ✕ ☞ ✝ ☞ ✔ ✒ ✓ ✎✒ ✑ ✎ ✗ ✍ ✌ ☞ ✠ ✞ ☛ ☎ ✡ ✠ ✆ ☎✆ ✘ ✞ ☎ ✂ ✒ ✓ ✒ ✛ ✞ ✠ ✛ ✄ ✛✢ ✙ ✄ ✞ ☎✆ ☎ ✄ ✜ ✙ ✞ ✛ ✟ Collaborations • Collaborations – Client uses component class interface to interact with objects in composite structure. – If recipient is: ✂☎✄ ☞✙✄ ✔✖✕ ✗✤✣ ✘✚✙

  62. � ● ❆ ● ❉ ❍ ❆ ■ ❆ ❉ ▼ ❅ ❇ ● ❍ ◆ ❈ ● ❈ ▲ ❭ ■ ▼ ❆ ■ ● ❍ ❉ ❍ ❅❆ ❅ ▼ ❅ ✮ ✵ ❴ ✲ ✯ ❪ ❨ ✷ ❳ ❫ ✴ ❨ ❪ ❪ ✲ ✬ ✱ ✳ ✴ ✷ ✯ ✮ ✴ ✱ ✷ ✯ ✴ ❳ ✮ ❲ ◆ ❋ ✻✼ ❄ ❯ ❍ ❊ ❉ ❈ ❅ ❍ ❆ ● ■ ❍ ❯ ■ ❯ ❍ ■ ❆ ❖ ■ ❍ ▼ ❏ ❅ ❅ ❆ ❆ ■ ❍ ● ❄ ▲ ❅ ◆ ● ▼ ❱ ■ ● ❈ ❉ ❇ ❭ ■ ❏ ❉ ❍ ■ ▼ ❆ ● ❅ ❊ ❉ ❭ ❈ ❅ ❍ ❆ ● ❅ ❆ ✲ ✲ ● ✴ ✳ ✴ ✵ ✰ ✺ ❨ ✲ ✴ ✸ ✮ ❣ ✲ ✱ ✱ ✮ ✷ ✯ ✲ ✻✼ ✲ ✵ ✮ ✱ ❨ ✮ ❫ ✶ ✲ ✱ ✷ ❴ ✱ ❳ ✴✵ ❣ ✵ ✴ ✻ ✷ ✱ ✶ ✳ ✲ ❪ ✲ ✴ ❨ ✸ ✮ ❣ ✶ ✲ ✰ ✯ ✱ ✴❨ ❨ ✲ ✻✼ ✲ ✵ ✰ ❤ ❨ ❲ ✱ ✳ ✴ ❪ ✳ ✮ ❣ ✱ ✷ ✯ ✴ ❳ ✮ ✬ ✱ ✫ ✥ ❢ ❜ ✪ ✧ ❜ ✫❜ ✩ ❛ ❵ ✯ ✱ ✴❨ ✲ ✲ ❀ ✴❨ ✴ ✱ ✷ ✯ ✲ ✻✼ ✲ ✵ ✮ ❴ ✲ ✯ ✱ ❨ ✳ ✲ ✻✼ ✲ ✵ ✴ ❣ ✱ ✱ ✵ ✷ ✳ ✱ ✯ ✴ � ❅❆ ❋ ✴ ❈ ❅ ❇ ❅ ❃ ❂ ✱ ✴✵ ✿ ✾ ✲ ✽ ✺ ❋ ✻✼ ✲ ✵ ✴ ✯ ✲ ✻✼ ✲ ✵ ✰ ✺ ✴ ✯ ❉❊ ● ✶ ◆ ▼ ❋ ❉ ❊ ❉ ❊ ❍ ● ❅ ❇ ● ❍ ● ● ❅❆ ❈ ▲ ❉ ▼ ❍ ❊ ❅ ▲ ❑ ❅ ■ ❊ ❍ ✳ ✴✵ ❍ ☛ ✕ ✔ ☛ ☛ ✓ ✑ ✒ ✍✑ ✏ ✎ ✍ ✌ ☞ ✟ ✖ ✝ ✡ ✄ ✠ ✟ ☎ ✞ ✝ ✆ ✄☎ ✄ ✂ ✁ ✖ ✗ ✳ ✑ ✲ ✱ ✰ ✯ ✬ ✫ ✪ ★✩ ✥ ✧ ✁ ✑ ✒ ✚ ✚ ✝ ✟ ✚ ✚✜ ✘ ✂ ✝ ✄☎ ✄ ✂ ✛ ✘ ✝ ■ ❆ ❉ ❆ ▲ ◆ ■ ❊ ❅ P ❆ ❉ ❊ ❆ ❈ ❱ ❍ ● ▼ ❄ ❊ ● ❄ ❯ ❍ ❊ ❅ ◗ P ■ ❖ ❅ ■ ❅ ▲ ✵ ❩ ❂ ✳ ✴ ✺ ✼ ✻ ✷ ✯ ✱ ✴❨ ✷ ✺ ✯ ❏ ✴ ❳ ✮ ❲ ✬ ❆ ■ ■ ▼ ❏ ❆ ❉ ❙ ❋ ❑ ❅ ● ❍ ❊ ◆ ◗ P ■ ❅ ❇ ● ❍ ● ◆ ❈ ■ ❘ ❂ ❍ ❊ ❅ ◗ ◆ P ■ ❅ ❍ ● ▼ ▼ ❊ ▲ ▼ ■ ❊ ❅ ❈ ■ ◆ ■ ❍ ❅ ❆ ● ❏ ❅ ❉❊ ❉ ■ ■ ❆ P ❅ ❊ ■ ◆ ▲ ▼❚❙ ❏✦❊ ✯❁❀ ✖✣✢ Consequences ✗✙✘ ❏✦❅ ✯❁❀ ❋✹❬ ✷✹✸ ❄✦❅❆ ❄✦❅ ❨✣✐ ❝❡❞ ✭✦✮ ✤✦✥

  63. � ✯ ✽ ✾ ✬ ✧ ✻ ✪ ✧ ✶ ✼ ✭ ✬ ✧ ✷ ✱ ✱ ✱ ✳ ✧ ✹ ✧ ✼ ✼ ✰ ✲ ✷ ✽ ✫ ✪ ✧ ✧ ✧ ✶ ✬ ✲ ✶ ✭ ✺ ✺ ✩ ✰ ✺ ❃ ✱ ✶ ✰ ✬ ✭ ✮✯ ✭ ✩ ✧ ✺ ✱ ✷ ✽ ✭ ✬ ✲ ✱ ✻ ✰ ✬ ✰ ✱ ✪ ✺ ❂ ✥ ✲ ✶ ✭ ✺ ✱ ✬ ✱ ✱ ✩ ✧ ✵ ✽ ✭ ✧ ✺ ✱ ✬ ✪ ✧ ✾ ✺ ✳ ✩ ✧ ✮ ✧ ✶ ✮✯ ✰ ✱ ✭ ✳ ✬ ✰ ✧ ✪ ✧ ✼ ✹ ✧ ✱ ✳ ✰ ✱ ✳ ✧ ✳ ✭ ✮✯ ✩✭ ✪ ✧ ✪ ✪ ✰ ✬ ✪ ✧ ✲ ✳ ✪ ✾ ✪ ✧ ❄ ✳ ✺ ✫ ✩ ✹ ✧ ✱ ✳ ✰ ✬ ✧ ✻ ✪ ✧ ✶ ✽ ✰ ✭ ✧ ✷ ✶ ✧ ✻ ✰ ✬ ✹ ✫ ✩ ✧ ✹ ✬ ✹ ✶ ✫ ✱ ✩ ✫ ✹ ✱ ✬ ❅ ✧ ✶ ✮✯ ✩✭ ✲ ✼ ✳ ✬ ✰ ✹ ✯ ✧ ✺ ✱ ✬✧ ✫ ✳ ✩✪ ✱ ✳ ✧ ✶ ✮ ✩ ✧ ✺ ✱ ✹ ✭ ✼ ✧ ✹ ✧ ✺ ❂ ❁ ✰ ✰ ✭ ✮✯ ✰ ✱ ✩ ✧ ✶ ✶ ✩✭ ✧ ✱ ✰ ✬ ✭ ✩✭ ✱ ✧ ✺ ✱ ✲ ✳ ✪ ✼ ✪ ✧ ✶ ✧ ✻ ✰ ✁ ✩ ✼ ✭ ✩ ✭ ✱ ✮ ✬ ✰ ✺ ✯ ✹ ✭ ✮ ✯ ✧ ✬✧ ✫ ✱ ✬ ✫ ✵ ✱ ✴ ✬✳ ✧ ✭ ✲ ✹ ✪ ✱ ✭ ✮ ✭ ✹ ✹ ✬ ✱ ✩ ✧ ✵ ✽ ✭ ✼ ✬ ✱ ✧ ✯ ✷ ✱ ✬ ✫ ✭ ✰ ✹ ✪ ✻ ✧ ✧ ✰ ✺ ✠ ☞ ☞ ✔ ✒ ✓ ✎✒ ✑ ✏ ✎ ✍ ✌ ☞ ✞ ✖ ☛ ☎ ✡ ✠ ✆ ✟ ✞ ✝ ☎✆ ☎ ✄ ✂ ✕ ✗ ✬ ✠ ✭ ✮✯ ✩✭ ✬✧ ✫ ✩✪ ✥ ✒ ✓ ✒ ✛ ✞ ✛ ✗ ✛✢ ✙ ✄ ✞ ☎✆ ☎ ✄ ✜ ✙ ✞ ✛ ✘ ✱ ✼ ✧ ✭ ✺ ✱ ✼ ✭ ✳ ✭ ✰ ✱ ✩ ✧ ✶ ✶ ✩ ✬✧ ✪ ✬ ✲ ✶ ✭ ✺ ✺ ✱ ✭ ✪ ✱ ✪ ✧ ✭ ✱ ✬✧ ✧ ✱ ✳ ✰ ✧ ✮ ✪ ✬ ✧ ✺ ✱ ✬ ✫ ✽ ✭ ✬ ✶ ✪ ✲ ✳ ✪ ✬ ✱ ✩ ✧ ✵ ✺ ✽ ✬ ✰ ✬ ✬ ✰ ✹ ✩ ✬ ✧ ✲ ✭ ✬ ✶ ✪ ✱ ✩ ✧ ✪ ✼ ✹ ✧ ✱ ✳ ✰ ✳ ✭ ✮ ✮ ✭ ✩ ✺ ✱ ✩ ✼ ✪ ✶ ✩ ✹ ✧ ✳ ✰ ✪ ✱ ✳ ✹ ✪ ✩✭ ❁ ✭ ✧ ✳ ✪ ✭ ✱ ✰ Why is Composite considered a design pattern and ✽✶✸✷ not just an example of polymorphism? ✬✿❁ ✶✸✷ ✗✤✣ ✽❀✧ ✧✿✾ ✘✚✙ Question? ✧✿✾ ✶✸✷ ✦★✧ •

  64. � ✭ ✶ ✧ ✮ ✧ ✳ ✱ ✬ ✼ ✶ ✱ ✺ ✧ ✬ ✱ ✹ ✫ ✧ ✶ ✱ ✪ ✼ ✭ � ✰ ✱ ✧ ✹ ✱ ✪ ✰ ✳ ❄ ✭ ✻ ✧ ✹ ✩ ✫ ✹ ✭ ✩✭ ✳ ✱ ✧ ✳ ✱ ✬ ✼ ✧ ✪ ✻ ✧ ✩ ✱ ✭ ✹ ✆ ✹ ✻ ✹ ✶ ✧ ✁ ✹ ✧ ❅ ✪ ✮✯ ✰ ✭ ✱ ✧ ✹ ✪ ✱ ✰ ✳ ❄ ✱ ✹ ✭ ✖ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✗ ✏ ✗ ✘ ✛ ✞ ✯ ✜ ✄ ✑ ✎ ☎✆ ✆ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✠ ✍ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ☎ ✙ ✞ ✹ ✹ ✫ ✄ ✱ ✫ ✹ ✧ ✬ ✧ ✬ ✄ ✫ ✰ ✹ ✧ ✬ ✫ ✯ ✱ ✩ ✷ ✒ ✙ ✛✢ ✳ ✠ ✞ ✛ ✛ ✪ ✓ ✒ ✁ ✂ element in list Move to next Current element The Iterator Pattern ✗✤✣ Last element for (int i=0; i < v.size(); i++) { [Gamma et al, pp 257-271] Object o = v.elementAt(i); ✘✚✙ ✧✿✾ … … } First element ☎ ★✭

  65. ✁ � ☎✆ ✖ ✗ ✂ ✘ ✛ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✗ ☞ ☛ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✔ ✡ ☎ ✠ ✞ ✏ ✒ ✓ ✠ ✑ ✎✒ ✎ ✍ ✌ ☞ The Iterator Pattern • Iteration allows a program to successively visit each element in a container class – I.e. over any class that contains a collection of elements • List (Vector), Tree, Hash Table, Graph, … ✗✤✣ ✘✚✙

  66. � ✷ ✺ ✩ ✷ ✩ ✫ ✩ ✧ ✮ ✿ ✯ ✷ ✭ ✻ ✩ ✬ ✩ ✮ ✪ ✬ ✧ ✩ ✳ ✶ ✾ ✸ ✼ ✩ ✁ ✧ ✳ ✩ ✬ ✷ ✸ ✯ ✧ ✶ ✽ ✰ ✧ ✮ ✭ ✯ ✧ ✩ ✬ ✱ ✩ ✻ ❀❁ ✮ ✧ ✵ ✲ ✯ ✴ ✯ ✮ ✭ ✮ ✯ ✰ ✧ ✬ ✰ ✮ ✴ ✱ ✩ ✶ ✯ ✵ ✯ ✩ ✰ ✩ ❂ ✭ ❃ ❄ ❅ ❁ ❆ ❁ ❀ ❃ ✩ ❇ ✶ ✳ ✮ ✷ ✸ ✰ ✧ ✶ ✩ ✽ ❈ ✬ ✺ ✪ ✴ ☎✆ ✖ ✗ ✗ ✘ ✛ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✥ ✕ ☞ ✧ ☎ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☛ ✔ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✮ ✩ ✪ ✮ ✵ ✪ ✧ ✶ ✮ ✧ ✭ ✷ ✯ ✺ ✫ ✭ ✮ ✭ ✻ ✴ ✮ ✬ ✭ ✶ ✧ ✳ ✳ ✬ ✱ ✲ ✯ ✧ ✩✴ ✯ ✮ ✩ ✮ ✬ ✭ ✭ ✮ ✬ ✩ ✰ // Returns true as long as there are more // Returns next element to visit ✮✹✸ public interface Enumeration { ✯★✰ boolean hasMoreElements(); Iterators in Java ✗✤✣ Object nextElement(); ✯★✰ // elements to visit ✘✚✙ ✮✹✸ ✯★✰ ✦★✧ }

  67. �✁ ✒ ✘ ✛ ✄ ✂ ☎✆ ✞ ✄ ✘ ✚✜ ✚ ✠ ✞ ✚ ✁ ✚ ✒ ✽ ✤ ✥ ✩ ✭✮ ✯ ✴ ✵ ✪ ✳ ✶✷ ✧ ✮ ✞ ☎ ✞ ✌ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✗ ☞ ✠ ✍ ☞ ✓ ✔ ✕ ✒ ✎ ✁ ☞ ✖ ✎✒ ✑ ✖ ✏ Iterators in Java ✪✬✫ ✦★✧ // Returns all elements in the Vector public Enumeration elements(); ✰★✱ ✲★✳ // Returns all elements in the Hashtable. // That is, returns all values that have been // entered into the hashtable via put public Enumeration elements(); ✖✣✢ ✗✙✘

  68. �✁ ☎✆ ✕ ✖ ✖ ✗ ✚ ✞ ✘ ✛ ✄ ☎ ✞ ✂ ✄ ✘ ✚✜ ✚ ✠ ✞ ✚ ✁ ✒ ✁ ✔ ☞ ☞ ✓ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎ ✁ ✒ ✁ Example: Using an Iterator with ✖✣✢ ✗✙✘ a Vector

  69. � ✚ ✁ ✒ ✁ ✚ ✞ ✠ � ✆ ✘ ✄ ✞ ☎✆ ☎ ✄ ✁ ✠ ✘ ✡ ✔✗ ✕ ☞ ✡☛ ✖ ✔ ✔ ✡☛ ✑ ☞ ✎ ✡✓ ✎ ✍ ✛ ✚✜ ✞ ✠ ✚ ☞ ✠ ✞ ☛ ☎ ✡ ✆ ✎ ✟ ✞ ✝ ☎✆ ☎ ✄ ✂ ✍ ✌ ☞ ☞ ✗ ✖ ✖ ✏ ✑ ✕ ✎ ✁ ✔ ✒ ✁ ✓ Using an Iterator with a Vector • Client creates an iterator by calling the Vector’s elements method ☞✟✕ ✏✒✑ ✝✟✞ ☞✟✌ ✂☎✄ • Client then accesses elements of Vector via Enumeration’s methods for (Enumeration e = v.elements(); e.hasMoreElements();) { Object o = e.nextElement(); … } ✖✣✢ ✗✙✘

  70. � ☎✆ ✖ ✁ ✗ ✘ ✛ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✗ ☞ ☛ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✔ ✡ ☎ ✠ ✞ ✏ ✒ ✓ ✠ ✑ ✎✒ ✎ ✍ ✌ ☞ Implementation of Iterator for Vector • Vector must implement method – Enumeration elements () – Must return an implementation of the Enumeration interface – Iterator must successively return values in vector ✗✤✣ ✘✚✙

  71. �✁ ☎✆ ✖ ✗ ✗ ✘ ✛ ✞ ✙ ✜ ✄ ☎ ✞ ✂ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ☞ ☞ ☛ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ✞ ✔ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ Classes for Vector Iterator ✗✤✣ ✘✚✙

  72. class Vector { … public Enumeration elements () { return new VectorEnumeration (this); } … } How we might implement an enumeration in a vector. (Not necessarily how it is really implemented.)

  73. import java.util.Vector; class VectorEnumeration { // Represents where we are in the vector private int currentElement = 0; // The vector we are iterating through private Vector v; public boolean hasMoreElements () { return (currentElement < v.size()); } public Object nextElement () { Object returnElement = v.get (currentElement); currentElement++; return returnElement; } public VectorEnumeration (Vector v) { this.v = v; } }

  74. � ☎✆ ✖ ✗ ✂ ✘ ✛ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✗ ☞ ☛ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✔ ✡ ☎ ✠ ✞ ✏ ✒ ✠ ✎✒ ✑ ✓ ✎ ✍ ✌ ☞ General Form of Iterator Abstract representation of container Enumeration interface Concrete representation Concrete iterator for specific container class - e.g. of container - e.g. Vector iterator for Vector is different from iterator for hashtable ✗✤✣ ✘✚✙

  75. � ☎✆ ✖ ✗ ✗ � ✛ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✘ ☞ ☛ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✔ ✆ ✡ ☎ ✠ ✞ ✏ ✒ ✓ ✠ ✑ ✎✒ ✎ ✍ ✌ ☞ Relation to Quality Attributes • Modifiability – Separates iteration behaviour from specifics of container class – Therefore can change container class without impacting code that iterates over it • Performance – May be some minor performance penalty through indirection in iterator ✗✤✣ ✘✚✙

  76. � ☎✆ � ✗ ✗ ✘ ✛ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✖ ☞ ☛ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✔ ☎ ✡ ✞ ✏ ✒ ✓ ✎✒ ✠ ✑ ✎ ✍ ✌ ☞ Builder Design Pattern [Gamma et. al. pp. 97-106] • Intent – Separate the construction of a complex object from its representation ✗✤✣ ✘✚✙

  77. � ☎✆ ✖ ✓ ✗ ✘ ✛ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✗ ☞ ☛ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✔ ✡ ☎ ✠ ✞ ✏ ✒ ✓ ✠ ✑ ✎✒ ✎ ✍ ✌ ☞ Applicability • Use the Builder pattern when… – The algorithm for creating a complex object should be independent of the parts that make up the object and how they’re assembled – The construction process must allow different representations for the object that’s constructed ✗✤✣ ✘✚✙

  78. � ✌✍ ✎ ✜ ✮ ✌ ✬ ✕ ✚ ✑ ✜ ✕ ✣ ✛ ✔ ✙ ✒✓ ✑ ✕ ✍ ✜ ✑ ✓ ✙ ✩ ✫ ✌ ✟ ★ ✬ ✕ ✑ ✕ ✓ ✌ ✌✑ ✖ ✌☛ ✵ ✠ ✧ ✖ ✂ ✝ ✥ � ✌ ✍ ✒ ✏ ✖ ✌ ✕ ✠ ✥ ✍ ✱ ✌ ✕ ✒✓ ✑ ✌ ✛ ✎ ✲ ✙ ✖ ✠ ✏ ✌✑ ✕ ✒ ✒ ✖ ✍ ✕ ✎ ✓ ✙ ✎ ✖ ✒ ★ ✌ ✧ ✞ ✟ ☎ ✳ � ✕ ✍ ✙ ✖ ☛ ✬ ✍ ✕ ✚ ✲ ✌ ✎ ✖ ✕ ✌ ✖ ✖ ✌ ✌ ✙ ✒ ✚ ✒ ✖ ✑ ✌ ✍ ✒ ✏ ✖ ✌ ✕ ✛ ✑ ✜ ✫ ✍ ✕ ✖ ✒ ☛ ✕ ✓ ✌ ✜ ✑ ✌ ✎ ✙ ✫ ✜ ✑ ✌ ✖ ✫ ✒ ✏ ✌ ✬ ✕ ✕ ✪ ✑ ✕ ✖ ✒ ☛ ✌ ✬ ✕ ✚ ✫ ✔ ✕ ✕ ✪ ✌ ✠ ✎ ✕ ✍ ✜ ✑ ✓ ✙ ✍ ✖ ✛ ✓ ✓ ✜ ✕ ✌✍ ✣ ✔ ✙ ✶ ✌ ✫ ☛ ✹ ✍ ✑ ✒ ✓ ✙ ✍ ✌ ✬ ✕ ✌ ✏ ✌ ✛ ✕ ✬ ✫ ✕ ✌✑ ✑ ✑ ✒ ✫ ✍ ✑ ✌ ✛ ✜ ✕ ✌ ✕ ✕ ✖ ✍ ✖ ✌ ✍ ✒ ✏ ✖ ✌ ✕ ✍ ✒ ✒ ✑ ✔ ✒ ✒✓ ✌✑ ✎ ✏ ✎ ✌✍ ✠ ✌ ✕ ✞ ✕ ★ ✞ ✟ ✦✧ � ✕ ✌✍ ✓ ✔ ✙ ✍ ✚ ✙ ✖ ☛ ✒ ✏ ✙ ✑ ✕ ✖ ✒ ☛ ✟ ✆✝ ☎ ✞ ✒ ✓ ✎✒ ✑ ✏ ✎ ✍ ✌ ☞ ✠ ☛ ☞ ☎ ✡ ✠ ✆ ✟ ✞ ✝ ☎✆ ☎ ✄ ✂ ✔ ☞ ☎ ✄ � ✒ ✓ ✒ ✛ ✞ ✠ ✛ ✛✢ ✙ ✞ ✕ ☎✆ ☎ ✄ ✜ ✙ ✞ ✛ ✘ ✗ ✗ ✖ ✞ ✣ ✆✝ ✎ ✍ ✒ ✏ ✖ ✌ ✕ ✖ ✌ ✛ ✫ ✜ ✠ ✮ ✌ ✬ ✕ ✚ ✎ ✕ ✓ ✌ ✪ ✌ ✌ ✯ ☛ ✙ ✌ ✌✑ ✖ ☛ ✌ ✖ ✌ ✬ ✕ ✏ ✰ ✌ ✍ ✒ ✑ ✌☛ ✌ ✰ ✛ ✒✓ ✑ ✌ ✏ ✫ ✓ ✪ ✛ ✒ ✎ ☛ ✑ ✌ ✔✫ ✪ ✌ ✑ ✑ ✒ ✒✓ ✕ ✑ ✕ ✍ ✜ ✑ ✓ ✙ ✩ ✠ ✟ ✞ ✖ ✕ ☛ ✏ ✕ ✍ ✑ ✙ ✙ ✖ ✌ ✬ ✕ ✛✢✜ ✎✘✓ ✎✘✓ ✔✢✭ ✙✓✸✷ ✑✸✺ ✎✘✓ ✛✢✜ ✛✢✜ ✕✗✖ ✎✘✓ ✎✘✓ ✗✤✣ ✎✘✓ ✎✘✓ ✏✄✙ Participants ✘✚✙ ✎✘✓ ✎✘✓ ✏✄✙ ✎✘✓ ✎✘✓ ✕✗✖ ✕✗✖ ✎✘✓ ✏✄✙ ✁✄✂ ✎✘✓ ✕✗✖ ✕✗✖ ✎✘✓ ✡☞☛ ✎✘✓ ✁✄✂ ✤✄✥ ✴✄✟

  79. � ✬ ✮ ✿ ✾★ ✽ ✭ ✯ ✪ ✮ ❀ ✯ ✾ ✶ ✶❅ ✬ ✫ ✪ ❂ ✶ ✭ ✫ ✧ ✻ ✶ ✬ ❅ ✶ ✸ ✯ ✶ ✧ ✸ ❯ ❖ ❙ ❙ ❱ ❘ ❚❯ ❙ ◗ ▼ ▲ ❑ ❆ ✶❅ ✸ ✮ ✸ ✮ ✫ ❂ ❂ ✶ ❀ ✯ ✿❅ ✫ ✬ ✮ ✶ ✭ ❘ ✬ ✶ ★ ✬ ✭ ✯ ✮ ✬ ✫ ✽ ❁ ★ ✮ ★ ✭ ★ ✿ ✭✯ ✮ ✿ ★ ✬ ✁ ❀ ❀ ✫ ✪ ✶ ✻ ✮ ✶ ✽ ✻ ✸ ❊ ✿ ✯ ✫ ✮ ✶ ✮ ✿ ★ ✸ ★ ✬ ★ ✮ ✬ ✮ ✿ ★ ✬ ★ ❀ ❀ ✫ ✪ ✶ ✮ ✯ ❩ ❬ ✽ ❙ ❣ ❝ ❱ ◆ ❞ ◆ ❱ ❘ ❝ ❬ ❭ ❖ ◆ ◆ ❭ ❬ ❙ ◗ ❯ ❬ ❘ ❫ ❙ ❭ ❱ ❖ ❯ ❭ ❪ ❬ ❙ ❱ ◆ ❬ ❙ ❖ ❘ ❬ ❭ ❡ ❯ ❝ ❘ ❚ ◆ ❘ ❯ ❡ ❯ ❯ ❬ ❙ ❘ ❥ ❯ ✐ ❩ ❚ ❘ ❤ ❵ ❴ ❘ ❘ ❙ ❘ ❙ ❱ ❯ ❖ ❯ ❬ ❩ ❯ ❬ ❵ ❱ ❱ ❵ ❭ ◆ ❙ ❫ ❴ ❬ ❙ ❖ ❱ ❭ ❭ ❱ ❙ ▲ ◆ ❲ ❝❘ ❫ ❴ ❵ ❭ ❬ ❩ ❙ ❪ ❭ ❱ ❙ ❘ ❱ ❬ ❬ ❭ ❙ ❖ ❱❯ ◆ ❪ ❯ ❵ ❬ ❭ ❙ ❫ ❯ ✻ ★ ✸ ✻ ✪ ✴ ✳ ✲ ★ ✧ ✮ ✸ ★ ✸ ✬ ✶ ✮ ✭ ✫ ✼ ✬ ★ ✸ ✬ ✶ ✻ ✮ ✺ ★ ✮ ✶ ✯ ✽ ✬ ✮ ✮ ✿ ★ ✮ ✿ ✯ ❃ ✪ ✬ ✫ ✽ ❁ ✸ ✫ ✾★ ✸ ★ ✫ ❀ ✫ ✮ ✯ ✿ ✪ ✿ ✶ ✮ ✿ ✷ ✶ ✶ ☞ ✕ ☞ ☞ ✔ ✒ ✓ ✎✒ ✑ ✏ ✎ ✍ ✌ ✠ ✗ ✞ ☛ ☎ ✡ ✠ ✆ ✟ ✞ ✝ ☎✆ ☎ ✄ ✂ ✖ ✗ ★ ✛ ✵ ✳✴ ✲ ✱ ✬ ✭ ★ ✪ ✦ ✥ ✒ ✓ ✒ ✞ ✘ ✠ ✛ ✛✢ ✙ ✄ ✞ ☎✆ ☎ ✄ ✜ ✙ ✞ ✛ ❄ ✱ ✿ ❇ ✬ ✮ ✿ ✶ ✶ ❄ ✲ ❉ ❈ ❆ ✿ ✬ ✶ ❄ ✯ ✫ ✽ ❁ ★ ✮ ★ ✬ ✭ ✾★ ❊ ❋ ✮ ★ ✮ ★ ❀ ✯ ★ ★ ✬ ✮ ✸ ✶ ✸ ✶ ✪ ✮ ✶ ★ ✬ ✿ ✭ ✧ ✭ ✪ ✫ ✧ ✭✯ ✿ ❂ ✮ ✽ ✮ ✶ ✪ ✪ ✿ ✶ ✸ ✶❅ ✮ ✽ ✮ ✧ ✸ ✽ ✯ ❁ ✶ ✷ ✬ ★ ✬ ✭✯ ✽ ❂ ✮ ✶ ★ ✮ ✸ ✦ ● ✬ ★ ❋ ✶ ❀ ★ ✶ ❇ ✿ ✽ ✶ ✭✯ ✿ ★ ✮ ✫ ✮ ❆ Example: JDOM SAXBuilder ❂✪✩★ ▼✤❯ Constructs products from an XML document. ✪❂✩★ ◆P❭ ◆✹❞ ❪✤❫ ✪❂✩★ ◆P❭ ▼✤❯ ❱❝❜❛ ✗✤✣ ▼✤❯ ❂✪✩★ ❂❏■ ▼✤❯ ▼✤❯ ✫✹✸ ✫✹✸ ❲❨❳ ❂✪✩★ ✪✩★ ✘✚✙ ▼✤❯ ◆P❖ ◆P❭❱❜❛ ✧✩★ ▼✤❘ ❂❏■ ✮✰✯ ❂✩★ ◆P❖ ❙❢❡ ✬❍❊ ◆P❖ ✫✤✬ ✪✩★ ✧✩★ ✧✩★ ✧✩★ •

  80. � ☎✆ ✖ ✗ ✁ ✘ ✛ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✗ ☞ ☛ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✔ ✠ ✡ ☎ ✆ ✞ ✏ ✒ ✠ ✎✒ ✑ ✓ ✎ ✍ ✌ ☞ Structure Builder Director buildPart() ConcreteBuilder <<creates>> Product buildPart() getProduct() ✗✤✣ ✘✚✙

  81. � ☎✆ ✖ ✗ ✗ ✂ ✛ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✘ ☞ ☛ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✔ ✠ ☎ ✡ ✞ ✏ ✒ ✓ ✠ ✑ ✎✒ ✎ ✍ ✌ ☞ Collaborations The client creates the Director object and • configures it with the desired Builder object • Director notifies the Builder whenever a part of the product should be built • Builder handles requests from the director and adds parts to the product • The client retrieves the product from the builder ✗✤✣ ✘✚✙

  82. � ☎✆ ✖ ✗ ✘ ✁ ✛ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✗ ☞ ☛ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✔ ✠ ✡ ☎ ✆ ✞ ✏ ✒ ✠ ✎✒ ✑ ✓ ✎ ✍ ✌ ☞ Collaborations (cont’d) aClient : Client aDirector : aConcreteBuilder Director : ConcreteBuilder new ConcreteBuilder() new Director(aConcreteBuilder) construct() buildPartA() buildPartB() buildPartC() getResult() ✗✤✣ ✘✚✙

  83. � ☎✆ ✖ ✗ ✘ ✂ ✛ ✞ ✙ ✜ ✄ ☎ ✞ ☞ ✄ ✙ ✛✢ ✛ ✠ ✞ ✛ ✒ ✓ ✒ ✕ ✗ ☞ ☛ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✔ ✡ ☎ ✠ ✞ ✏ ✒ ✠ ✎✒ ✑ ✓ ✎ ✍ ✌ ☞ Consequences • Benefits – It lets you vary a product’s internal representation – It isolates code for construction and representation – It gives you finer control over the construction process (object constructed step-by-step under the director’s control) • Disadvantages – N/A ✗✤✣ ✘✚✙

  84. � ✝ ✞ ✝ ✒ ✄ ✆ ✝ ✒ ✓ ✖✗ ✓ ✕ ✘ ✄ ✆ ✒ ☛ ✔ ✝ ✌✍ ✒ ✝ ✒ ✓ ✆ ✓ ✄ ✞ ✠ ✕ ✓ ✖✗ ✆ ✎ � ✠ ✄ ✎ ✌✍ ✝ ✂ ✛ ✎ ✝ ✆ ✄ ✒ ✎ ✄ ✚ ✎ ✞ ✄ ☛ ✒ ✄ ✆ ✙ ✆ ✎ ✆ ✠ ✆ ✓ ✎ ✆ ✓ ✎ ✄ ✌✍ ✍ ✘ ✗ ✗ ✖ ✕ ☞ ☞ ✔ ✒ ✓ ✎✒ ✑ ✏ ✎ ✌ ✝ ✟ ✂ ✄ ☎ ☎✆ ✝ ✞ ✆ ☞ ✠ ✡ ☎ ☛ ✞ ✠ ✛ ✞ ✙ ✛ ✆ ✆ ✞✟ ✝ ✂ ✆ ✁ � ✜ ✒ ✓ ✒ ✞ ✞ ✠ ✛ ✄ ✛✢ ☎ ☎✆ ✞ ✙ ✄ Builder, Composite and Iterator Design Patterns often Relationship between Composite, ✂☎✄ Iterator, and Builder ✗✤✣ ✂☎✄ ✏✑☛ ✘✚✙ ✡☞☛ work together ✂☎✠ ✂☎✄ •

  85. � ✗ ✘ ✄ ✆ ✝ ✒ ✓ ✖✗ ✓ ✕ ✘ ✎ ✄ ✆ ✠ ✠ ✍ ✥ ✘ ✎ ✄ ✌✍ ✝ ✘ ✎ ✧ ✓ ✆ ✛ ✠ ✦ ★ ✆ ✄ ✄ ✚ ✎ ✄ ✒ ✏ ✪ ✘ ✎ ✓ ✆ ✠ ✎ ✆ ✠ ✙ ✘ ✆ ✂ ✟ ✝ ✄ ✩ ✧ ✌ ✦ ✘ ✄ ✍ ✁ ✦ ✛ ☞ ✗ ✖ ✕ ☞ ☞ ✔ ✒ ✓ ✎✒ ✑ ✏ ✎ ✍ ✌ ✠ ✠ ✞ ☛ ☎ ✡ ✠ ✆ ✟ ✞ ✝ ☎✆ ☎ ✄ ✂ ✁ ✗ ✘ ✎ ✠ ✎ ✆ ✒ ✏ ✥ ✘ ✒ ✓ ✒ ✛ ✞ ✛ ✛ ☎✆ ✞ ✙ ✛✢ ✙ ✄ ✜ ✄ ✞ ☎ Summary of Patterns ✗✤✣ Discussed… ✘✚✙ Behavioural Patterns: Creational Patterns: Structural Patterns: ✡☞☛ • • •

Recommend


More recommend