Download presentation
Presentation is loading. Please wait.
Published byKathleen French Modified over 8 years ago
1
1 Design Patterns CS 123/CS 231
2
2 Outline zDefinition and Description of a Design Pattern zDiscussion of Selected Patterns zKinds of Patterns Reference: Gamma et al (“Gang-of-4”), Design Patterns
3
3 Design Pattern zSolution to a particular kind of problem zHow to combine classes and methods zBased on design experience zUse requires understanding of the appropriate problem and being able to recognize when such problems occur
4
4 Describing a Pattern zName zIntent ÕSituation (problem) and context zSolution ÕUML diagrams (class relationships and responsibilities) and code implications zConsequences ÕResults, variations, and tradeoffs
5
5 Selected Patterns for Discussion zSingleton zFactory Method zComposite zIterator
6
6 Singleton zIntent: ensure a class has only one instance, and provide a global point of access to it zDesign Solution: Singleton static uniqueinstance Singleton attributes… static getinstance() Singleton methods…
7
7 Singleton Example (Java) zDatabase Database static Database* DB instance attributes… static Database* getDB() instance methods… public class Database { private static Database DB;... private Database() {... } public static Database getDB() { if (DB == null) DB = new Database(); return DB; }... } In application code… Database db = Database.getDB(); db.someMethod();
8
8 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();
9
9 Singleton Consequences zEnsures only one (e.g., Database) instance exists in the system zCan maintain a pointer (need to create object on first get call) or an actual object zCan also use this pattern to control fixed multiple instances zMuch better than the alternative: global variables
10
10 Abstract Factory zIntent: provide an interface for creating objects without specifying their concrete classes zExample: Stacks, Queues, and other data structures ÕWant users to not know or care how these structures are implemented (separation)
11
11 Solutions in C++ zUse 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 zAlternative: create an abstract superclass with pure virtual data structure methods
12
12 Design Solution for Abstract Factory Factory createProduct() Product virtual methods ConcreteProdA methods ConcreteProdB methods Client Note: this is an abbreviated design
13
13 Stack Example (C++) zStack class defines virtual methods Õpush(), pop(), etc. zArrayStack and LinkedStack are derived classes of Stack and contain concrete implementations zStackFactory class defines a createStack() method that returns a ptr to a concrete stack ÕStack *createStack() { return new ArrayStack(); } zClient programs need to be aware of Stack and StackFactory classes only ÕNo need to know about ArrayStack()
14
14 Factories in Java zStack is an Interface zArrayStack and LinkedStack implement Stack zStackFactory returns objects of type Stack through its factory methods
15
15 Abstract Factory Consequences zFactory class or method can be altered without affecting the application ÕConcrete classes are isolated zFactory class can be responsible for creating different types of objects Õe.g., DataStructure factory that returns stacks, queues, lists, etc. Õ “product families”
16
16 Composite Pattern zIntent: compose objects into tree structures to represent (nested) part- whole hierarchies zExample: 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
17
17 Iterator Pattern zIntent: provide a way to access the elements of an aggregate object sequentially without expressing its underlying representation zExample: iterators of C++ STL containers ÕNote that you can have several iterator objects for a container and that the iterators are separate classes
18
18 Kinds of Patterns zCreational ÕObject creation; e.g., Factory and Singleton zStructural ÕObject structure; e.g., Composite zBehavioral ÕObject interaction and distribution of responsibilities; e.g., Iterator
19
19 Creational Patterns zAbstract Factory zBuilder zFactory Method zPrototype zSingleton
20
20 Structural Patterns zAdapter zBridge zComposite zDecorator zFaçade zFlyweight zProxy
21
21 Behavioral Patterns zChain Of Responsibility zCommand zInterpreter zIterator zMediator zMemento zAnd a few more …
22
22 Summary zMain 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” zSearch for such solutions first ÕAlthough there is some merit attempting to create the solution yourself zBecoming a design architect
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.