Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag...

115
Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    0

Transcript of Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag...

Page 1: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Basic Design PatternsBasic Design Patterns

1. Introduction

Gabriel MañanaEd. 453 Of. 120 Ext. 14080

gjmananag @unal.edu.co

Page 2: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Basic Design PatternsBasic Design Patterns

1. IntroductionWhat is a Design Pattern?Design Patterns in MVCDescribing Design PatternsCatalog of Design PatternsSolving Design ProblemsSelecting Design PatternsUsing Design Patterns

Page 3: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Basic Design PatternsBasic Design Patterns

Designing OO software is hard

Designing reusable OO software is even harder

Page 4: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Basic Design PatternsBasic Design Patterns

1. Find pertinent objects

2. Factor objects into classes

3. Define class interfaces

4. Define inheritance hierarchies

5. Establish key relationships

Page 5: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Basic Design PatternsBasic Design Patterns

The design should be specific to the problem at hand but also general enough to address future problems and requirements:

avoid redesign

Page 6: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Basic Design PatternsBasic Design Patterns

The difference between expert and novice designers is experience:

Recurring patterns of classes and communicating objects.

Page 7: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Basic Design PatternsBasic Design Patterns

These patterns solve specific design problems and make OO designs more flexible:

reusable

Page 8: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Basic Design PatternsBasic Design Patterns

Each design pattern systematically names, explains and evaluates an important and recurring design in OO systems.

Page 9: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Basic Design PatternsBasic Design Patterns

Christopher Alexander:

“Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.”

Page 10: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Basic Design PatternsBasic Design Patterns

Essential pattern elements:

1. pattern name

2. problem

3. solution

4. consequences

Page 11: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Basic Design PatternsBasic Design Patterns

1. Pattern Name:

Let us design at a higher level of abstraction.

Page 12: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Basic Design PatternsBasic Design Patterns

2. Problem:

Explains the problem and its context.

describes when to apply the pattern

Page 13: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Basic Design PatternsBasic Design Patterns

3. Solution:

Describes the elements that make up the design, their relationships, responsibilities and collaborations.

Page 14: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Basic Design PatternsBasic Design Patterns

4. Consequences:

Are the results and trade-offs of applying the pattern, its impact on a system’s flexibility, extensibility or portability

Page 15: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Basic Design PatternsBasic Design Patterns

Level of Abstraction?

Description of communicating objects and classes that are customized to solve a general design problem in a particular context.

Page 16: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Basic Design PatternsBasic Design Patterns

The design pattern identifies the participating classes and instances, their roles and collaborations, and the distribution of responsibilities.

Page 17: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Basic Design PatternsBasic Design Patterns

It describes when it applies, whether it can be applied in view of other design constraints, and the consequences and trade-offs of its use. (code)

Page 18: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Smalltalk MVCSmalltalk MVC

Model/View/Controller

Triad of classes used to build user interfaces

pattern

Page 19: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Smalltalk MVCSmalltalk MVC

Model: application object

View: its screen presentation

Controller: describes the way the UI reacts to user input

Page 20: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Smalltalk MVCSmalltalk MVC

MVC: Decouples views and models by establishing a subscribe/notify protocol between them.

(Observer Pattern)

Page 21: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Smalltalk MVCSmalltalk MVC

In MVC views can be nested: CompositeView, subclass of View.

(Composite Pattern)

Page 22: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Smalltalk MVCSmalltalk MVC

MVC also allows to change the way a view responds to user input without changing its visual presentation.

(Controller Pattern)

Page 23: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Smalltalk MVCSmalltalk MVC

View-Controller relationship is an example of the Strategy design pattern.

(Strategy Pattern: an object that represents an algorithm)

Page 24: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Smalltalk MVCSmalltalk MVC

MVC main relationships:

Observer

Composite

Strategy

Page 25: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Describing Design PatternsDescribing Design Patterns

Pattern Name

The pattern’s name conveys the essence of the pattern succintly.

Page 26: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Describing Design PatternsDescribing Design Patterns

IntentWhat does the design pattern do?

What is the rationale and intent?

What particular design issue or

problem does it address?

Page 27: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Describing Design PatternsDescribing Design Patterns

Also Known As

Other well-known names for the pattern, if any.

Page 28: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Describing Design PatternsDescribing Design Patterns

