Design Patterns - 01 Introduction and Decorator Pattern

17
01 Introduction & Decorator Pattern Design Patterns © Prafulla Paraskar 2010

Transcript of Design Patterns - 01 Introduction and Decorator Pattern

Page 1: Design Patterns - 01 Introduction and Decorator Pattern

© Prafulla Paraskar 2010

01 Introduction & Decorator Pattern

Design Patterns

Page 2: Design Patterns - 01 Introduction and Decorator Pattern

Introduction

Page 3: Design Patterns - 01 Introduction and Decorator Pattern

DESIGN PATTERNS - DEFINITION Definition:

a general reusable solution to a commonly occurring problem in software design (Wikipedia).

Not a finished design Description or template for how to solve a

problem that can be used in many different situations

Shows relationships and interactions between classes and/or objects

Not all software patterns are design patterns (e.g. algorithms)

Page 4: Design Patterns - 01 Introduction and Decorator Pattern

DESIGN PATTERNS – CLASSIFICATION

Structural Pattern Ease the design by identifying a simple way to realize

relationships between entities E.g. Decorator, Proxy

Creational Pattern Deal with object creation mechanism E.g. Abstract Factory, Singleton

Behavioral Pattern Deal with common communication between objects. E.g. Chain of Responsibility, Command

Concurrency Pattern Deal with multi-threaded programming paradigm. E.g. Monitor Object, Thread Pool Not covered during this series

© Prafulla Paraskar 2010

Page 5: Design Patterns - 01 Introduction and Decorator Pattern

DESIGN PATTERNS – CLASSIFICATIONStructural Patterns

• 1. Decorator• 2. Proxy• 3. Bridge• 4. Composite• 5. Flyweight• 6. Adapter• 7. Facade

Creational Patterns• 1. Prototype• 2. Factory Method• 3. Singleton• 4. Abstract

Factory• 5. Builder

Behavioral Patterns• 1. Strategy• 2. State• 3.

TemplateMethod• 4. Chain of

Responsibility• 5. Command• 6. Iterator• 7. Mediator• 8. Observer• 9. Visitor• 10. Interpreter• 11. Memento

Page 6: Design Patterns - 01 Introduction and Decorator Pattern

UML CLASS DIAGRAM NOTATION (1/3)

Page 7: Design Patterns - 01 Introduction and Decorator Pattern

UML CLASS DIAGRAM NOTATION (2/3)

© Prafulla Paraskar 2010

Page 8: Design Patterns - 01 Introduction and Decorator Pattern

UML CLASS DIAGRAM NOTATION (3/3)

© Prafulla Paraskar 2010

Page 9: Design Patterns - 01 Introduction and Decorator Pattern

DecoratorStructural Design Pattern

Page 10: Design Patterns - 01 Introduction and Decorator Pattern

DEFINITION

In object-oriented programming, the decorator pattern is a design pattern that allows new/additional behaviour to be added to an existing object dynamically.

© Prafulla Paraskar 2010

Page 11: Design Patterns - 01 Introduction and Decorator Pattern

DECORATOR PATTERN – EXPLAINED (1/2)

Photo+

Frame+

CaptionGuptas (2010)

© Prafulla Paraskar 2010

Page 12: Design Patterns - 01 Introduction and Decorator Pattern

DECORATOR PATTERN – EXPLAINED (2/2) Adds functionality at Runtime.

The object does not know it is being “decorated”.

Three is no one big feature-laden class with all the options in it.

The decorations are independent of each other.

The decorations can be composed together in a mix-and-match fashion.

© Prafulla Paraskar 2010

Page 13: Design Patterns - 01 Introduction and Decorator Pattern

DECORATOR PATTERN – UML DIAGRAM

© Prafulla Paraskar 2010

Page 14: Design Patterns - 01 Introduction and Decorator Pattern

DECORATOR PATTERN – REAL WORLD SAMPLES Graphics world (as illustrated). I/O namespace of .NET

System.IO.Stream System.IO.BufferedStream System.IO.FileStream System.IO.MemoryStream

Cross platform applications (Mobile/Desktop) Actual decorator classes in .NET 3.0

System.Windows.Controls (Base Class) Border (Decorator) Viewbox (Decorator)

© Prafulla Paraskar 2010

Page 15: Design Patterns - 01 Introduction and Decorator Pattern

DECORATOR PATTERN – GUIDELINES (1/2) You have:

An existing component class that may be unavailable for subclassing.

You want to: Attach additional state or behavior to an object

dynamically. Make Changes to some objects of a class without affecting

others. Avoid subclassing because too many classes could result.

© Prafulla Paraskar 2010

Page 16: Design Patterns - 01 Introduction and Decorator Pattern

But consider using instead: The Adapter Pattern

Sets up an interface between different classes The Composite Pattern

Aggregates an object without also inheriting its interface. The Proxy Pattern

Specifically controls access to objects. The Strategy Pattern

Changes the original object rather than wrapping it.

DECORATOR PATTERN – GUIDELINES (2/2)

© Prafulla Paraskar 2010