PowerPoint Presentation

Document Sample
PowerPoint Presentation Powered By Docstoc
					             Introduction to Design Patterns

• This lecture will introduce the idea of design patterns
   – What they are and how they are structured


• We’ll talk about specific patterns several times this semester
   – Today: Iterator and Factory Method
   – After we’ve studied C++ objects: Adapter, Memento, Observer
   – After we’ve studied C++ memory: Singleton, Prototype, Visitor




  CSE 332: Design Patterns
                What’s a Design Pattern?
• A design pattern has a name
  – So when someone says “Adapter” you know what they mean
  – So you can communicate design ideas as a “vocabulary”


• A design pattern describes the core of a solution to a
  recurring design problem
  – So you don’t have to reinvent known design techniques
  – So you can benefit from others’ (and your) prior experience


• A design pattern is capable of generating many
  distinct design decisions in different circumstances
  – So you can apply the pattern repeatedly as appropriate
  – So you can work through different design problems using it
 CSE 332: Design Patterns
                          Iterator Pattern
• Problem
  – Want to access aggregated elements sequentially
      • E.g., traverse a container of values, objects etc. and print them out
• Context
  – Don’t want to know/manage details of how they’re stored
      • E.g., could be in an array, list, vector, or deque
• Solution core
  – Provide a common interface for iteration over a container:
    (1) start; (2) access; (3) increment; (4) termination
• Consequences
  – Frees user from knowing details of how elements are stored
  – Decouples containers from algorithms (crucial in C++ STL)
• Example
  list<int>::iterator

 CSE 332: Design Patterns
                   Factory Method Pattern
• Problem
  – You want a type to create a related type polymorphically
      • E.g., a container should create appropriate begin and end iterators

• Context
  – Each type knows which related type it should create
• Solution core
  – Polymorphic creation
      • E.g., abstract method that different types override
      • E.g., provide traits and common interface (as in the STL we’ll use)
• Consequences
  – Type that’s created matches type(s) it’s used with
• Example
  vector<double> v; vector<double>::iterator i = v.begin();

 CSE 332: Design Patterns

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:10/2/2012
language:English
pages:4