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

cisc 323 intro to software engineering
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CISC 323 Intro to Software Engineering

Week 6: Design Patterns

slide-2
SLIDE 2

CISC 323 Intro to Software Engineering

Lecture 6-1 Introduction to Design Patterns

slide-3
SLIDE 3
✂ ✄ ✄☎ ✆ ✝ ✞ ☎ ✟ ✠ ✄ ✡ ✝ ✟ ☛ ☞ ✌ ✍ ✎ ✏ ✍ ✑
☛ ☛ ✓ ✔ ✕ ✕ ✖ ✖✘✗ ✙ ✝ ✗ ✚ ✂ ✄ ✄☎ ✝ ✂ ✗ ✙✛ ✕✢✜ ✙ ✟ ✝ ✙ ✑
  • Review of Quality Attributes
  • Recall

– Requirements analysis determines desired quality attributes of system – When designing system, attempt to

✣ ✤ ✥✧✦ ★ ✩ ✪ ✫✭✬ ✮ ✯ ✦ ✮✰ ✱ ✫ ✯ ✪ ✰ ✲ ✳ ✴✧✦ ✯ ✦ ✵ ✶ ✮ ✷ ✪ ✩ ✬ ✮ ✩ ✩ ✯ ✪ ✸ ✶ ✩ ✦ ✰ ✹ ✮ ✬ ★ ✱ ✩ ✸ ✦ ✹✦ ✩ ✺ ✻ ✦ ✥✭✶ ✼ ✦ ✯ ✪ ✰ ✲ ✩ ✴ ✯ ✱ ✶ ✽ ✴✥✧✦ ✰ ✪ ✽ ★ ✼ ✴ ✱ ✪ ✼ ✦ ✰

– Some attributes amenable to mathematical expression (performance, availability), others less so (modifiability)

slide-4
SLIDE 4
✂ ✄ ✄☎ ✆ ✝ ✞ ☎ ✟ ✠ ✄ ✡ ✝ ✟ ☛ ☞ ✌ ✍ ✎ ✏ ✍✑ ✒ ✑ ✓ ☛ ☛ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✝ ✘ ✛ ✂ ✄ ✄☎ ✝ ✂ ✘ ✚✜ ✖✣✢ ✚ ✟ ✝ ✚ ✑ ✒ ✑

Example Quality Attributes

  • Performance
  • Availability
  • Security
  • Modifiability
  • Usability
  • Testability
slide-5
SLIDE 5
✂ ✄ ✄☎ ✆ ✝ ✞ ☎ ✟ ✠ ✄ ✡ ✝ ✟ ☛ ☞ ✌ ✍ ✎ ✏ ✍✑ ✒ ✑ ✓ ☛ ☛ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✝ ✘ ✛ ✂ ✄ ✄☎ ✝ ✂ ✘ ✚✜ ✖✣✢ ✚ ✟ ✝ ✚ ✑ ✒ ✑

Design Choices Trade Off Quality Attributes

  • E.g., to obtain performance, may need to use

a very complex algorithm, reducing modifiability

  • E.g., to obtain availability, may need to

replicate data sources, reducing security

  • Therefore, design choices rely on knowing

required targets for quality attributes

– At what point is security sufficient? – At what point is system sufficiently fast? – … Etc.

slide-6
SLIDE 6
✂ ✄ ✄☎ ✆ ✝ ✞ ☎ ✟ ✠ ✄ ✡ ✝ ✟ ☛ ☞ ✌ ✍ ✎ ✏ ✍✑ ✒ ✑ ✓ ☛ ☛ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✝ ✘ ✛ ✂ ✄ ✄☎ ✝ ✂ ✘ ✚✜ ✖✣✢ ✚ ✟ ✝ ✚ ✑ ✒ ✑

Design Patterns

  • Software architecture normally covers the

large-scale design of software systems

  • Within architectural components, it can be

useful to apply micro-architectures to help design these components

– Sometimes called design patterns

slide-7
SLIDE 7
✂ ✄ ✄☎ ✆ ✝ ✞ ☎ ✟ ✠ ✄ ✡ ✝ ✟ ☛ ☞ ✌ ✍ ✎ ✏ ✍✑ ✒ ✑ ✓ ☛ ☛ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✝ ✘ ✛ ✂ ✄ ✄☎ ✝ ✂ ✘ ✚✜ ✖✣✢ ✚ ✟ ✝ ✚ ✑ ✒ ✑

Design Patterns

  • A design pattern is a set of rules of how to

structure code in order to solve a particular class of problem

  • Provides vocabulary to discuss design
  • Particularly suited to OO design as patterns

can be expressed in terms of classes, associations

  • Design patterns normally help in improving at

least one quality attribute, perhaps at the expense of others

slide-8
SLIDE 8
✂ ✄ ✄☎ ✆ ✝ ✞ ☎ ✟ ✠ ✄ ✡ ✝ ✟ ☛ ☞ ✌ ✍ ✎ ✏ ✍✑ ✒ ✑ ✓ ☛ ☛ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✝ ✘ ✛ ✂ ✄ ✄☎ ✝ ✂ ✘ ✚✜ ✖✣✢ ✚ ✟ ✝ ✚ ✑ ✒ ✑

Example: the Publish and Subscribe Design Pattern

  • Problem

– A data source is shared by a number of clients – Multiple clients depend on the value of the data source – Modifiability attribute important

slide-9
SLIDE 9
✂ ✄ ✄☎ ✆ ✝ ✞ ☎ ✟ ✠ ✄ ✡ ✝ ✟ ☛ ☞ ✌ ✍ ✎ ✏ ✍✑ ✒ ✑ ✓ ☛ ☛ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✝ ✘ ✛ ✂ ✄ ✄☎ ✝ ✂ ✘ ✚✜ ✖✣✢ ✚ ✟ ✝ ✚ ✑ ✒ ✑

Concrete Example: Expense Report

