More Design Patterns 1.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator.
Plab – Tirgul 12 Design Patterns
. Plab – Tirgul 12 Design Patterns. Design Patterns u The De-Facto Book on Design Patterns:
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Using Design Patterns to Elaborate upon Design Models Moving from Analysis to Design Examine Design models and initial code to: Improve cohesion Reduce.
7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
1 Dept. of Computer Science & Engineering, York University, Toronto Software Development CSE3311 Composite Pattern.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Composite Design Pattern. Motivation – Dynamic Structure.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Design Patterns.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
SE2811 Week 7, Class 1 Composite Pattern Applications Conceptual form Class structure Coding Example Lab Thursday: Quiz SE-2811 Slide design: Dr. Mark.
BTS530: Major Project Planning and Design The Composite Pattern All References and Material From: Design Patterns,by (GOF!) E.Gamma, R.Helm, R.Johnson,
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
Decorator Explained. Intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to sub-classing for.
Computing IV Singleton Pattern Xinwen Fu.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IV Structural Patterns.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
08 - StructuralCSC4071 Structural Patterns concerned with how classes and objects are composed to form larger structures –Adapter interface converter Bridge.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
Design Patterns Introduction
1 Advanced Object-oriented Design – Principles and Patterns Structural Design Patterns.
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
S.Ducasse Stéphane Ducasse 1 Decorator.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Reference – Object Oriented Software Development Using Java - Jia COP 3331 Object Oriented Analysis and Design Chapter 10 – Patterns Jean Muhammad.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
Composite Pattern Himanshu Gupta Shashank Hegde CSE776 – Design Patterns Fall 2011 Good composition is like a suspension bridge - each line adds strength.
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Design Patterns: MORE Examples
Design Patterns: Brief Examples
Slide design: Dr. Mark L. Hornick
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Factory Patterns 1.
Chapter 11 Object-Oriented Design
Composite Design Pattern
Composite Pattern SE2811 Software Component Design
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Decorator Design Pattern
Intent (Thanks to Jim Fawcett for the slides)
Design Patterns - A few examples
More Design Patterns 1.
Object Oriented Analysis and Design
Decorator Intent Also known as Wrapper Example: a Text Window
Design Patterns A Case Study: Designing a Document Editor
Decorator Pattern Intent
Jim Fawcett CSE776 – Design Patterns Summer 2003
Software Development CSE3311
Informatics 122 Software Design II
Decorator Pattern Richard Gesick.
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Structural Patterns: Adapter and Bridge
13. Composite Pattern SE2811 Software Component Design
Creational Patterns.
Informatics 122 Software Design II
13. Composite Pattern SE2811 Software Component Design
Composite Design Pattern By Aravind Reddy Patlola.
Decorator Pattern.
Visitor Pattern Intent
Presentation transcript:

More Design Patterns 1

Additional Patterns Null Singleton Composite Decorator

Null Object Pattern Intent Participants Provide an object as a surrogate for the lack of an object of a given type. The Null Object Pattern provides intelligent do nothing behavior, hiding the details from its collaborators Participants AbstractClass defines abstract primitive operations that concrete implementations have to define RealClass a real implementation of the AbstractClass performing some real actions NullClass an implementation of the AbstractClass but will do nothing, in order to provide a non-null object to the client Client the client gets an implementation of the abstract class and uses it. It doesn't really care if the implementation is a null object or a real object since both of them are used in the same way

Null Object Pattern Applicability The Null Object Pattern is used to avoid special if blocks for do nothing code, by putting the “do nothing” code in the Null Object which becomes responsible for doing nothing. The client is not aware anymore if the real object or the null object is called so the 'if' section is removed from client implementation. The Null Object design pattern is more likely to be used in conjunction with the Factory pattern. The reason for this is obvious: A Concrete Classes need to be instantiated and then to be served to the client. The client uses the concrete class. The concrete class can be a Real Object or a Null Object.

