Escolar Documentos
Profissional Documentos
Cultura Documentos
201
Computing IV
Introduction to Design Pattern
Xinwen Fu
Useful References
Design Pattern
By Dr. Xinwen Fu
CS@UML
Outline
Introduction to Pattern
Gang of Four Design Pattern
Adapter Pattern
Introduction to UML
By Dr. Xinwen Fu
CS@UML
What is a Pattern?
CS@UML
Pattern of Porches
CS@UML
Why Patterns?
CS@UML
1993 - Kent Beck and Grady Booch sponsor the first meeting
of what is now known as the Hillside Group
By Dr. Xinwen Fu
CS@UML
Conceptual Pattern
Design Pattern
CS@UML
CS@UML
Outline
Introduction to Pattern
Gang of Four Design Pattern
Adapter Pattern
Introduction to UML
By Dr. Xinwen Fu
CS@UML
10
CS@UML
11
Creational Patterns
Structural Patterns
Behavioral Patterns
CS@UML
12
Purpose
Design Pattern
Creational
Builder (110)
Prototype (133)
Singleton (144)
Adapter (157)
interface to an object
Bridge (171)
implementation of an object
Composite (183)
Decorator (196)
Facade (208)
interface to a subsystem
Flyweight (218)
Proxy (233)
Chain of Responsibility
(251)
Command (263)
Interpreter (274)
Iterator (289)
Mediator (305)
Memento (316)
Observer (326)
State (338)
states of an object
Strategy (349)
an algorithm
steps of an algorithm
Structural
Behavioral
Online Materials
Design Patterns
By Dr. Xinwen Fu
CS@UML
14
Iterator
By Dr. Xinwen Fu
CS@UML
15
CS@UML
16
Overgrown, tangled,
haphazard spaghetti
code is hard to
comprehend, repair,
or extend, and tends
to grow even worse
if it is not somehow
brought under
control.
By Dr. Xinwen Fu
CS@UML
17
Outline
Introduction to Pattern
Gang of Four Design Pattern
Adapter Pattern
Introduction to UML
By Dr. Xinwen Fu
CS@UML
18
C++ Example
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
#include <iostream.h>
using namespace std;
typedef int Coordinate;
typedef int Dimension;
// Desired interface (Target)
class Rectangle {
public:
virtual void draw() = 0;
};
// Legacy component (Adaptee)
class LegacyRectangle {
public:
LegacyRectangle(int x1, int y1, int x2, int y2) {
x1_ = x1;
y1_ = y1;
x2_ = x2;
y2_ = y2;
cout << "LegacyRectangle: create. (" << x1_ << "," << y1_ << ") => ("
<< x2_ << "," << y2_ << ")" << endl;
}
By Dr. Xinwen Fu
19
CS@UML
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
void oldDraw() {
cout << "LegacyRectangle: oldDraw. (" << x1_ << "," << y1_ <<
") => (" << x2_ << "," << y2_ << ")" << endl;
}
private:
int x1_, y1_, x2_, y2_;
};
// Adapter wrapper
class RectangleAdapter: public Rectangle, private LegacyRectangle {
public:
RectangleAdapter(int x, int y, int w, int h):
LegacyRectangle(x, y, x + w, y + h) {
cout << "RectangleAdapter: create. (" << x << "," << y <<
"), width = " << w << ", height = " << h << endl;
}
virtual void draw() {
cout << "RectangleAdapter: draw." << endl;
oldDraw();
}
};
int main() {
22.
Rectangle *r = new RectangleAdapter(120, 200, 60, 40);
23.
r->draw();
By Dr. Xinwen Fu
24.
}
CS@UML
21.
20
6.
7.
8.
9.
#include <iostream>
class Foo {
public:
Foo() { std::cout << "Foo's constructor" << std::endl; }
};
class Bar : public Foo {
public:
Bar() { std::cout << "Bar's constructor" << std::endl; }
};
int main() {
11.
// a lovely elephant ;)
12.
Bar bar;
13. }
10.
By Dr. Xinwen Fu
CS@UML
21
Interface classes
22
CS@UML
class IErrorLog
{
virtual bool OpenLog(const char *strFilename) = 0;
virtual bool CloseLog() = 0;
5.
6.
7.
};
23
CS@UML
Intent
CS@UML
24
Implementation Issues
CS@UML
25
Class Adapter
By Dr. Xinwen Fu
CS@UML
26
Object Adapter
By Dr. Xinwen Fu
CS@UML
27
Outline
Introduction to Pattern
Gang of Four Design Pattern
Adapter Pattern
Introduction to UML
By Dr. Xinwen Fu
CS@UML
28
Objectives of UML
29
CS@UML
A Class in UML
Public
Protected
Private
Package
Class name
Attribute
Operator
30
CS@UML
Object Diagram
object name : class
31
CS@UML
Inheritance
Association
-Role2
0..1
Dependency
CS@UML
By Dr. Xinwen Fu
CS@UML
33
Sensor
Generalization
CalibratingSensor
An
is-a relationship
Abstract class in italic font
+currentValue()
+calibrate()
HistoricalSensor
WindDirectionSensor
+currentDirection()
-highestValue
-lowestVale
+resetHighest()
+resetLowest()
TrendSensor
-trend
TemperatureSensor
CS@UML
HumiditySensor
WindspeedSensor
-humidity
-speed
+currentHumidity()
+currentSpeed()
Barometer
-temp
-pressure
+currentTemp()
+currentPressure()
34
35
CS@UML
Association
36
CS@UML
Examples of Association
Bi-directional association
Uni-directional association
37
CS@UML
Complicated Association:
Aggregation and Composition
Aggregation is a shared
containment. Many other
classes may have the same type
of aggregate. e.g., string, list
38
CS@UML
Dependency
The Cart class is the client, and the Product class is the supplier
Product
39
CS@UML
Dependency Example
Here is a simple rule for when a dependency occurs. If the code for one class contains the
name of another class in the diagram, and no other connection exists in the diagram between
them then there should be a dashed dependency arrow from the first to the second class
5.
class A
{
public:
B returns_a_B();
void has_a_B_argument(B);
void has_a_B_in_its_implementation();
};
6.
A::void has_a_B_in_its_implementation(){ B b; }
1.
2.
3.
4.
40
CS@UML
CS@UML
CS@UML
42
Visual Paradigm
Only problem is e
By Dr. Xinwen Fu
CS@UML
43
By Dr. Xinwen Fu
CS@UML
44
Watermarked Diagram
By Dr. Xinwen Fu
CS@UML
45
CS@UML
46
Backup
By Dr. Xinwen Fu
CS@UML
47
Reference
1.
2.
3.
4.
5.
6.
By Dr. Xinwen Fu
CS@UML
48
CS@UML
49
By Dr. Xinwen Fu
CS@UML
50
UML Part II
Introduction to UML
Use Case Diagrams
Class Diagrams
Modeling Behavior and Sequence
Diagrams
51
CS@UML
Objectives of UML
52
CS@UML
3.
4.
5.
class Base {
protected:
public:
const char* GetName() { return "Base"; }
};
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
int main() {
Derived cDerived;
Base &rBase = cDerived;
cout << "rBase is a " << rBase.GetName() << endl;
}
53
CS@UML
Virtual functions
1.
2.
class Base {
protected:
3.
4.
5.
6.
public:
virtual const char* GetName() { return "Base"; }
};
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
int main() {
Derived cDerived;
Base &rBase = &cDerived;
cout << "rBase is a " << rBase.GetName() << endl;
17.
return 0;
18.
19.
}
54
CS@UML
1.
class Base {
public:
// a normal non-virtual function
const char* SayHi() { return "Hi"; }
2.
3.
4.
5.
6.
7.
8.
9.
10.
};
55
CS@UML
CS@UML
Interface classes
CS@UML
class IErrorLog
{
virtual bool OpenLog(const char *strFilename) = 0;
virtual bool CloseLog() = 0;
5.
6.
7.
};
58
CS@UML