Item $US Cost $Cdn Cost Total cost ($Cdn) Air $670 $670 Hotel $980 $1470 Meals $70 $105 TOTAL $1050 $670 $1245

Forms-based interface for calculating expense reports.

slide-10
SLIDE 10
✁ ✂ ✄ ✄☎ ✆ ✝ ✞ ☎ ✟ ✠ ✄ ✡ ✝ ✟ ☛ ☞ ✌ ✍ ✎ ✏ ✍✑ ✒ ✑ ✓ ☛ ☛ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✝ ✘ ✛ ✂ ✄ ✄☎ ✝ ✂ ✘ ✚✜ ✖✣✢ ✚ ✟ ✝ ✚ ✑ ✒ ✑

Concrete Example: Expense Report

Item $US Cost $Cdn Cost Total cost ($Cdn) Air $670 $670 Hotel $980 $1470 Meals $70 $105 TOTAL $1050 $670 $1245

Items depend on value of exchange rate. If exchange rate changes, these values should change too.

slide-11
SLIDE 11
✂ ✄ ✄☎ ✆ ✝ ✞ ☎ ✟ ✠ ✄ ✡ ✝ ✟ ☛ ☞ ✌ ✍ ✎ ✏ ✍✑ ✒ ✑ ✓ ☛ ☛ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✝ ✘ ✛ ✂ ✄ ✄☎ ✝ ✂ ✘ ✚✜ ✖✣✢ ✚ ✟ ✝ ✚ ✑ ✒ ✑

Dependencies

Exchange Rate $Cdn cost of hotel $US cost of hotel $Cdn cost of meals $US cost of meals depends depends depends depends

slide-12
SLIDE 12
✁ ✂ ✄ ✄☎ ✆ ✝ ✞ ☎ ✟ ✠ ✄ ✡ ✝ ✟ ☛ ☞ ✌ ✍ ✎ ✏ ✍✑ ✒ ✑ ✓ ☛ ☛ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✝ ✘ ✛ ✂ ✄ ✄☎ ✝ ✂ ✘ ✚✜ ✖✣✢ ✚ ✟ ✝ ✚ ✑ ✒ ✑

Problem

  • When exchange rate modified, must update values of

all components that depend on it

✂☎✄ ✆ ✄ ✝☎✞ ✆ ✄ ✟ ✠ ✡ ☛ ☞☎✌ ✞ ✍ ✍ ✂ ✎✑✏ ✂ ✏ ✞ ✟✒ ✞ ✌ ✄ ✌ ☛ ✡ ✓☎✄ ✒ ✄ ✌ ✓ ✞ ✌ ✄ ✔ ✏ ✂☎✕ ✌ ✖✄ ✆ ✕ ☛ ✄
✂ ✎ ✡ ✏ ✕ ✠ ✡ ✄ ✡ ✖ ✆ ✄ ✕ ☛ ✒ ✆ ✞ ✗✘ ✄ ✟ ✡ ✎ ✌ ✟ ✞ ✓ ✎ ✝ ✎ ✕ ✗ ✎ ✘ ✎ ☛✚✙

Exchange Rate $Cdn cost of hotel $US cost of hotel $Cdn cost of meals $US cost of meals depends depends depends depends

slide-13
SLIDE 13
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎ ✁✒ ✁ ✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✁✒ ✁

Solution: Publish and Subscribe Design Pattern

  • A source component

contains data on which a set of client components depend (e.g., current exchange rate)

  • The client components

subscribe to changes in the data

  • The source component

publishes any changes in the data

Exchange Rate $Cdn cost of hotel (1) Client component subscribes to changes in exchange rate (2) Source component publishes changes in exchange rate

slide-14
SLIDE 14
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎ ✁✒ ✁ ✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✁✒ ✁

Solution: Publish and Subscribe Design Pattern

  • Advantages
✠ ✗✘ ✎ ✡ ✂☎✄ ✆ ✏ ✞ ✓☎✄ ✓ ✞ ✄ ✡ ✌ ✞ ☛ ✓☎✄ ✒ ✄ ✌ ✓ ✞ ✌ ✍ ✂ ✞ ✡ ✠ ✗ ✡ ✏ ✆ ✎ ✗ ✄ ✆ ✡ ✕ ✆ ✄
✂☎✄ ✆ ✄ ✝☎✞ ✆ ✄ ✏ ✕ ✌ ✕ ✓ ✓ ✕ ✆ ✗ ✎ ☛ ✆ ✕ ✆ ✙ ✡ ✠ ✗ ✡ ✏ ✆ ✎ ✗ ✄ ✆ ✡ ✕ ☛ ✆ ✠ ✌ ☛ ✎ ✟✄
✞ ✆ ✄ ✔ ✕ ✟✒ ✘ ✄ ✄ ✕ ✌ ✙ ✄ ✌ ☛ ✆ ✎ ✄ ✡ ✎ ✌ ✄ ✔ ✒ ✄ ✌ ✡ ✄ ✆ ✄ ✒ ✞ ✆ ☛ ☛ ✂☎✕ ☛ ✕ ✆ ✄ ✄ ✔ ✒ ✆ ✄ ✡ ✡ ✄ ✓ ✎ ✌ ☎ ✆ ✝ ✗ ✄ ✏ ✞ ✟✄ ✡ ✠ ✗ ✡ ✏ ✆ ✎ ✗ ✄ ✆ ✡ ☛ ✞ ☛ ✂☎✄ ✄ ✔ ✏ ✂☎✕ ✌ ✖✄ ✆ ✕ ☛ ✄

Exchange Rate $Cdn cost of hotel (1) subscribe (2) publish

Subscriber Publisher

slide-15
SLIDE 15
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎ ✁✒ ✁ ✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✁✒ ✁

Implementing Design Patterns

  • Design patterns represents strategies for
  • rganizing code, not explicit implementations
  • Actual implementation used must be

determined on a case-by-case basis

slide-16
SLIDE 16
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Event-Connector Implementation

