Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.
-
date post
21-Dec-2015 -
Category
Documents
-
view
214 -
download
0
Transcript of Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.
![Page 1: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/1.jpg)
Design PatternsPart IV
(TIC++V2:C10)
Yingcai Xiao
10/01/08
![Page 2: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/2.jpg)
Design Patterns (What?)
• The original paper by the gang of four http://www.cse.msu.edu/~cse870/Materials/Patterns/Docs/orig-patterns-paper.pdf
• Design Patterns are devices that allow designers to share knowledge about their design. Design patterns identify, name, and abstract common themes in object-oriented design.
• idea reuse vs. code reuse.
![Page 3: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/3.jpg)
Characteristics of a Design Pattern
• smart: elegant solutions that would not occur to a novice immediately
• generic: independent of specific system characteristics
• well-proven: identified from successful real, object-oriented systems
• simple: involve only a handful of classes
![Page 4: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/4.jpg)
More Characteristics
• reusable: reuse at the design level, generic, well-documented
• object-oriented: uses classes, objects, generalization, and polymorphism
![Page 5: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/5.jpg)
Constructing a Design Pattern
• name
• problem description: when pattern is to be used and which problem it attempts to solve
• solution: classes and objects, their structure, and dynamic collaboration
• consequences: results and trade-offs of applying the pattern
![Page 6: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/6.jpg)
Types of Patterns (Purpose)
Categorize patterns by what they do.
• creational: deal with the process of object creation
• structural: deal with the composition of classes or objects
• behavioral: describe ways in which classes or objects interact and allocate responsibilities
![Page 7: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/7.jpg)
Types of Patterns (Scope)
• Scope specifies whether a pattern applies primarily to classes or to objects.
• Class scope:: deals with relationship between classes, established through inheritance. (static)
• Object scope: deals with object relationship, established through inclusion and usage. (dynamic)
![Page 8: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/8.jpg)
Design Pattern Space
![Page 9: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/9.jpg)
Example: Proxy Pattern• Proxy: provides a surrogate to hide the real object behind.• Applications:
1. remote proxy: to represent a remote object locally for easy and efficient coding (e.g. Java RMI and .NET Remoting)
2. protection proxy: to control the access to the real object (e.g. a proxy server hides the real server behind the firewall)
3. virtual proxy: to defer the expansive actions creating the real object.
4. smart reference: replacement for bare pointer that performs additional actions when an object is accessed
![Page 10: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/10.jpg)
Example: Virtual Proxyfrom the original paper by the gang of four
using OMT Notation (object diagram)
Referes to Referes to (virtual representation)
![Page 11: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/11.jpg)
Class Diagram of the Virtual Image Proxy
![Page 12: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/12.jpg)
General Structure of a Proxy
Class Diagram
Object Diagram
![Page 13: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/13.jpg)
Components of a Proxy
• Proxy:– maintains a reference to let proxy access the
real subject– provides an interface identical to Subject’s so a
proxy can be substituted for the real subject– controls access to the real subject; may be
responsible for creating and deleting it
![Page 14: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/14.jpg)
More Participants
• Subject:– defines the common interface for RealSubject
and Proxy so a Proxy can be used anywhere a RealSubject is expected
• RealSubject:– defines the real object that the proxy represents
![Page 15: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/15.jpg)
Collaborations
• Proxy forwards request to RealSubject when appropriate, depending on the kind of proxy
![Page 16: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/16.jpg)
Sequence Diagram
![Page 17: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/17.jpg)
Collaboration Diagram
![Page 18: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/18.jpg)
Consequences
• proxy pattern introduces a level of indirection when accessing an object– a remote proxy can hide the fact that an object
resides in a different address space– a virtual proxy can perform optimizations such
as creating an object on demand– protection proxies and smart references allow
additional tasks when an object is referenced
![Page 19: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/19.jpg)
ImplementationA proxy can exploit the following features:• Java: use interface and implementation.• C++: use virtual functions and overloading
the member access operators. • Smalltalk: use doesNotUnderstand, which
supports automatic forwarding of requests• Proxy doesn’t have to know the type of the
real object (upcasted to Object in Java)
![Page 20: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/20.jpg)
Sample Code in Javapublic class Proxy implements Subject {
RealSubject refersTo;
public void Request ( ) {
if (refersTo = = null)
refersTo = new RealSubject ( );
refersTo.Request ( );
}
}
![Page 21: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/21.jpg)
Known Uses
• Stubs in Java RMI.
• Proxy server in networking
• NEXTSTEP uses proxies as local representatives for objects that may be distributed
• Proxies in Smalltalk to access remote objects
![Page 22: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/22.jpg)
Related Patterns
• adapter: provides a different interface to the object it adapts; proxy provides the same interface as its subject
• decorator: adds one or more responsibilities to an object; proxy controls access to an object
• a protection proxy might be implemented exactly like a decorator
![Page 23: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/23.jpg)
Proxy Example in C++
class ProxyBase {public: virtual void f() = 0; virtual void g() = 0; virtual void h() = 0; virtual ~ProxyBase() {}}; class Implementation : public ProxyBase {public: void f() { cout << "Implementation.f()" << endl; } void g() { cout << "Implementation.g()" << endl; } void h() { cout << "Implementation.h()" << endl; }};
![Page 24: Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.](https://reader035.fdocuments.us/reader035/viewer/2022070323/56649d615503460f94a43873/html5/thumbnails/24.jpg)
Proxy Example in C++
class Proxy : public ProxyBase { ProxyBase* implementation;public: Proxy() { implementation = new Implementation(); } ~Proxy() { delete implementation; } // Forward calls to the implementation: void f() { implementation->f(); } void g() { implementation->g(); } void h() { implementation->h(); }}; int main() {
Proxy p; p.f(); p.g(); p.h();
} ///:~