Motivation

A scenario that illustrates a design problem and how the class and object structures in the pattern

solve the problem.

Page 29: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Describing Design PatternsDescribing Design Patterns

ApplicabilityWhat are the situations in which the design pattern can be applied?

What are examples of poor designs that the pattern can address?

Page 30: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Describing Design PatternsDescribing Design Patterns

StructureA graphical representation of the classes in the pattern using a notation based on the Unified Modeling Language (UML).

Interaction diagrams are also used to illustrate sequences of request and collaborations between objects.

Page 31: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Describing Design PatternsDescribing Design Patterns

Participants

The classes and/or objects participating in the design pattern and their responsibilities.

Page 32: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Describing Design PatternsDescribing Design Patterns

Collaborations

How the participants collaborate to carry out their responsibilities.

Page 33: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Describing Design PatternsDescribing Design Patterns

Consequences What are the trade-offs and results of using the pattern?

What aspect of system structure does it let vary independently?

Page 34: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Describing Design PatternsDescribing Design Patterns

Implementation What pitfalls, hints or techniques should you be aware of when implementing the pattern?

Are there language-specific issues?

Page 35: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Describing Design PatternsDescribing Design Patterns

Sample Code

Code fragments that illustrate how you might implement the pattern.

Page 36: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Describing Design PatternsDescribing Design Patterns

Known UsesExamples of the pattern found in real systems.

At least two examples from different domains.

Page 37: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Describing Design PatternsDescribing Design Patterns

Related Patterns What design patterns are closely related to this one?

What are the important differences?

With other patterns should this one be used?

Page 38: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

Abstract Factory

Provide an interface for creating families of related/dependent objects without specifying their

concrete classes.

Page 39: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

Adapter

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of imcompatible interfaces.

Page 40: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

Bridge

Decouple an abstraction from its implementation so that the two can vary independently.

Page 41: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

Builder

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Page 42: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

Chain of ResponsibilityAvoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

Page 43: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

Command

Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

Page 44: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

Composite

Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

Page 45: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

Decorator

Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

Page 46: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

Façade

Provide a unified interface to a set of interfaces in a subsystem. Facades define a higher-level interface that makes the subsystem easier to use.

Page 47: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

Factory Method

Define an interface for creating an object, but let subclasses decide which class instantiate. Factory Method lets a class defer instantiation to subclasses.

Page 48: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

Flyweight

Use sharing to support large number of fine-grained objects efficiently.

Page 49: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

Interpreter

Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.

Page 50: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

Iterator

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Page 51: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

MediatorDefine an object that encapsulates how a set of objects interact. Mediators promote loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

Page 52: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

Memento

Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.

Page 53: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

Observer

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Page 54: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

Prototype

Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype.

Page 55: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

Proxy

Provide a surrogate or placeholder for another object to control access to it.

Page 56: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

Singleton

Ensure a class has only one instance, and provide a global point to access to it.

Page 57: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

State

Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

Page 58: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

StrategyDefine a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Page 59: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

Template MethodDefine the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algoritm without changing the algorithm's structure.

Page 60: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

VisitorRepresent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

Page 61: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

1. Purpose:

Creational

Structural

Behavioral

Page 62: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Catalog of Design PatternsCatalog of Design Patterns

2. Scope:

Class (static)

Object (dynamic)

Page 63: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Design Patterns SpaceDesign Patterns SpacePurpose

Creational Structural Behavioral

Scope Class Factory Method Adapter (class) Interpreter

Template Method

Object Abstract Factory

Builder

Prototype

Singleton

Adapter (obj)

Bridge

Composite

Decorator

Façade

Flyweight

Proxy

Chain of Responsibility

Command

Iterator

Mediator

Memento

Observer

State

Strategy

Visitor

Page 64: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Finding appropiate objects

Determining object granularity

Specifying object interfaces

Specifying object implementations

Class vs. Interface inheritance

Page 65: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

ClassName

operation1()Type operation2()...

instanceVariable1Type instanceVariable2...

AbstractClass

operation1()Type operation2()...

Page 66: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

ParentClass

operation()...

Instantiator

SubClass

Instantiatee

Page 67: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

AbstractClass

operation()

ConcreteSubClass

operation()implementationpseudocode

Page 68: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Programming to an Interface,not an Implementation