✥ ✦★✧ ✩ ✪★✫ ✧ ✫ ✧ ✬ ✧ ✭ ✧ ✫ ✮✰✯ ✱ ✧ ✲✤✳ ✴ ✵✷✶ ✫ ✸ ✧ ✹ ✶ ✮ ✧ ✺ ✵✷✶ ✫ ✸ ✧ ✻ ✲ ✭ ✧ ✫ ✮ ✼ ✧ ✱ ✼ ✧ ✽ ✧ ✫ ✮ ✪ ✫ ✸ ✴ ✵✷✶ ✫ ✸ ✧ ✪★✫ ✧ ✳ ✴ ✵✷✶ ✫ ✸ ✧ ✼ ✶ ✮ ✧ ✥ ✺✤✾ ✫ ✫ ✧ ✴ ✮ ✽ ✿ ❀ ✽ ✴ ✼ ✪ ❀ ✧ ✼ ✽ ✮ ✾ ✱ ✿ ❀❁ ✪ ✽ ✵ ✧ ✼ ✭ ✪ ✶ ✧ ✭ ✧ ✫ ✮ ✴✾ ✫ ✫ ✧ ✴ ✮ ✾ ✼ ❂ ❃✤❄ ❅✤❆❇ ❈ ❉ ❅✤❊ ❈ ❆ ❇ ❋
❍■ ■ ❊ ❇ ❏ ❑ ❆ ▲ ❋ ❈ ▼ ❊ ❏ ❏ ❍ ❇ ❍■ ■ ❊ ❇ ❏ ◆ ❖✤P ◗❘ ❙★❚ ❯✷❱ ❯✷❲ ❳❨ ❩ ❚ ◗❭❬ ❪ ❙✤❫ ❙ ❳❨ ❴✤❵ ❱ ❯✷❲ ❳❨ ❩ ❛ ❲ ❜ ❩ ❝ ❯✷❲ ❳❨ ❩ ❞ ❴✤❡ ❩ ❳ ❜ ❩ ❡ ❩ ❳ ❜ ❚

Exchange Rate $Cdn cost of hotel (1) subscribe = connectAsTarget (2) publish = raiseEvent

Subscriber Publisher

slide-17
SLIDE 17
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

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); }

slide-18
SLIDE 18
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

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; } }

slide-19
SLIDE 19
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

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); } }

slide-20
SLIDE 20 ✓ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

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

slide-21
SLIDE 21
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒
✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒

Sequence Diagram

Publisher Subscriber1 Subscriber2

connectAsTarget connectAsTarget raiseEvent (change in state initiated by outside source or Publisher) performEvent performEvent

slide-22
SLIDE 22
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒
✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒

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

slide-23
SLIDE 23
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒
✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒

Reading

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

CISC 323 Intro to Software Engineering

Lecture 6-2 Adapter, Façade, Abstract Factory, and Flyweight Design Patterns

slide-25
SLIDE 25
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒
✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒

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

  • perations in the predefined Vector class
slide-26
SLIDE 26
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒
✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒

Adapter Design Pattern

  • Options:

– Implement BookList from scratch

✁ ✂☎✄ ✆✝ ✞✟ ✠☎✡ ✝ ☛ ✝ ☞ ☞ ✌ ☞ ✆ ☞ ✌ ✞ ✝

– Use Vector

✁ ✍ ✌ ✞ ✎ ✏ ✑ ✝ ✏ ✒ ✝ ✟ ✠ ☞ ✝ ✒ ✠ ✞ ✏ ✝ ☞ ✓✕✔ ✖ ✝ ✓ ✌ ☞ ✗ ✌ ✌ ✘ ✙ ✠ ✟ ✏

– Adapt Vector to BookList interface

slide-27
SLIDE 27
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒
✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒

Adapter

  • Two ways of adapting the Vector

– Class adapter

✁ ✗ ✔ ✟ ✝ ✒ ✌ ✞ ✠ ✞ ✤ ✝ ☞ ✠ ✏ ✔ ✞ ✖ ✝

– Object adapter

✁ ✗ ✔ ✟ ✝ ✒ ✌ ✞ ✔ ✑ ✑ ☞ ✝ ✑ ✔ ✏ ✠ ✌ ✞
slide-28
SLIDE 28
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒
✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒

Class Adapter

slide-29
SLIDE 29
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒
✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒

Class Adapter

public interface BookList { public int getNumBooks(); public Book getBook(int n); public void addBook(Book newBook); }

BookList.java

slide-30
SLIDE 30

import java.util.Vector; public class BookListClassAdapter extends Vector implements BookList { public int getNumBooks() { return size(); } public Book getBook(int n) { 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); } }

BookListClassAdapter.java

slide-31
SLIDE 31
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎ ✒
☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒
  • // 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”); } }

SampleClient.java

slide-32
SLIDE 32 ✒ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎ ✒
☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒
  • Class Adapter
  • Adapter simply inherits operations of Vector
  • Clients use BookList interface, not

BookListClassAdapter directly

✂☎✄ ✆ ✄ ✝☎✞ ✆ ✄ ✂ ✟ ✠☎✄ ✡ ☛ ☞ ✌✍ ✟✏✎ ✑✄ ✒ ✂ ✞ ✠ ✡ ✞ ✝ ✓✏✔✕ ✖✘✗ ✙ ✚✜✛ ✢✣ ✤ ✥ ✦✏✧ ★ ✩
slide-33
SLIDE 33
✂ ✄ ✄☎ ✆ ✝ ✞ ☎ ✟ ✠ ✄ ✡ ✝ ✟ ☛ ☞ ✌ ✍ ✎ ✏ ✍ ✑
☛ ☛ ✓ ✔ ✕ ✕ ✖ ✖✘✗ ✙ ✝ ✗ ✚ ✂ ✄ ✄☎ ✝ ✂ ✗ ✙✛ ✕✢✜ ✙ ✟ ✝ ✙ ✑
  • General Form of Class Adapter
