You are on page 1of 31

Object-Oriented Analysis and Design

Design Patterns
Introduction to Design Patterns

Week #9 Jarungjit Parnjai

Burapha University, 2001

Object-Oriented Analysis and Design

Lecture Outline
What is A Pattern? Why Patterns? Software Patterns GoF Design Patterns Singleton (Object Creational Design Patterns)

Burapha University, 2001

Object-Oriented Analysis and Design

What is A Pattern?
A Pattern
Christopher Alexander Alexander Each pattern is a three-part rule, which expresses a relation between a certain context, a problem and a solution. definition : A solution to a problem in a context

Burapha University, 2001

Object-Oriented Analysis and Design

A Solution to a Problem in a Context


Context Problem

Context

Solution

Burapha University, 2001

Object-Oriented Analysis and Design

Why Patterns?
Erich Gamma
Designing object-oriented software is hard and designing reusuable object-oriented software is even harder. Solution Pattern Class Object Pattern
Burapha University, 2001

Object-Oriented Analysis and Design

Software Patterns History


1987 - CunningHam Beck Alexander Pattern Language Smalltalk 1990 - Gang of Four (Gamma, Helm, Johnson Vlissides ; GoF) catalog design pattern 1991 - Bruce Anderson Patterns Workshop OOPSLA 1993 - Kent Back Grady Booch meeting Hillside Group 1994 - Pattern Languages of Programs (PLoP) 1995 - GoF Design Pattern
Burapha University, 2001

Object-Oriented Analysis and Design

Types of Software Patterns


Analysis Design Organizational Process Project Planning Configuration Management

Burapha University, 2001

Object-Oriented Analysis and Design

Types of Software Patterns


Software Pattern 3 (Riehel Zullighoven) Conceptual Pattern
Pattern whose form is described by means of terms and concepts from the application domain.

Design Pattern Programming Pattern


Pattern whose form is described by means of software design constructs such as objects. Classes, inheritance and aggregation. Pattern whose form is described by means of programming langugage
Burapha University, 2001

Object-Oriented Analysis and Design

Design Patterns Levels of Abstraction


Complex design for an entire application or subsystem Solution to a general design problem in a particular context
More Abstract More Concrete

Simple reusable design class such as a linked list, hash table, etc.
Burapha University, 2001

Object-Oriented Analysis and Design

GoF Classification of Design Patterns


GoF Design Patterns Levels of Abstraction A design pattern names, abstracts, and identifies key aspects of a common design structure that makes it useful for creating a reusable object-oriented design. GoF Design Pattern description of communicating objects and classes that are customized to solve a general design problem in a particular context.

Burapha University, 2001

Object-Oriented Analysis and Design

GoF Design Patterns


Purposes - what a pattern does Creational Patterns Object Structural Patterns Class Object Behavioral Patterns Class Object Scope - what the pattern applies to Class Patterns Class Subclass Inheritance Object Patterns Object Composition
Burapha University, 2001

Object-Oriented Analysis and Design

GoF Design Patterns


Purpose Scope Class Creational Factory Method Structural Adapter Behavioral Interpreter Template Method

Object

Abstract Factory Builder Prototype Singleton

Abstract Bridge Composite Faade Flyweight Proxy

Chain of Responsibility Command Iterator Mediator Memento State Strategy

Burapha University, 2001

Object-Oriented Analysis and Design

Design for Change


Creating an object by specifying a class explicitly Abstract Factory, Factory Method, Prototype Dependence on specific operation Chain of Responsibility, Command Dependence on h/w and s/w platforms Abstract Factory, Bridge Dependence on object representations or implementations Abstract Factory, Bridge, Memento, Proxy Algorithm Dependencies Builder, Iterator, Strategy, Template Method, Visitor
Burapha University, 2001

Object-Oriented Analysis and Design

Design for Change (Continued)


Tight Coupling Abstract factory, Bridge, Chain of Responsibility, Command, Faade, Mediator, Observer Extending functionality of subclassing Bridge, Chain of Responsibility, Composite, Decorator, Observer, Strategy Inability to alter classes conveniently Adapter, Decorator, Visitor

Burapha University, 2001

Object-Oriented Analysis and Design

GoF Essential Elements of Design Patterns


Pattern Name Problem problem context Pattern Pattern Solution Element Design Pattern relationship responsibilities collaboration concrete design implemenation abstract description Consequences Pros Cons pattern
Burapha University, 2001

