Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling...
-
Upload
hilary-newman -
Category
Documents
-
view
216 -
download
0
Transcript of Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling...
![Page 1: Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/1.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/2.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/3.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/4.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/5.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/6.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/7.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/8.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/9.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/10.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/11.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/12.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/13.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/14.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/15.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/16.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/17.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/18.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/19.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/20.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/21.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/22.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/23.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/24.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/25.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/26.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/27.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/28.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/29.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/30.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/31.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/32.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/33.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/34.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/35.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/36.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/37.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/38.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022062518/56649f0d5503460f94c20e35/html5/thumbnails/39.jpg)
Other design patterns
wikipedia!