Class Diagram Component void request( ) Component void request( ) RealComponent void request( ) NULLComponent void request( ) void request () { // do nothing }

Singleton Pattern Intent Applicability Consequences Ensure a class only has one instance, and provide a global point of access to it. Applicability Use Singleton pattern when There must be exactly one instance of a class, and it must be accessible to clients from a well-known access point When the sole instance should be extensible by sub-classing, and clients should be able to use an extended instance without modifying their code Consequences Controlled access to sole instance Singleton class encapsulates its sole instance and has strict control over how and when clients access it Reduced name space It avoids polluting the name space with global variables that store sole instances Permits refinement of operations and representation It can be subclassed

Singleton Pattern – Example code Singleton class declaration Implementation of Singleton class Singleton { public: static Singleton* Instance( ); protected: Singleton( ); private: static Singleton * _instance; } Singleton * Singleton:: _instance = 0; Singleton * Singleton::Instance( ) { if (_instance == 0) _instance = new Singleton( ); return _instance; }

Composite Pattern Applicability Participants Component Leaf Composite You want to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly Participants Component declares the interface for objects in the composition implements default behavior for the interface common to all classes, as appropriate declares an interface for accessing and managing its child components Leaf defines behavior for primitive objects in the composition Composite defines behavior for components having children. stores child components implements child-related operations in the Component interface

Composite Pattern Collaborations Consequences Clients use the Component class interface to interact with object in the composite structure. If the recipient is a Leaf, then the request is handled directly. If the recipient is a Composite, then it usually forwards request to its child components, possibly performing additional operations before and/or after forwarding Consequences Defines class hierarchies consisting of primitive objects and composite objects. Wherever client code expects a primitive object, it can also take a composite object. Makes the client simple. Clients can treat composite structures and individual objects uniformly. Makes it easier to add new kinds of components. Newly defined Composite or Leaf subclasses work automatically with existing structures and client code. Can make your design overly general. It makes it harder to restrict the components of a composite.

Class Diagram Equipment Client * CompositeEquipment CDDrive FloppyDisk Operation( ) Add(Component) Remove(Component) GetChild(int) Client * CompositeEquipment Operation( ) Add(Component) Remove(Component) GetChild(int) CDDrive Operation( ) FloppyDisk Operation( ) For all g in children g.Operation( );

Composite Pattern – Example code class FloppyDisk : public Equipment { public: FloppyDisk(const char*); virtual ~FloppyDisk( ); virtual Watt Power( ); virtual Currency NetPrice( ); virtual Currency DiscountPrice( ); } class Equipment { public: virtual ~Equipment( ); const char* name( ) { return _name; } virtual Watt Power( ); virtual Currency NetPrice( ); virtual Currency DiscountPrice( ); virtual void Add(Equipment *); virtual void Remove(Equipment *); virtual Iterator<Equipment*> * CreateIterator( ); protected: Equipment (const char * ); private: const char * _name; } class Chassis : public CompositeEquipment { public: Chassis (const char* ); virtual ~Chassis( ); virtual Watt Power( ); virtual Currency NetPrice( ); virtual Currency DiscountPrice( ); }

Composite Pattern – Example code class CompositeEquipment: public Equipment { public: virtual ~CompositeEquipment(); virtual Watt Power(); virtual Currency NetPrice(); virtual Currency DiscountPrice(); virtual void Add(Equipment *); virtual void Remove(Equipment *); virtual Iterator<Equipment *> * CreateIterator(); protected: CompositeEquipment(const char *); private: List<Equimpent *> _equipment; } An implementation of NetPrice( ) Currency CompositeEquipoment::NetPrice() { Iterator<Equipment *> * i = CreateIterator(); Currency total = 0; for (i->first(); i->isDone(); i->next()) total += i->currentItem()->NetPrice(); } delete i; return total;

Composite Pattern – Example code Assume we have additional Equipment classes such as Bus, Cabinet, etc. We can assemple equipment into a (simple) computer (CompositeEquipment object) Cabinet * cabinet = new Cabinet(“PC Cabinet”); Chassis * chassis = new Chassis(PC Chassis”); cabinet->Add(chassis); Bus * bus = new Bus(“MCA Bus”); bus->Add(new Card(“100 Mbs Ethernet”) ); chassis->Add(bus); chassis->Add (new FloppyDisk(“3.5 in Floppy”) ); cout << “ the net price is “ << chassis->NetPrice() << endl;

Decorator Pattern Intent Allows the programmer to add responsibilities to individual objects, not to an entire class. The decorator conforms to the interface of the component it decorates so that its presence is transparent to the component’s clients

Class Diagram Component operation( ) ConcreteComponent operation( ) Decorator operation( ) component.operation(); ConcreteDecoratorA operation( ) addedState ConcreteDecoratorB operation( ) addedBehavior( ) base.operation(); addedBehavior();

Decorator Pattern Participants Allows the programmer to add responsibilities to individual objects, not to an entire class. The decorator conforms to the interface of the component it decorates so that its presence is transparent to the component’s clients Component Defines the interface for objects that can be “decorated” ConcreteComponent Defines an object to which additional responsibilities can be added (can be “decorated”) Decorator Maintains a reference to a Component object and defines an interface that conforms to Components interface. ConcreteDecorator Adds responsibilities to the Component.

Decorator Pattern Consequences More flexible than static inheritance Avoids feature-laden classes high up in the hierarchy Instead of trying to support all foreseeable features in a complex, customizable class, you can define a simple class and add functionality incrementally with Decorator objects A Decorator and its Component are not identical A decorator acts as a transparent enclosure

?

References