Вы находитесь на странице: 1из 62

LECTURE ON DESIGN PATTERNS

Lecture Outline
TOPIC SLIDE NO.
Design Architecture
o o

MVC Architecture - 3 3-tier/3-layer Architecture - 4 Definition & Organization - 5-6 Singleton Pattern - 7-11 Composite Pattern - 12-16 State Pattern - 17-20 Strategy Pattern - 21-25 Observer Pattern - 26-29 Facade Pattern - 30-32 Decorator Pattern - 33-39 Factory Method Pattern - 40-44 Adapter Pattern - 45-48 Template Method Pattern - 49-51 Builder Pattern - 52-56 Proxy Pattern - 57-60

Design Patterns
o o o o o o o o o o o o o

Design Architecture
A. MVC ARCHITECTURE

Design Architecture
B. 3-TIER/ 3-LAYER ARCHITECTURE

Design Patterns
Definitions: - A formal way of documenting a solution to a design problem in a particular field of expertise (Wikipedia).

- Provides a set of ideas about how to solve the problems that arise as you program.

- or simply as Design + Pattern.

Design Patterns
Patterns are organized into:
Creational Patterns abstracts the instantiation process. They help make a system independent of how its objects are created, composed and represented.
Examples : Abstract Factory, Builder, Factory Method, Prototype, Singleton

Structural Patterns concerned with how classes and objects are composed to form larger structures. Handles the structure of the classes.
Examples: Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy

Behavioral Patterns concerned with algorithms and the assignment of responsibilities between objects.
Examples: Chain of Responsibilities, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method, Visitor

Design Patterns
A. SINGLETON PATTERN

INTENT Ensures a class only has one instance, and provides a global point of access to. MOTIVATION It is important for some class to have exactly one instance and that it is easily accessible. PARTICIPANTS Singleton defines an instance operation that lets clients access its unique instance.

Design Patterns
A. SINGLETON PATTERN

SAMPLE DIAGRAM 1

Design Patterns
A. SINGLETON PATTERN

SAMPLE DIAGRAM 1S SOURCE CODE

Design Patterns
A. SINGLETON PATTERN

SAMPLE DIAGRAM 2

Design Patterns
A. SINGLETON PATTERN

SAMPLE DIAGRAM 2S SOURCE CODE

Design Patterns
B. COMPOSITE PATTERN

INTENT Compose object into tree structures to represent part-whole hierarchies.. MOTIVATION The user can group components to form larger components, which in turn can be grouped to form still larger components. PARTICIPANTS Component Declares the interface for objects in the composition. Leaf represents leaf objects in the composition. Composite Defines behavior for components having children Client manipulates objects in the composition through the component interface.

Design Patterns
B. COMPOSITE PATTERN

SAMPLE DIAGRAM 1

Design Patterns
B. COMPOSITE PATTERN
SAMPLE DIAGRAM 1S SOURCE CODE

Design Patterns
B. COMPOSITE PATTERN

SAMPLE DIAGRAM 2

Design Patterns
B. COMPOSITE PATTERN
SAMPLE DIAGRAM 2S SOURCE CODE

Design Patterns
C. STATE PATTERN

INTENT Allow an object to alter its behavior when its internal state changes. The object will appear to change its class. MOTIVATION An object can have different state and reacts differently depending on its current state. PARTICIPANTS Context defines the interface of interest to clients. State defines an interface for encapsulating the behavior associated with particular state of the context. Concrete State implements the behavior associated with a state of a context.

Design Patterns
C. STATE PATTERN

SAMPLE DIAGRAM 1

Design Patterns
C. STATE PATTERN
SAMPLE DIAGRAM 1S SOURCE CODE

Design Patterns
C. STATE PATTERN
SAMPLE DIAGRAM 1S SOURCE CODE (contd)

Design Patterns
D. STRATEGY PATTERN