slide-34
SLIDE 34
✂ ✄ ✄☎ ✆ ✝ ✞ ☎ ✟ ✠ ✄ ✡ ✝ ✟ ☛ ☞ ✌ ✍ ✎ ✏ ✍ ✑
☛ ☛ ✓ ✔ ✕ ✕ ✖ ✖✘✗ ✙ ✝ ✗ ✚ ✂ ✄ ✄☎ ✝ ✂ ✗ ✙✛ ✕✢✜ ✙ ✟ ✝ ✙ ✑
  • Object Adapter
slide-35
SLIDE 35

Object Adapter

import java.util.Vector; public class BookListObjectAdapter implements BookList { private Vector books = new Vector(); public int getNumBooks() { 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); } }

BookListObjectAdapter.java

slide-36
SLIDE 36
✂ ✄ ✄☎ ✆ ✝ ✞ ☎ ✟ ✠ ✄ ✡ ✝ ✟ ☛ ☞ ✌ ✍ ✎ ✏ ✍ ✑
☛ ☛ ✓ ✔ ✕ ✕ ✖ ✖✘✗ ✙ ✝ ✗ ✚ ✂ ✄ ✄☎ ✝ ✂ ✗ ✙✛ ✕✢✜ ✙ ✟ ✝ ✙ ✑
  • // 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”); } }

SampleClient.java

slide-37
SLIDE 37
✂ ✄ ✄☎ ✆ ✝ ✞ ☎ ✟ ✠ ✄ ✡ ✝ ✟ ☛ ☞ ✌ ✍ ✎ ✏ ✍ ✑
☛ ☛ ✓ ✔ ✕ ✕ ✖ ✖✘✗ ✙ ✝ ✗ ✚ ✂ ✄ ✄☎ ✝ ✂ ✗ ✙✛ ✕✢✜ ✙ ✟ ✝ ✙ ✑
  • General Form of Object Adapter
slide-38
SLIDE 38
✂ ✄ ✄☎ ✆ ✝ ✞ ☎ ✟ ✠ ✄ ✡ ✝ ✟ ☛ ☞ ✌ ✍ ✎ ✏ ✍ ✑
☛ ☛ ✓ ✔ ✕ ✕ ✖ ✖✘✗ ✙ ✝ ✗ ✚ ✂ ✄ ✄☎ ✝ ✂ ✗ ✙✛ ✕✢✜ ✙ ✟ ✝ ✙ ✑
  • 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

slide-39
SLIDE 39 ✁ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎ ✒
☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒
  • 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

  • f system, use façade object to provide interface

to subsystem as a whole

slide-40
SLIDE 40
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎ ✒
☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒
  • General Idea

client classes subsystem classes Facade

slide-41
SLIDE 41
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎ ✒
☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒
  • The Façade Pattern
  • Example

– A student information system provides access to functions on

✁ ✂ ✄✆☎ ✝✟✞ ✝✡✠ ☛✆☞✌ ✝ ✞ ☎ ✍ ☞ ☞✌ ✍ ✎ ✏✆☞ ☛ ✑ ✌ ☎ ✒ ✑✔✓ ☞✌ ✕ ✎ ✠ ✍ ✞ ☞ ✖ ✂ ✄✆☎ ✝ ✕ ✎ ✠ ✍ ✞ ☞ ✞ ☎ ✒ ✑✔✓ ☞✌ ✞ ✝ ✠ ☛✆☞✌ ✝ ✑ ✞ ☞✌ ✍ ✎ ✏✆☞ ☛ ✑ ✌ ✖ ✂ ✄✆☞ ✍ ☞ ☎ ✒ ✑✔✓ ☞✌ ✕ ✎ ✠ ✍ ✞ ☞ ✑ ✞ ✗ ☞ ✑ ✌ ✒ ✄✆☞ ✏ ☛

– Implemented via three classes

✖ ✘ ✎ ✠ ✍ ✞ ☞ ✞ ✙ ✑ ✌ ✚ ✎ ✍ ✛☎ ✝ ✑ ✎ ✌ ✎ ✌ ✕ ✎ ✠ ✍ ✞ ☞ ☞✌ ✍ ✎ ✏ ✏ ✛☞✌ ✝ ✞ ✖ ✜ ✝✡✠ ☛✆☞✌ ✝ ✞ ✙ ✑ ✌ ✚ ✎ ✍ ✛☎ ✝ ✑ ✎ ✌ ✎ ✌ ✞ ✝ ✠ ☛✆☞✌ ✝ ✞ ✢ ✕ ✎ ✠ ✍ ✞ ☞ ✞ ✖ ✣ ☎ ✕ ✑ ✏ ✑ ✝ ✑ ☞ ✞ ✙ ✑ ✌ ✚ ✎ ✍ ✛☎ ✝ ✑ ✎ ✌ ✎ ✌ ✏ ✎ ✕ ☎ ✝ ✑ ✎ ✌ ✎ ✚ ✕ ✎ ✠ ✍ ✞ ☞ ✞
slide-42
SLIDE 42
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✁ ✒ ✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒ ✁ ✒

Student Information System Classes

class Courses { … public StudentList getEnrollment (CourseId c) { … } … } class Students { … public CourseList getCourses (StudentId s) { … } } class Facilities { … public LectureHallId getLectureHall (CourseId c) { … } }

Courses.java Students.java Facilities.java

slide-43
SLIDE 43
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✁ ✒ ✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒ ✁ ✒

Student Information System without Façade

Student Information Subsystem

slide-44
SLIDE 44
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✁ ✒ ✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒ ✁ ✒

Student Information System with Façade

Student Information Subsystem

slide-45
SLIDE 45
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✁ ✒ ✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒ ✁ ✒

Façade Implementation

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); } }

StudentInformationSystem.java

slide-46
SLIDE 46
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✁ ✒ ✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒ ✁ ✒

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

