220 likes | 239 Views
Design Patterns. CS 123/CS 231. Outline. Definition and Description of a Design Pattern Discussion of Selected Patterns Kinds of Patterns Reference: Gamma et al (“Gang-of-4”), Design Patterns. Design Pattern. Solution to a particular kind of problem How to combine classes and methods
E N D
Design Patterns CS 123/CS 231
Outline • Definition and Description of a Design Pattern • Discussion of Selected Patterns • Kinds of Patterns Reference: Gamma et al (“Gang-of-4”), Design Patterns
Design Pattern • Solution to a particular kind of problem • How to combine classes and methods • Based on design experience • Use requires understanding of the appropriate problem and being able to recognize when such problems occur
Describing a Pattern • Name • Intent • Situation (problem) and context • Solution • UML diagrams (class relationships and responsibilities) and code implications • Consequences • Results, variations, and tradeoffs
Selected Patterns for Discussion • Singleton • Factory Method • Composite • Iterator
Singleton • Intent: ensure a class has only one instance, and provide a global point of access to it • Design Solution: Singleton static uniqueinstance Singleton attributes… static getinstance() Singleton methods…
Singleton Example (Java) • Database public class Database { private static Database DB; ... private Database() { ... } public static Database getDB() { if (DB == null) DB = new Database(); return DB; } ... } Database static Database* DB instance attributes… static Database* getDB() instance methods… In application code… Database db = Database.getDB(); db.someMethod();
Singleton Example (C++) class Database { private: static Database *DB; ... private Database() { ... } public: static Database *getDB() { if (DB == NULL) DB = new Database()); return DB; } ... } Database *Database::DB=NULL; In application code… Database *db = Database.getDB(); Db->someMethod();
Singleton Consequences • Ensures only one (e.g., Database) instance exists in the system • Can maintain a pointer (need to create object on first get call) or an actual object • Can also use this pattern to control fixed multiple instances • Much better than the alternative: global variables
Abstract Factory • Intent: provide an interface for creating objects without specifying their concrete classes • Example: Stacks, Queues, and other data structures • Want users to not know or care how these structures are implemented (separation)
Solutions in C++ • Use of header file (class declarations) and implementation file (method definitions) ok but limited • Header file usually contains private declarations which are technically part of the implementation • Change in implementation requires that the application using the data structure be recompiled • Alternative: create an abstract superclass with pure virtual data structure methods
Design Solution for Abstract Factory Factory createProduct() Product virtual methods Client ConcreteProdA methods ConcreteProdB methods Note: this is an abbreviated design
Stack Example (C++) • Stack class defines virtual methods • push(), pop(), etc. • ArrayStack and LinkedStack are derived classes of Stack and contain concrete implementations • StackFactory class defines a createStack() method that returns a ptr to a concrete stack • Stack *createStack() { return new ArrayStack(); } • Client programs need to be aware of Stack and StackFactory classes only • No need to know about ArrayStack()
Factories in Java • Stack is an Interface • ArrayStack and LinkedStack implement Stack • StackFactory returns objects of type Stack through its factory methods
Abstract FactoryConsequences • Factory class or method can be altered without affecting the application • Concrete classes are isolated • Factory class can be responsible for creating different types of objects • e.g., DataStructure factory that returns stacks, queues, lists, etc. • “product families”
Composite Pattern • Intent: compose objects into tree structures to represent (nested) part-whole hierarchies • Example: GUIs (e.g., java.awt.*) • Buttons, labels, text fields, and panels are VisualComponents but panels can also contain VisualComponent objects • Calling show() on a panel will call show() on the objects contained in it
Iterator Pattern • Intent: provide a way to access the elements of an aggregate object sequentially without expressing its underlying representation • Example: iterators of C++ STL containers • Note that you can have several iterator objects for a container and that the iterators are separate classes
Kinds of Patterns • Creational • Object creation; e.g., Factory and Singleton • Structural • Object structure; e.g., Composite • Behavioral • Object interaction and distribution of responsibilities; e.g., Iterator
Creational Patterns • Abstract Factory • Builder • Factory Method • Prototype • Singleton
Structural Patterns • Adapter • Bridge • Composite • Decorator • Façade • Flyweight • Proxy
Behavioral Patterns • Chain Of Responsibility • Command • Interpreter • Iterator • Mediator • Memento • And a few more …
Summary • Main point: to recognize that there are proven solutions to problems that a designer/ programmer may encounter • Solutions are results of others’ experiences • Towards “standard approaches” • Search for such solutions first • Although there is some merit attempting to create the solution yourself • Becoming a design architect