320312 Software Engineering (P. Baumann)
Design Patterns
Instructor: Peter Baumann email: p.baumann@jacobs-university.de tel:
- 3178
- ffice:
room 88, Research 1 Credits: Xiaochuan Yi, U of Georgia Nenad Medvidović dofactory.com Sommerville, Chapter 18
Design Patterns Credits: Xiaochuan Yi, U of Georgia Nenad - - PowerPoint PPT Presentation
Design Patterns Credits: Xiaochuan Yi, U of Georgia Nenad Medvidovi Sommerville, Chapter 18 dofactory.com Instructor: Peter Baumann email: p.baumann@jacobs-university.de tel: -3178 office: room 88, Research 1 CONGRESS.SYS Corrupted:
320312 Software Engineering (P. Baumann)
Instructor: Peter Baumann email: p.baumann@jacobs-university.de tel:
room 88, Research 1 Credits: Xiaochuan Yi, U of Georgia Nenad Medvidović dofactory.com Sommerville, Chapter 18
2 320312 Software Engineering (P. Baumann)
3 320312 Software Engineering (P. Baumann)
if ( condition ) statement; if [ condition ]; then statement; fi
conditional evaluation
"goto considered bad"
BNF grammars www.cs.sfu.ca/~cameron/Teaching/383/syn-sem-prag-meta.html
4 320312 Software Engineering (P. Baumann)
5 320312 Software Engineering (P. Baumann)
6 320312 Software Engineering (P. Baumann)
a beer hall where people socialize?
7 320312 Software Engineering (P. Baumann)
8 320312 Software Engineering (P. Baumann)
that can be instantiated in different ways
9 320312 Software Engineering (P. Baumann)
10 320312 Software Engineering (P. Baumann)
// Singleton pattern --Structural example class Singleton { public: static Singleton* Instance() { static Singleton instance; return &instance; } private: Singleton() {} } int main() { // Constructor is protected, cannot use new Singleton *s1 = Singleton::Instance(); Singleton *s2 = Singleton::Instance(); Singleton *s3 = s1->Instance(); Singleton &s4 = *Singleton::Instance(); if( s1 == s2 ) cout << "same instance" << endl; }
11 320312 Software Engineering (P. Baumann)
class LoadBalancer { private: LoadBalancer() { add_all_servers; } public: static LoadBalancer *GetLoadBalancer() { // thread-safe in C++ 11 static LoadBalancer balancer; return &balancer; } … }
// SingletonApp test LoadBalancer *b1 = LoadBalancer::GetLoadBalancer(); LoadBalancer *b2 = LoadBalancer::GetLoadBalancer(); if( b1 == b2 ) cout << "same instance" << endl;
12 320312 Software Engineering (P. Baumann)
// Singleton pattern class Singleton { public: static Singleton* Instance() { static Singleton instance; return &instance; } private: Singleton() {} }
// Singleton -- modified example class Singleton { public: static Singleton* Instance() { static Singleton instance; return &instance; } private: Singleton() {} Singleton(const Singleton&); Singleton& operator=(const Singleton&); }
13 320312 Software Engineering (P. Baumann)
Subject A: 40 B: 25 C: 15 D: 20 Observer 1 Observer 2
50 25 A B C D A B C D
14 320312 Software Engineering (P. Baumann)
15 320312 Software Engineering (P. Baumann)
Subject Observer Attach (Observer) Detach (Observer) Notify () Update () ConcreteSubject GetState () subjectState ConcreteObserver Update ()
return subjectState for all o in observers
subject -> GetState ()
16 320312 Software Engineering (P. Baumann)
cooperate; Centralizes control
17 320312 Software Engineering (P. Baumann)
a set of interfaces in a subsystem
that makes subsystem easier to use
18 320312 Software Engineering (P. Baumann)
19 320312 Software Engineering (P. Baumann)
that could not otherwise because of incompatible interfaces
20 320312 Software Engineering (P. Baumann)
22 320312 Software Engineering (P. Baumann)
23 320312 Software Engineering (P. Baumann)
29 320312 Software Engineering (P. Baumann)
Creates an instance of several families of classes
Separates object construction from its representation
Creates an instance of several derived classes
A fully initialized instance to be copied or cloned
A class of which only a single instance can exist
Match interfaces of different classes
Separates an object’s interface from its implementation
A tree structure of simple and composite objects
Add responsibilities to objects dynamically
A single class that represents an entire subsystem
A fine-grained instance used for efficient sharing
An object representing another object
30 320312 Software Engineering (P. Baumann)
A way of passing a request between a chain of objects
Encapsulate a command request as an object
A way to include language elements in a program
Sequentially access the elements of a collection
Defines simplified communication between classes
Capture and restore an object's internal state
A way of notifying change to a number of classes
Alter an object's behavior when its state changes
Encapsulates an algorithm inside a class
Defer the exact steps of an algorithm to a subclass
Defines a new operation to a class without change
31 320312 Software Engineering (P. Baumann)