✁ ✂☎✄ ✆ ✝ ✞✟ ✠ ✡ ✆ ✡ ✄ ☛ ✆ ☞ ✟ ✌ ✞ ✍ ✞ ✆ ✌ ✟ ✞ ✞ ✄ ✎ ✏ ✠ ✞ ✑ ✠ ✒ ✠ ✞✓ ✔ ✆ ✕ ✓ ✆ ✖☎✗ ✓ ✆ ✟ ✠ ✍ ✄ ☛ ✡ ✓ ✘ ✘✙ ✓ ✗ ✆ ☞ ✟ ☞☎✚ ✌ ✠ ✛ ✠ ✍ ✜ ✆ ✏ ✌ ✞ ✌ ✌ ✢ ✆ ✡ ✠ ✎ ✠ ✘ ✘ ✞
slide-47
SLIDE 47 ✁ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Another Example: SavitchIn

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

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

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

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

slide-50
SLIDE 50

import java.util.Vector; public class BookListClassAdapter extends Vector implements BookList { public int getNumBooks() { return size(); } public Book getBook(int n) { 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); } }

Here is an initial implementation of a BookList

slide-51
SLIDE 51 ✁ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Abstract Factory

  • Adding sorting behavior to the BookList class

– Option 1: Modify BookList so that every time a book is entered, the Vector is sorted

✥ ✦✤✧ ★✩ ✪ ✫✭✬ ✮✯ ✰ ✱ ✲ ✳✴ ✮ ✵ ✯ ✶✷ ✳ ✸ ✬ ✹ ✬ ✬ ✺ ✫ ✱ ✴ ✸ ✮ ✰ ✯ ✴ ✴ ✱ ✸ ✴ ✳ ✰✻ ✥ ✼ ★✽ ✩ ✪ ✾ ✬ ✿❀ ✮ ✳ ✸ ✬ ✹ ✬ ✬ ✺ ✫ ✱ ✴ ✸ ❁ ✯ ❂ ✶ ✬ ✸ ❃ ✳ ✯ ❄ ✯ ✱ ✰ ✯ ❃ ✰ ✳ ✪ ✹ ✬ ✬ ✺ ✫ ✱ ✴ ✸ ❁ ✯ ❂ ❃ ✳ ✿ ✴ ✳ ❅ ✱ ✶ ✬ ✸ ✵ ✳ ❀ ❆ ❀ ✬ ✷ ❀ ✯ ❁✴❈❇ ✬ ✸ ✵ ✳ ❀ ✴ ✿ ❃ ✴ ❂ ✴ ✸ ✳ ❁ ✴ ✬ ✻ ✴ ✯ ❁ ✳ ❆ ❀ ✬ ✷ ❀ ✯ ❁ ❇ ✴ ✬ ✸ ✵ ✯ ✸ ✮ ✵ ✯ ✶✷ ✳ ✱ ✶ ✮ ✰ ✯ ✴ ✴ ✻ ✿ ✶ ✮ ✸ ✱ ✬ ✶ ✯ ✰ ✱ ✸ ❂ ✮ ✬ ✿ ✰ ❅ ✵ ✯ ❄ ✳ ❉ ✱ ❅ ✳ ❀ ✱ ❁ ❆ ✯ ✮ ✸ ✸ ✵ ✯ ✶ ❆ ✰ ✯ ✶ ✶ ✳ ❅ ✪ ❊ ✯ ✶✷ ✳ ❀ ✬ ✻ ❄ ✳ ❀ ✴ ✱ ✬ ✶ ❆ ❀ ✬ ✰ ✱ ✻ ✳ ❀ ✯ ✸ ✱ ✬ ✶ ❇ ❉ ✵ ✳ ❀ ✳ ❅ ✱ ✻ ✻ ✳ ❀ ✳ ✶ ✸ ❄ ✳ ❀ ✴ ✱ ✬ ✶ ✴ ✬ ✻ ✹ ✬ ✬ ✺ ✫ ✱ ✴ ✸ ❉ ✱ ✸ ✵ ❅ ✱ ✻ ✻ ✳ ❀ ✳ ✶ ✸ ✻ ✿ ✶ ✮ ✸ ✱ ✬ ✶ ✯ ✰ ✱ ✸ ❂ ✯ ❆ ❆ ✳ ✯ ❀ ✱ ✶ ❅ ✱ ✻ ✻ ✳ ❀ ✳ ✶ ✸ ❆ ❀ ✬ ✷ ❀ ✯ ❁ ✴ ❋ ❋ ✱ ✶ ✮ ❀ ✳ ✯ ✴ ✳ ✴ ❆ ❀ ✬ ❃ ✰ ✳ ❁ ✴ ❉ ✱ ✸ ✵ ❁ ✯ ✱ ✶ ✸ ✳ ✶ ✯ ✶ ✮ ✳
slide-52
SLIDE 52
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✁ ✒ ✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒ ✁ ✒

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

slide-53
SLIDE 53
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✁ ✒ ✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒ ✁ ✒

BookList variants

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

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?

slide-55
SLIDE 55
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Abstract Factory

slide-56
SLIDE 56
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Abstract Factory

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

BookListFactory

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

BookListFactory.java VectorBookListFactory.java Client.java

slide-58
SLIDE 58
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Extending to Multiple Factories

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.

BookListFactory Client uses VectorBookListFactory SortingVectorBookListFactory <<interface>> BookList uses VectorBookList SortingVectorBookList creates creates

slide-59
SLIDE 59
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Extending to Multiple Factories

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.

BookListFactory Client uses VectorBookListFactory SortingVectorBookListFactory <<interface>> BookList uses VectorBookList SortingVectorBookList creates creates

slide-60
SLIDE 60 ✁ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Extending to Multiple Factories

The client sees an object of type BookListFactory, so does not need to know whether it is using a VectorBookListFactory

  • r a SortingVectorBookListFactory

BookListFactory Client uses VectorBookListFactory SortingVectorBookListFactory <<interface>> BookList uses VectorBookList SortingVectorBookList creates creates