Inheritance vs. Composition

Delegation

Inheritance vs. Parameterized Types

Page 69: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

There are two benefits to manipulating objects solely in terms of the interface defined by abstract classes:

1. Clients remain unaware of the specific types of objects they use, as long as the objects adhere to the interface the clients expect.

2. Clients remain unaware of the specific classes that implement these objects. Clients only know about the abstract class(es) defining the interface.

Page 70: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

This so greatly reduces implementation dependencies between subsystems that it leads to the following principle of Object Oriented Design:

Program to an interface, not an implementation

Page 71: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Inheritance vs. Composition

There are two common techniques for reusing functionality in object oriented systems:

1. class inheritance

2. object composition

Page 72: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Class Inheritance:

Lets you define the implementation of one class in terms of another’s.

Reuse by subclassing is often referred to as white-box reuse. The term white-box refers to visibility: with inheritance, the internals of parent classes are often visible to subclasses.

Page 73: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Object Composition:

Here, new functionality is obtained by assembling or composing objects to get more complex functionality.

This style of reuse is called black-box reuse, because no internal details of objects are visible.

Page 74: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Inheritance:

Straightforward to use, since it’s supported directly by the programming language

Makes it easier to modify the implementation being used (method overriding)

Page 75: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Inheritance:

You can’t change the implementations inherited from parent classes at run-time, because inheritance is defined at compile-time

Because inheritance exposes a subclass to details of it’s implementation, it’s often said that “inheritance breaks encapsulation”

Page 76: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Object Composition:

Object composition is defined dynamically at run-time through objects acquiring references to other objects (Abstract Factory, Builder, …)

No encapsulation broken (interfaces)

Any object can be replaced at run-time as long as it has the same type

Page 77: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Object Composition:

Favoring object composition over class inheritance helps you keep each class encapsulated and focused on one task.

Your classes and class hierarchies will remain small

A design based on object composition will have more objects

Page 78: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Second principle of Object Oriented Design:

Favor object composition over class

inheritance

Page 79: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Delegation

Delegation is a way of making composition as powerful for reuse as inheritance.

Page 80: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

The receiving object delegates operations to its delegate.

This is similar to subclasses deferring operations to parent classes

The receiver passes itself (this) to the delegate, to let the delegated operation refer to the receiver

Page 81: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Window

area()

Rectangle

area()

widthheight

return width*height

return rect->area()

rect

Page 82: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Delegation has a disadvantage it shares with other techniques that make software more flexible through object composition:

dynamic, highly parameterized software is harder to understand than more static software.

Page 83: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Several design patterns use delegation:

State

Strategy

Visitor

Mediator

Chain of Responsibility

Bridge

Page 84: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Inheritance vs. Paremeterized Types

Another technique for reusing functionality in object oriented systems is trough parameterized types:

This technique lets you define a type without specifying all the other types it uses

The unspecified type is supplied at the point of use

Page 85: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Inheritance vs. Paremeterized Types

Parameterized types give a third way to compose behavior in object oriented systems:

Page 86: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Inheritance vs. Paremeterized Types

To parameterize a sorting routine by the operation it uses to compare elements, we could make the comparison

1. An operation implemented by subclasses (Template Method)

2. The responsibility of an object that’s passed to the sorting routine (Strategy)

3. An argument of a C++ template or Java parameterized type

Page 87: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Parameterized Type Example

List<Integer> integerList = new LinkedList<Integer>()

Map<String, Integer> integerMap = new HashMap<String, Integer>()

Page 88: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Generic Class Example