INTENT Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. MOTIVATION Different algorithms will be appropriate at different times. PARTICIPANTS Strategy declares an interface common to all supported algorithm. Concrete Strategy implements the algorithm using the strategy interface Context - maintains a reference to a strategy object.

Design Patterns
D. STRATEGY PATTERN

SAMPLE DIAGRAM 1

Design Patterns
D. STRATEGY PATTERN
SAMPLE DIAGRAM 1S SOURCE CODE

Design Patterns
D. STRATEGY PATTERN

SAMPLE DIAGRAM 2

Design Patterns
D. STRATEGY PATTERN
SAMPLE DIAGRAM 2S SOURCE CODE

Design Patterns
E. OBSERVER PATTERN

INTENT Define a one-to-many dependency between objects so that when one object changes state, all its dependent are notified and updated automatically. MOTIVATION Some objects need to be notified when a certain event happens PARTICIPANTS Subject Knows its observers. Provides an interface for attaching and detaching observer objects. Observer defines an updating interface for objects that should be notified of changes in a subject. Concrete Subject sends notification to its observers when its state changes. Concrete observers implements the observer updating interface to keep its state consistent with the subject.

Design Patterns
E. OBSERVER PATTERN

SAMPLE DIAGRAM 1

Design Patterns
E. OBSERVER PATTERN
SAMPLE DIAGRAM 1S SOURCE CODE

Design Patterns
E. OBSERVER PATTERN
SAMPLE DIAGRAM 1S SOURCE CODE (Contd) SAMPLE USAGE

Design Patterns
F. FACADE PATTERN

INTENT Provide a unified interface to a set of interfaces in a subsystem. Faade defines a higher-level interface that makes the subsystem easier to use. MOTIVATION Structuring a system into a subsystem helps reduce complexity PARTICIPANTS Faade knows which subsystem classes are responsible for a request. Subsystem classes implement subsystem functionality.

Design Patterns
F. FACADE PATTERN

SAMPLE DIAGRAM 1

SAMPLE DIAGRAM 1S SOURCE CODE

Design Patterns
F. FACADE PATTERN

Design Patterns
G. DECORATOR PATTERN

INTENT Attach additional responsibilities to an object dynamically. Decorators provide a flexible Alternative to sub classing for extending functionality. MOTIVATION Sometimes we want to add responsibilities to individual objects, not to an entire class. PARTICIPANTS Component defines the interface for objects that can be decorated. Concrete Component defines the actual object Decorator maintains a reference to a component object and defines an interface that conforms to components interface Concrete Decorator adds responsibilities to the component.

Design Patterns
G. DECORATOR PATTERN
SAMPLE DIAGRAM 1

Design Patterns
G. DECORATOR PATTERN
SAMPLE OUTPUT Option Window Treeview Decorator Property Decorator

Design Patterns
G. DECORATOR PATTERN
SAMPLE CODE (S)AND OUTPUT

Design Patterns
G. DECORATOR PATTERN
SAMPLE CODE (S)AND OUTPUT

Design Patterns
G. DECORATOR PATTERN
SAMPLE CODE (S)AND OUTPUT

Design Patterns
G. DECORATOR PATTERN Decorator vs Inheritance
The decorator pattern allows you to add behavior dynamically at runtime to an instance of a particular class by attaching and detaching them, whereas inheritance adds behavior at compile time.

Design Patterns
H. FACTORY METHOD PATTERN

INTENT Define an interface for creating an object, but let subclasses decide which class to instantiate. MOTIVATION Needs a base class for defining and maintaining relations between objects and must be flexible. PARTICIPANTS Product defines the interfaces of the object the factory method creates. Concrete Product implements the Product interface Creator declares the factory method, creates the product. Concrete Creator overrides the factory method to return an instance of a concrete product.

Design Patterns
H. FACTORY METHOD PATTERN
SAMPLE DIAGRAM 1

Design Patterns
H. FACTORY METHOD PATTERN
SAMPLE DIAGRAM 1S SOURCE CODE