slide-61
SLIDE 61

General Form of Abstract Factory

slide-62
SLIDE 62
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Relating to Quality Attributes

  • Modifiability

– Increased through use of indirection in creation process – Creator does not need to know exactly what type

  • f object is being created
slide-63
SLIDE 63 ✁ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎ ✁ ✒ ✁ ✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✁ ✒ ✁

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
slide-64
SLIDE 64
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎ ✁ ✒ ✁ ✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✁ ✒ ✁

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

an object

  • However, a document of

tens of thousands of characters would require tens of thousands of objects

a p p a r e n t

slide-65
SLIDE 65
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎ ✁ ✒ ✁ ✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✁ ✒ ✁

Flyweight

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

a p p a r e n t a b c d e f g h i k l m n

  • p

q r s t u v w x y z

slide-66
SLIDE 66
✂ ✄ ✄☎ ✆ ✝ ✞ ☎ ✟ ✠ ✄ ✡ ✝ ✟ ☛ ☞ ✌ ✍ ✎ ✏ ✍✑ ✒ ✑ ✓ ☛ ☛ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✝ ✘ ✛ ✂ ✄ ✄☎ ✝ ✂ ✘ ✚✜ ✖✣✢ ✚ ✟ ✝ ✚ ✑ ✒ ✑

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 k l m n

  • p

q r s t u v w x y z

slide-67
SLIDE 67
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

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
slide-68
SLIDE 68
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Relation to Quality Attributes

  • Performance

– Systems with tens/hundreds of thousands or more

  • f 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

  • bjects
slide-69
SLIDE 69

CISC 323 Intro to Software Engineering

Lecture 6-3 Composite, Iterator, and Builder Design Patterns

slide-70
SLIDE 70
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

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

slide-71
SLIDE 71
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Motivation

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

Applicability

  • Use the Composite pattern if…

– You want to represent part-whole hierarchies of

  • bjects.

– 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.

slide-73
SLIDE 73
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Example: Swing Components

  • Component
✁ ✂☎✄ ✆ ✄ ✝✟✞ ✠ ✡ ☛☎☞ ✌ ✞ ✍ ✎✑✏ ✒✓ ✏ ✔✕ ✔ ✖ ✗ ✓ ✘ ✙✚ ✛ ✜ ✢ ✙✤✣ ✖✦✥ ✢ ✜ ✖ ✜ ✚ ✢ ✣ ✣ ✧
  • Leaf (atomic components)
★ ✍✩ ✘ ✖ ✖ ✏ ✔ ★ ✍✪ ✢ ✙ ✕ ✚ ★ ✍ ✎ ✫✤✕ ✜ ✬ ✙ ✏ ✭ ★ ✮
  • Composite (top-level Swing

containers)

★ ✍✯ ✥ ✢ ✒✕ ★ ✍✰ ✛ ✢ ✚ ✏ ✱ ★ ✮

Given a frame containing multiple panes, each with multiple buttons/labels/checkboxes…

  • Call paint() method on frame

causes everything to be painted.

  • Changing the look and feel on the

frame is likewise propagated down.

slide-74
SLIDE 74 ✁ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Participants

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

Structure

Leaf

  • peration()

Client Component

  • peration()

addComponent() removeComponent() getChild() Composite

  • peration()

addComponent() removeComponent() getChild() 0..* 0..* +children

slide-76
SLIDE 76
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Collaborations

  • Collaborations

– Client uses component class interface to interact with objects in composite structure. – If recipient is:

✁ ✂☎✄ ✆ ✝ ✞ ✟ ✄ ✠ ✡ ✄ ☛ ☞ ✌ ✆✍ ✎✏ ✄ ✎ ✎ ✑ ✟ ✄ ✒ ☞ ✏ ✓ ✁ ✔✖✕ ✗✘ ✕ ☛ ✑ ☞✙✄ ✞ ✟ ✄ ✠ ✡ ✄ ☛ ☞ ✝ ✕ ✟✚ ✆ ✟ ✎ ✄ ✎ ☞ ✕ ✒ ✌ ✑ ✏ ✎ ✟ ✄ ✍
slide-77
SLIDE 77
✂ ✄ ✄☎ ✆ ✝ ✞ ☎ ✟ ✠ ✄ ✡ ✝ ✟ ☛ ☞ ✌ ✍ ✎ ✏ ✍✑ ✒ ✑ ✓ ☛ ☛ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✝ ✘ ✛ ✂ ✄ ✄☎ ✝ ✂ ✘ ✚✜ ✖✣✢ ✚ ✟ ✝ ✚ ✑ ✒ ✑

Consequences

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

Question?

  • Why is Composite considered a design pattern and

not just an example of polymorphism?