Object-Oriented Analysis and Design

GoF Pattern Template


Pattern Name Classification Pattern Pattern Intent pattern Also Known As Pattern Motivation Pattern Applicability implement
Burapha University, 2001

Object-Oriented Analysis and Design

GoF Pattern Template (Continued)


Structure pattern Participants Class Object pattern Collaborations participant responsibilities Consequences Pros Cons Pattern Implementation implement
Burapha University, 2001

Object-Oriented Analysis and Design

GoF Pattern Template (Continued)


Sample Code Code implement Known Uses Pattern Relatted Patterns Patterns Pattern

Burapha University, 2001

Object-Oriented Analysis and Design

Singleton Design Pattern

Burapha University, 2001

Object-Oriented Analysis and Design

Singleton
Case Study , , application interface application

Burapha University, 2001

Object-Oriented Analysis and Design

Singleton
First Design approach : global data Encapsulation global data

Burapha University, 2001

Object-Oriented Analysis and Design

Singleton
Second Design approach :
Company - CompanyName - CompanyAddress - CompanyRegistrationNumber

+ getCompanyDetails()

Company Object Identifier ( ) Object Identfier global Encapsulation!!!


Burapha University, 2001

Object-Oriented Analysis and Design

Singleton
Second Design approach :
public class Company { private String name; private String address; private String regNumber; public Company(String n, String a, String r) { name = n; address = a; regNumber = r; } public String getCompanyName() { return name; } public String getCompanyAddress() { return address; } public String getCompanyRegistrationNumber() { return regNumber; } }
Burapha University, 2001

Object-Oriented Analysis and Design

Singleton
Third Design approach :
Company
- CompanyInstance : Company - CompanyName : String - CompanyAddress : String - CompanyRegistrationNumber : int + getCompanyInstance( ) : Comapany + getCompanyDetails() : String

Company instance Class Operation (Static Operation ) Object Identifier Burapha University, 2001

Object-Oriented Analysis and Design

Singleton
Third Design approach :
public class Company { private static Company instance = new Company("Company., Ltd.", "Burapha Unviersity", "99999"); private String name; private String address; private String regNumber; private Company(String n, String a, String r) { name = n; address = a; regNumber = r; } public static Company getCompanyInstance() { return instance; } public String getCompanyName() { return name; } public String getCompanyAddress() { return address; } public String getCompanyRegistrationNumber() { return regNumber; } Burapha University, 2001 }

Object-Oriented Analysis and Design

Singleton
Third Design approach :
public class TestCompany { public static void main(String[] args) { Company instance1 = Company.getCompanyInstance(); if (instance1 == null) { System.out.println("null!!!"); } else { System.out.println("not null!!!"); System.out.println(instance1); System.out.println(instance1.getCompanyName()); System.out.println(instance1.getCompanyAddress()); System.out.println(instance1.getCompanyRegistrationNumber()+"\n"); }
Burapha University, 2001

Object-Oriented Analysis and Design

Company instance2 = Company.getCompanyInstance(); if (instance2 == null) { System.out.println("null!!!"); } else { System.out.println("not null!!!"); System.out.println(instance2); System.out.println(instance2.getCompanyName()); System.out.println(instance2.getCompanyAddress()); System.out.println(instance2.getCompanyRegistrationNumber()); } } }

Burapha University, 2001

Object-Oriented Analysis and Design

not null!!! Company@256a7c Company., Ltd. Burapha Unviersity 99999 not null!!! Company@256a7c Company., Ltd. Burapha Unviersity 99999

Burapha University, 2001

Object-Oriented Analysis and Design

Singleton Design Pattern


Pattern Name Singleton / Object creational Problem class class instance global point instance class Context global data Object Identifier global class operation Object identifier Singleton Pattern instance
Burapha University, 2001

Object-Oriented Analysis and Design

Singleton Design Pattern


Solution class Class Operation instance class instance class load class instance class Class Operation instance class Consequences object instance encapsulate class name space global subclass Singleton class user subclass run-time instance 1 instance
Burapha University, 2001

Object-Oriented Analysis and Design

Summary

What is A Pattern? Why Patterns? Software Patterns GoF Design Patterns Singleton (Object Creational Design Patterns)

Burapha University, 2001

You might also like