Download presentation
Presentation is loading. Please wait.
1
Design Patterns William A. Hoffman NYU OOP Class
2
Design Patterns Overview Why Design Patterns? History Definitions / Terminology Pattern Examples - from DP book Summary
3
Why OO Design Patterns Effective for teaching OOD OOP is a new Art form –People have only had ~20 years experience with OOP –Architecture and painting have been around for thousands of years. Effective method of transferring skill and experience –new OO programmers can be overwhelmed by the options
4
History of Design Patterns Christopher Alexander - an architect –“The Timeless Way of Building”, 1979 –“A Pattern Language”, 1977 –Pattern - “ A solution to a problem in a context” –Purposes effective reuse dissemination of solutions
5
History in OOP 1987 workshop at OOPSLA Beck and Ward 1993 The Hillside Group - Beck, Ward, Coplien, Booch, Kerth, Johnson 1994 Pattern Languages of Programming (PLoP) Conference 1995 Design Patterns : Elements of Reusable OO software - Gamma, Helm, Johnson, Vlissides (Gang of Four)
6
Definitions / Terminology Pattern Language - a term from Christopher Alexander, not a software language but a group of patterns used to construct a whole Pattern - many definitions see FAQ, lets try this one: “Patterns represent distilled experience which, through their assimilation, convey expert insight and knowledge to inexpert developers. “ - Brad Appleton
7
Types of Software Application Programs - internal reuse, extension. Excell, PowerPoint Toolkits - set of related, reusable general purpose classes. (Fresco) Frameworks- reusable set of cooperating classes for a specific class of software (TargetJr)
8
How a Pattern is Defined (GoF form) Name - good name Intent- what does it do Also Known As Motivation - a scenario Applicability - when to use Structure- UML Participants - classes Collaborations - how they work together Consequences - trade offs Implementation- hints on implementation Sample Code Known Uses Related Patterns
9
New Patterns Rule of 3, should be used in 3 places, it should be a reoccurring pattern, not an algorithm. Each Domain has patterns
10
Purpose of Design Patterns Creational - Abstact Factory Structural - Adapter, Bridge Behavioral - Iterator, State
11
Singleton (creational) Intent - ensure a class has only one instance and provide a global point of access Motivation - Some classes must only have one instance, (one file system, one window mgr), allow class to ensure only one instance. Applicability –Must be only one instance –The one instance should be extensible by subclassing, and clients should be able to use extended version without modification of client
12
Singleton Cont. Structure static Instance(); SingleOperation(); GetSingletonDate(); Singleton return uniqueInstance static uniqueInstance singletonData
13
Singleton Cont Participants –Singleton –defines an Instance operation that lets clients access its unique instance, a static or class method is used Collaborations –clients access only from the unique instance
14
Singleton Cont Consequences –controlled access to sole instance –reduced name space (no globals) –permits refinement via sub-classing –permits a variable number of instnaces –more flexible than class operations, specifically in c++/java static is not virtual –clients access only from the unique instance
15
class Singleton { public: static Singleton* Instance(); protected: Singleton(); private: static Singleton* _instance; }; Singleton* Singleton::_instance = 0; Singleton* Singleton::Instance() { if(!_instance) _instance = new Singleton(); return _instance; }
16
Singleton Cont Sample Code - mazefactory class Known Uses –meta class –InterViews WidgetKit Related Patterns –many patterns use singleton See: Abstarct Factory, Builder and Prototype
17
Proxy A surrogate or placeholder for another object to control access to ORB Image (instance) Client1 Image_proxy Client2 Image_proxy TCIP
18
CacheProxy ORB Image (instance) Client1 Image_proxy_wcache Client2 Image_proxy_wcache Can use Observer Pattern to Notify clients of changes in the source image class
19
Interaction of Patterns Patterns are often interelated –AbstractFactory is often done as a Singleton Observer and Proxy in CORBA
20
Template Method Intent - Define a skeleton of an algorithm in an operation, deferring some steps to subclasses. Sub-classes can redefine steps in an algorithm without changing its structure Motivation- Document example Applicability –implement invariant parts of an algorithm –avoid code duplications by noticing common behavior in sub-classes –hook extenstions
21
AbstractClass TemplateMethod() PrimativeOp1(); PrimativeOp2(); ConcreateClass PrimativeOp1(); PrimativeOp2(); … PrimativeOp1(); PrimativeOp2();...
22
// From NeXT AppKit void View::Display() { SetFocus(); DoDisplay(); ResetFocus(); } void View::DoDisplay() { } void MyView::DoDisplay() { // do my render }
23
Overview of Patterns in GoF Design Patterns Creational –Factory Method –Abstract Factory –Builder –Prototype –Singleton Structural –Adapter –Bridge –Composite –Decorator –Façade –Flyweight –Proxy Behavioral –Interpreter –Template Method –Chain of Responsibility –Command –Iterator –Mediator –Memento –Observer –State –Strategy –Visitor
24
Design Pattern Benefits Adds to the language of OOP –much information is expressed in a single term Documentation Reuse of ideas not just code CORBA problem - found a pattern on the web Anti-Patterns
25
Problems Classification Tools Formalization
26
Patterns on the web Patterns Home Page: http://hillside.net/patterns http://st-www.cs.uiuc.edu/users/patterns/patterns.html http://c2.com/ppr/index.html - Portland Pattern Repository http://www.entract.com/~bradapp/docs/patterns-intro.html
27
Parting Quote It is possible to make buildings by stringing together patterns, in a rather loose way. A building make like this, is an assembly of patterns. It is not dense. It is not profound. But I is also possible to put patterns together in such a way hat many patterns overlap in the same physical space: the building is very dense; it has many meanings captured in a small space; and through this density, it becomes profound. –Christopher Alexander
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.