✥ ✦★✧ ✩✪ ✫ ✬✧ ✩✭ ✮✯ ✭ ✬ ✰ ✱ ✧ ✲ ✭ ✧ ✬✳ ✴ ✱ ✵ ✫ ✬ ✱ ✫ ✬✧ ✯ ✭ ✶✸✷ ✮ ✭ ✹ ✯ ✺ ✰ ✬ ✮ ✱ ✭ ✩ ✹ ✧ ✪ ✱ ✧ ✻ ✪ ✹ ✰ ✭ ✫ ✬ ✱ ✷ ✯ ✧ ✬ ✭ ✼ ✭ ✽ ✵ ✧ ✩ ✱ ✬ ✼ ✹ ✭ ✮ ✱ ✺ ✧ ✪ ✩ ✭ ✮ ✮ ✭ ✳ ✰ ✳ ✱ ✧ ✹ ✼ ✪ ✩ ✧✿✾ ✰ ✱ ✪ ✶ ✬ ✭ ✲ ✧ ✬ ✩ ✹ ✰ ✽❀✧ ✬ ✱ ✺ ✧ ✩ ✹ ✧ ✪ ✱ ✰ ✭ ✳ ✭ ✼ ✪ ✩✭ ✳ ✱ ✪ ✰ ✳ ✧ ✹ ✩ ✶ ✪ ✬ ✬ ✱ ✺ ✪ ✱ ✽ ✭ ✱ ✺ ✺ ✭ ✶ ✲ ✬ ✪ ✩ ✭ ✶ ✶ ✧ ✩ ✱ ✰ ✭ ✳ ✭ ✼ ✱ ✺ ✧ ✬✧ ✭ ✽ ✵ ✧ ✩ ✱ ✬ ✪ ✳ ✲ ✪ ✶ ✬ ✭ ✫ ✬✧ ✬ ✱ ✺ ✧ ✬ ✪ ✮ ✧ ✰ ✳ ✱ ✧ ✹ ✼ ✪ ✩ ✧ ✪ ✬ ✱ ✺ ✧ ✭ ✽ ✵ ✧ ✩ ✱ ✬ ✰ ✱ ✺ ✭ ✶ ✲ ✬✿❁ ✥ ❂ ✺ ✪ ✱ ✰ ✬ ✾ ✱ ✺ ✧ ✩✭ ✮✯ ✭ ✳ ✧ ✳ ✱ ✰ ✳ ✱ ✧ ✹ ✼ ✪ ✩ ✧ ✰ ✬ ✳ ✭ ✱ ✰ ✮✯ ✶ ✧ ✮ ✧ ✳ ✱ ✧ ✲ ✽ ✷ ✲ ✰ ✼ ✼ ✧ ✹ ✧ ✳ ✱ ✱ ✷ ✯ ✧ ✬ ✭ ✼ ✶ ✧ ✪ ✻ ✧ ✬ ✾ ✽ ✫ ✱ ✪ ✶ ✬ ✭ ✽ ✷ ✱ ✺ ✧ ✩ ✭ ✮✯ ✭ ✬ ✰ ✱ ✧ ❃ ✺ ✰ ✩ ✺ ✺ ✭ ✶ ✲ ✬ ✶ ✧ ✪ ✻ ✧ ✬ ❁ ❂ ✺ ✧ ✹ ✧ ✼ ✭ ✹ ✧✿✾ ✱ ✺ ✧ ✩ ✶ ✰ ✧ ✳ ✱ ✩✪ ✳ ✫ ✬✧ ✱ ✺ ✧ ✯ ✹ ✰ ✮ ✰ ✱ ✰ ✻ ✧ ✶ ✧ ✪ ✼ ✪ ✳ ✲ ✱ ✺ ✧ ✩✭ ✮✯ ✭ ✬ ✰ ✱ ✧ ✩✭ ✶ ✶ ✧ ✩ ✱ ✰ ✭ ✳ ✭ ✼ ✶ ✧ ✪ ✻ ✧ ✬ ✰ ✳ ✱ ✧ ✹ ✩ ✺ ✪ ✳ ❄ ✧ ✪ ✽✶✸✷ ✾ ✪ ✳ ✲ ✧ ✪ ✬ ✰ ✶✸✷ ✽ ✫ ✰ ✶ ✲ ✩✭ ✮✯ ✶ ✧ ❅ ✬ ✱ ✹ ✫ ✩ ✱ ✫ ✹ ✧ ✬ ✹ ✧ ✩ ✫ ✹ ✬ ✰ ✻ ✧ ✶ ✷ ❁
slide-79
SLIDE 79
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

The Iterator Pattern

[Gamma et al, pp 257-271]

✁ ✂ ✪ ✳ ✷ ✬ ✱ ✹ ✫ ✩ ✱ ✫ ✹ ✧ ✬ ✹ ✧ ✄ ✫ ✰ ✹ ✧ ✬ ✫ ✯ ✯ ✭ ✹ ✱ ✼ ✭ ✹ ✰ ✱ ✧ ✹ ✪ ✱ ✰ ✳ ❄ ✭ ✻ ✧ ✹ ✪ ✶ ✶ ✧ ✶ ✧ ✮ ✧ ✳ ✱ ✬ ✭ ✼ ✱ ✺ ✧ ✬ ✱ ✹ ✫ ✩ ✱ ✫ ✹ ✧ ✁ ☎★✭ ✹ ✧ ❅ ✪ ✮✯ ✶ ✧✿✾ ✰ ✱ ✧ ✹ ✪ ✱ ✰ ✳ ❄ ✭ ✻ ✧ ✹ ✩✭ ✳ ✱ ✧ ✳ ✱ ✬ ✭ ✼ ✪ ✻ ✧ ✩ ✱ ✭ ✹✆

for (int i=0; i < v.size(); i++) { … Object o = v.elementAt(i); … }

Move to next element in list Current element First element Last element

slide-80
SLIDE 80 ✁ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

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, …
slide-81
SLIDE 81
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Iterators in Java

✥ ✦★✧ ✩ ✧ ✪ ✫ ✩ ✬ ✭ ✮ ✯★✰ ✧ ✱ ✬ ✰ ✲ ✯ ✳ ✩✴ ✯ ✮ ✩ ✬ ✭ ✮ ✯★✰ ✧ ✵ ✪ ✧ ✶ ✮ ✯★✰ ✧ ✭ ✷ ✯ ✮✹✸ ✮ ✺ ✭ ✮ ✭ ✻ ✴ ✮ ✬ ✭ ✶ ✮ ✴ ✮ ✺ ✩ ✪ ✧ ✳ ✩ ✬ ✷ ✸ ✯ ✧ ✼ ✶ ✰ ✧ ✮ ✭ ✯ ✧ ✩ ✬ ✮✹✸ ✱ ✩

