design patterns. properties of good design minimize complexity maintainable loose coupling...

39
Design Patterns

Upload: hilary-newman

Post on 17-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Design Patterns

Page 2: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Properties of good design

• Minimize complexity• Maintainable • Loose coupling• Extensibility• Reusability• High fan in• Low to medium fan out• Portability• Leanness • Stratification• Standard techniques

Page 3: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Some principles of good design

• Open-Close Principle• Dependency Inversion Principle• Interface Segregation Principle• Single Responsibility Principle• Liskov’s Substitution Principle• No forgery principle (keep data in a single

place)• One rule one place (don’t duplicate code)

Page 4: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

What is wrong with this picture?

Ball

double gravity 32.1Private:

Enemy

double gravity 32.1Private:

No forgery!

Page 5: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

How about this?

Global double gravity 3.21

Page 6: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Why not use globals?

A. They make code hard to understand.

B. They make code hard to debug.

C. They make code hard to modify.

Page 7: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Why not use globals?

D. Profs O’Neill and Kuenning with haunt your dreams if you do.

Page 8: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Answer

All of the above.

Page 9: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Singleton Pattern

Problem: Ensure a class has only one instance and provide a global point of access to that instance.

Page 10: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Singleton Class

class Singleton

{

public:

static Singleton* getInstance();

private:

static Singleton* theSingletonInstance;

Singleton() {};

~Singleton() {};

Singleton(const Singleton& toCopy) {};

Singleton& operator=(const Singleton& toCopy) {};

};

Singleton::Singleton* theSingletonInstance = NULL;

Page 11: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Instance Implementation

Singleton* getInstance()

{

if (theSingletonInstance == NULL)

theSingletonInstance = new Singleton;

return theSingletonInstance;

}

Page 12: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Exampleclass Ball

{

public:

static Ball* theBall();

tuple getPosition();

void setPosition(tuple newPosition);

private:

Sphere theSphere;

Ball() {};

~Ball() {};

tuple position;

};

Ball::Ball* theBall = NULL;

use:

Ball* myBall = Ball::getInstance();

Page 13: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

what I want

I need a 2D graphics library that supports the following functions for triangles:– set color to r,g,b– translate vertices by dx, dy– rotate degrees about the origin – draw

Page 14: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

what I have

I have a 3D graphics library with a triangle class with the following interface– triangle()– triangle(v1x, v1y, v1z, v2x, v2y, v2z, v3x, v3y, v3z)– ~triangle()– set color(r, g, b)– rotate(vector, angle)– translate(dx, dy, dz)– scale(sx, sy, sz)– draw()– flip(planeA, planeB, planeC, planeD)– texture(textureMap)– standardize()

Page 15: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

just use the 3d class

• Constructor:

triangle t(v1x, v1y, 0, v2x, v2y, 0, v3x, v3y, 0)

• Rotate:

t.rotate(<0,0,1>,alpha)

Interface Segregation Principle

Page 16: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Solution

Triangle2D Triangle3D

implements the 2d triangle interface

Page 17: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

façade

Problem: You need to use a subset of a complex system or you need to interact with the system in a particular way.

Page 18: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

what I want

I want a physics engine that (among other things) detects collisions:

cCollision cPhysicsEngine::detectCollision(cPath p, cTriangles t)

I have a fast collision detection algorithm and a slower, more robust algorithm.

Page 19: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

cPhysicsEngine

cPhysicsFast

How about this?

cPhysicsSlow

In the future I may want to use a super slow algorithm.

Page 20: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

cPhysicsEngine cDetectCollision

cDetectCollisionFast

Strategy Design Pattern

cDetectCollisionSlow

supports open-close and single responsibility principles

Page 21: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Strategy design pattern

Problem: Want to be able to swap the algorithm used in an application.

Page 22: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Bridge vs. Strategy

Shape Drawer

Low ResHi Res

cPhysicsEngine cDetectCollision

cDetectCollisionFast cDetectCollisionSlow

Different intents: • bridge allows implementation to vary and includes adapters• strategy allows algorithms (behavior) to vary

DP hi res DP hi res

Page 23: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

what I want

I am building a drawing program. The user enters keystrokes to change modes (Add, Delete, Move) and mouse input that is interpreted based on the current mode.

Page 24: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

what I have

global int mode;

drawer.processMouse(key,position) {if mode==add

processMouseAddMode(key,position)else if mode==delete

processMouseDeleteMode(key,position)else if mode==move

processMouseMoveMode(key,position)}

Page 25: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

DrawerprocessKeyprocessMouse

DeleteDrawerAddDrawer MoveDrawer

How about this

Page 26: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

DrawerprocessKey

processMouse

ModeprocessMouse

DeleteAdd Move

State Design Pattern

1

supports open-close and single responsibility principles

Page 27: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

DrawerprocessKey

processMouse

ModeprocessMouse

DeleteAdd Move

State Design Pattern

1

ModeManagerprocessKey

1 1 1

1

Mode mgr. returns pointer to correct mode

Page 28: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

State Design Pattern

Problem: want to allow an object to alter its behavior when its internal state changes

Page 29: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

State vs. Strategy

DrawerprocessKey

processMouse

ModeprocessMouse

DeleteAdd Move

1

ModeManagerprocessKey

1 1 1

1

cPhysicsEngine cDetectCollision

cDetectCollisionFast cDetectCollisionSlow

Different intents: • state allows behaviors to vary dynamically• strategy typically used when algorithm is selected at start

Page 30: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Problem continued

• I also want to support “Undo”

• Help!

Page 31: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Command

MouseKey Menu

Command Design Pattern

Page 32: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Command Design Pattern

Encapsulate a request as an object to permit logging, queuing, un-doing etc.

Page 33: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

what I want

• I want a 2D drawing program that supports triangle and lines

• I want to be able to add, delete, draw, and move primitives.

• I want to be also want to be able to group primitives into a “widget” and treat the widget as a primitive.

• I want to be able to add and delete primitives from a widget

Page 34: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

solution 1

Widget Shape*

Triangle Line

What is the difference between a triangle and a widget holding a triangle?

Page 35: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Composite Design Pattern

Widget

Shape*

Triangle Line

Page 36: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Abstract factory

Page 37: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Observer Design Pattern

Page 38: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Observer Design Pattern

Page 39: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out

Other design patterns

wikipedia!