Design Patterns
H. FACTORY METHOD PATTERN
SAMPLE DIAGRAM 1S SOURCE CODE (contd)

Design Patterns
H. FACTORY METHOD PATTERN
SAMPLE DIAGRAM 1S SOURCE CODE (contd)

Design Patterns
I. ADAPTER PATTERN

INTENT Convert the interface of a class into another interface clients expected. Adapter lets classes work together that couldnt otherwise because of incompatible interfaces. MOTIVATION Sometimes a class thats designed for reuse isnt reusable only because its interface or API doesnt match with the need of the application. PARTICIPANTS Target defines the domain-specific interface that client uses Client collaborates with objects confirming to the target interface Adaptee defines an existing interface that needs adapting Adapter adapts the interface of Adaptee to the Target Interface

Design Patterns
I. ADAPTER PATTERN
SAMPLE DIAGRAM 1

Design Patterns
I. ADAPTER PATTERN
CONCRETE EXAMPLE

Target Renderer Adapters GDIRenderer, SDLRenderer Client GameEngine2D Adaptee GDI32.Dll, SDL library

Design Patterns
I. ADAPTER PATTERN

Design Patterns
J. TEMPLATE METHOD PATTERN

INTENT Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. MOTIVATION subclasses redefine certain steps of an algorithm without changing the algorithms structure. PARTICIPANTS Abstract class - defines the abstract operations that the concrete subclass define to implement steps of an algorithm. Concrete class implements the primitive operations to carry out subclassspecific steps of the algorithm.

Design Patterns
J. TEMPLATE METHOD PATTERN
SAMPLE DIAGRAM 1

Design Patterns
J. TEMPLATE METHOD PATTERN
Example 1 Example 2

Design Patterns
K. BUILDER PATTERN

INTENT Separate the construction of a complex object so that the same construction process can create different representation. MOTIVATION Some construction process of a complex object differs in detail. PARTICIPANTS Director constructs an object using the builder interface Builder specifies an abstract interface for creating parts of a product. Product represents the complex object under construction Concrete Builder defines the representation of the object by implementing the interfaces.

Design Patterns
K. BUILDER PATTERN
SAMPLE DIAGRAM 1

Design Patterns
K. BUILDER PATTERN
SAMPLE DIAGRAM 1S SOURCE CODES

Design Patterns
K. BUILDER PATTERN
SAMPLE DIAGRAM 1S SOURCE CODES (contd)

Design Patterns
K. BUILDER PATTERN
SAMPLE DIAGRAM 1S SOURCE CODES (contd)

Design Patterns
L. PROXY PATTERN

INTENT Provide a surrogate or placeholder for another object to control access to it. MOTIVATION Sometimes we need the ability to control the access to an object. PARTICIPANTS Proxy provides an interface identical to Subject so that a proxy can be substituted for the real object. Subject defines the real object the proxy represent.

Design Patterns
L. PROXY PATTERN
SAMPLE DIAGRAM 1 C#

*C#

loads DLL and functions using DLLImport *Java loads DLL and functions using System.loadLibrary or Runtime.getRuntime().load

windows DLL

Design Patterns
L. PROXY PATTERN
SAMPLE DIAGRAM 1s SOURCE CODE

Design Patterns
L. PROXY PATTERN
SAMPLE DIAGRAM 2

Design Patterns
L. PROXY PATTERN
SAMPLE DIAGRAM 2s SOURCE CODE

References
Design Patterns Element of Reusable ObjectOriented Software by Eric Gamma, Richard Helm, Ralph Johnson, John Vlissides ISBN-13: 978-0-201-63361-0 Software Engineering for Game Developers by John P. Flynt, PH.D. with Omar Salem ISBN: 1-59200-155-6 http://www.oodesign.com/ http://en.wikipedia.org/wiki/Design_Patterns_(book)

Вам также может понравиться