public interface Enumeration { // Returns true as long as there are more // elements to visit boolean hasMoreElements(); // Returns next element to visit Object nextElement(); }

✽ ✾ ✭ ✶ ✺ ✩ ✷ ✩ ✫ ✩ ✧ ✮ ✿ ✯ ✷ ✷ ✻ ✩ ✬ ✩ ✮ ✪ ✬ ✧ ✩ ✳ ✻ ✸ ❀❁ ❂ ❃ ❄ ❅ ❁ ❆ ❁ ❀ ❃ ✩ ❇ ✭ ✶ ✮ ✷ ✸ ✰ ✧ ✶ ✩ ✽ ❈ ✬ ✳ ✩ ✬ ✰ ✵ ✲ ✯ ✴ ✯ ✮ ✭ ✮ ✯ ✰ ✧ ✧ ✰ ✮ ✴ ✱ ✩ ✶ ✯ ✵ ✯ ✩ ✳
slide-82
SLIDE 82 ✁ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✁ ✒ ✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✒ ✁ ✒

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();

slide-83
SLIDE 83 ✁ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎ ✁ ✒ ✁ ✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✁ ✒ ✁

Example: Using an Iterator with a Vector

slide-84
SLIDE 84
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎ ✁ ✒ ✁ ✓ ☞ ☞ ✔ ✕ ✖ ✖ ✗ ✗✙✘ ✚ ✞ ✘ ✛ ✄ ☎ ☎✆ ✞ ✄ ✘ ✚✜ ✖✣✢ ✚ ✠ ✞ ✚ ✁ ✒ ✁

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(); … }

slide-85
SLIDE 85
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

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

slide-86
SLIDE 86 ✁ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Classes for Vector Iterator

slide-87
SLIDE 87

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.)

slide-88
SLIDE 88

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; } }

slide-89
SLIDE 89
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

General Form of Iterator

Abstract representation

  • f container

Concrete representation

  • f container - e.g. Vector

Concrete iterator for specific container class - e.g. iterator for Vector is different from iterator for hashtable Enumeration interface

slide-90
SLIDE 90
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

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

slide-91
SLIDE 91
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Builder Design Pattern

[Gamma et. al. pp. 97-106]

  • Intent

– Separate the construction of a complex object from its representation

slide-92
SLIDE 92
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

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

slide-93
SLIDE 93
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Participants

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

Example: JDOM SAXBuilder

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

Structure

ConcreteBuilder buildPart() getProduct() Product Director Builder buildPart() <<creates>>

slide-96
SLIDE 96
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Collaborations

  • The client creates the Director object and

configures it with the desired Builder object

  • Director notifies the Builder whenever a part
  • f 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

slide-97
SLIDE 97
✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Collaborations (cont’d)

aClient : Client aDirector : Director aConcreteBuilder : ConcreteBuilder new ConcreteBuilder() new Director(aConcreteBuilder) construct() buildPartA() buildPartB() buildPartC() getResult()

slide-98
SLIDE 98
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

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

slide-99
SLIDE 99
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Relationship between Composite, Iterator, and Builder

  • Builder, Composite and Iterator Design Patterns often

work together

✂☎✄ ✆ ✂ ✝ ✞✟ ✆ ✂☎✠ ✆ ✡☞☛ ✝ ✌✍ ✄ ✎ ✏✑☛ ✝ ✌✍ ✒ ✝ ✒ ✓ ✔ ✆ ✄ ✞ ✠ ✕ ✓ ✖✗ ✓ ✒ ✝ ✆ ✄ ✘ ✕ ✓ ✖✗ ✓ ✒ ✝ ✆ ✄ ✒ ✝ ✞ ✆ ☛ ✎ ✞ ☛ ✒ ✄ ✆ ✂☎✄ ✙ ✆ ✄ ✎ ✠ ✆ ✓ ✎ ✆ ✓ ✆ ✎ ✠ ✚ ✄ ✎ ✒ ✄ ✆ ✂☎✄ ✝ ✎ ✛ ✂ ✝ ✌✍ ✎ ✄ ✞
slide-100
SLIDE 100
✁ ✂ ✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Summary of Patterns Discussed…

  • Creational Patterns:
✘ ✥ ✏ ✒ ✆ ✎ ✠ ✛ ✆ ✦ ✠ ✛ ✆ ✓ ✎✧ ✘ ✡☞☛ ✝ ✌✍ ✄ ✎
  • Structural Patterns:
✘ ✥ ✍ ✠ ✗ ✆ ✄ ✎ ✘ ✕ ✓ ✖✗ ✓ ✒ ✝ ✆ ✄ ✘ ✦ ✠ ★ ✠ ✍ ✄ ✘ ✦ ✌ ✧ ✩ ✄ ✝ ✟ ✂ ✆
  • Behavioural Patterns:
✘ ✙ ✆ ✄ ✎ ✠ ✆ ✓ ✎ ✘ ✪ ✏ ✒ ✄ ✎ ✚ ✄ ✎
slide-101
SLIDE 101
✄ ☎ ☎✆ ✝ ✞ ✟ ✆ ✠ ✡ ☎ ☛ ✞ ✠ ☞ ✌ ✍ ✎ ✏ ✑ ✎✒ ✓ ✒ ✔ ☞ ☞ ✕ ✖ ✗ ✗ ✘ ✘✚✙ ✛ ✞ ✙ ✜ ✄ ☎ ☎✆ ✞ ✄ ✙ ✛✢ ✗✤✣ ✛ ✠ ✞ ✛ ✒ ✓ ✒

Final Points

  • Design Patterns encapsulate generic solutions to

commonly occurring problems in design

✝ ✆ ✆ ✄ ✞ ✔ ✎ ✓ ✖ ✄ ✁ ✗ ✄ ✎ ✝ ✄ ✞ ✛ ✄ ✝ ✞ ✖ ☛ ✌ ✆ ✝ ✗ ✌☎✄ ✛ ✓ ✞ ✆ ✄ ✁ ✆ ✒
  • The Gang of Four text was the first widely read work
  • n patterns, and remains a widely cited and used

reference

  • Patterns rarely exist on their own, they often work

together to solve complex problems

  • There are always costs and benefits to applying a

given pattern, which must be carefully weighed.