270 likes | 592 Views
Design Patterns. William A. Hoffman NYU OOP Class. Design Patterns Overview. Why Design Patterns? History Definitions / Terminology Pattern Examples - from DP book Summary. Why OO Design Patterns . Effective for teaching OOD OOP is a new Art form
E N D
Design Patterns William A. Hoffman NYU OOP Class
Design Patterns Overview • Why Design Patterns? • History • Definitions / Terminology • Pattern Examples - from DP book • Summary
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
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
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)
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
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)
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 How a Pattern is Defined(GoF form)
New Patterns • Rule of 3, should be used in 3 places, it should be a reoccurring pattern, not an algorithm. • Each Domain has patterns
Purpose of Design Patterns • Creational - Abstact Factory • Structural - Adapter, Bridge • Behavioral - Iterator, State
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
Singleton Cont. • Structure Singleton return uniqueInstance static Instance(); SingleOperation(); GetSingletonDate(); static uniqueInstance singletonData
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
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
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; }
Singleton Cont • Sample Code - mazefactory class • Known Uses • meta class • InterViews WidgetKit • Related Patterns • many patterns use singleton See: Abstarct Factory, Builder and Prototype
Proxy • A surrogate or placeholder for another object to control access to TCIP Client1 Image_proxy ORB Image (instance) Client2 Image_proxy
CacheProxy Client1 Image_proxy_wcache ORB Image (instance) Client2 Image_proxy_wcache Can use Observer Pattern to Notify clients of changes in the source image class
Interaction of Patterns • Patterns are often interelated • AbstractFactory is often done as a Singleton • Observer and Proxy in CORBA
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
AbstractClass TemplateMethod() PrimativeOp1(); PrimativeOp2(); … PrimativeOp1(); PrimativeOp2(); ... ConcreateClass PrimativeOp1(); PrimativeOp2();
// From NeXT AppKit void View::Display() { SetFocus(); DoDisplay(); ResetFocus(); } void View::DoDisplay() { } void MyView::DoDisplay() { // do my render }
Creational Factory Method Abstract Factory Builder Prototype Singleton Structural Adapter Bridge Composite Decorator Façade Flyweight Proxy Overview of Patterns in GoF Design Patterns • Behavioral • Interpreter • Template Method • Chain of Responsibility • Command • Iterator • Mediator • Memento • Observer • State • Strategy • Visitor
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
Problems • Classification • Tools • Formalization
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
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