public class C<T1, T2> {

private T1 type1;

private T2 type2;

public C( T1 type1, T2 type2 ) {

this.type1 = type1;

this.type2 = type2;

}

Page 89: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Generic Class Example

...

public T1 getType1() {

return this.type1;

}

public T2 getType2() {

return this.type2;

}

Page 90: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Generic Class Example

public static void main(String args[]) {

C<String, Integer> cStrInt = new C<String, Integer>("one", 1);

C<Integer, Boolean> cIntBool = new C<Integer, Boolean>(1, true);

}

Page 91: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Generic Interface Example

public interface I<T> {

public T getConnectionPool();

public void releaseConnectionPool(

T connPool );

}

Page 92: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Solving ProblemsSolving Problems

Generic Method Examplepublic class M {

public static <T extends Comparable> T

minimum( T a, T b )

{ if (a.compareTo( b ) <= 0)

return a;else

return b; }

}

Page 93: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Designing for ChangeDesigning for Change

The key to maximizing reuse lies in anticipating new requirements and changes, and in designing your systems so that they can evolve accordingly.

Page 94: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Designing for ChangeDesigning for Change

Common causes of redesign:

1.Creating an object by specifying a class explicitly

(Abstract Factory)

(Factory Method)

(Prototype)

Page 95: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Designing for ChangeDesigning for Change

Common causes of redesign:

2.Dependence on specific operations

(Chain of Responsibility)

(Command)

Page 96: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Designing for ChangeDesigning for Change

Common causes of redesign:

3.Dependence on hardware and software platform

(Abstract Factory)

(Bridge)

Page 97: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Designing for ChangeDesigning for Change

Common causes of redesign:

4.Dependence on object implementations or representations

(Abstract Factory) (Bridge)

(Memento) (Proxy)

Page 98: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Designing for ChangeDesigning for Change

Common causes of redesign:

5.Algorithmic dependences

(Builder) (Iterator)

(Strategy) (Template Method)

(Visitor)

Page 99: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Designing for ChangeDesigning for Change

Common causes of redesign:

6.Tight coupling

(Abstract Factory) (Bridge)

(Chain of Responsibilty) (Command)

(Facade) (Mediator) (Observer)

Page 100: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Designing for ChangeDesigning for Change

Common causes of redesign:

7.Extending functionality by subclassing

(Bridge) (Chain of Responsibilty)

(Composite) (Decorator)

(Observer) (Strategy)

Page 101: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Designing for ChangeDesigning for Change

Common causes of redesign:

8. Inability to alter classes conveniently

(Adapter)

(Decorator)

(Visitor)

Page 102: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

ToolkitsToolkits

A set of related and reusable classes designed to provide useful, general purpose functionality.

(C++ iostream, STL)(java.lang/java.util)

Page 103: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

ToolkitsToolkits

Toolkits emphasize code reuse

The OO equivalent of subroutine libraries

Page 104: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

FrameworksFrameworks

A set of cooperating classes that make up a reusable design for a specific class of software.

(C++ MFC)(J2EE)

Page 105: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

FrameworksFrameworks

The framework dictates the architecture of the application

Defines the overall structure, its partitioning into classes and objects, the key responsibilities, and the collaboration patterns

Page 106: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Design Difficulty LevelsDesign Difficulty Levels

1.Applications

2.Toolkits (harder)

3.Frameworks (hardest)

Page 107: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Selecting Design PatternsSelecting Design Patterns

Consider how design patterns solve design problems

Scan intent sections

Study how patterns interrelate

Study patterns of like purpose

Page 108: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Selecting Design PatternsSelecting Design Patterns

Examine a cause of redesign

Consider what should be variable in the design

Page 109: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Using Design PatternsUsing Design Patterns

Read the pattern once through

Pay particular attention to the Applicability and Consequences sections to ensure the pattern is right for the problem.

Page 110: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Using Design PatternsUsing Design Patterns

Study the Structure, Participants and Collaboration sectionsMake sure you understand the classes and objects in the pattern and how they relate to one another.

Page 111: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Using Design PatternsUsing Design Patterns

Look at the Sample Code section to see a concrete exampleStudying the code helps you learn how to implement the pattern.

Page 112: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Using Design PatternsUsing Design Patterns

Choose Names for pattern participants that are meaningful in the application contextThe names for particpants in design patterns are usually too abstract to appear in an application.

Page 113: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Using Design PatternsUsing Design Patterns

Define the Classes

Declare their interfaces, establish their inheritance relationships, and define the instance variables that represent data and object references.

Page 114: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Using Design PatternsUsing Design Patterns

Define application specific names for operations in the patternUse the responsibilities and collaborations associated with each operation as a guide.

Page 115: Basic Design Patterns 1. Introduction Gabriel Mañana Ed. 453 Of. 120 Ext. 14080 gjmananag @unal.edu.co.

Using Design PatternsUsing Design Patterns

Implement the operations to carry out the responsibilities and collaborations in the patternThe examples in the sample code section can help.