CS - 00 - 10 JAPE: a Java Annotation Patterns Engine Hamish ...
1 Design Patterns CS 123/CS 231. 2 Outline zDefinition and Description of a Design Pattern...
-
Upload
kathleen-french -
Category
Documents
-
view
214 -
download
0
Transcript of 1 Design Patterns CS 123/CS 231. 2 Outline zDefinition and Description of a Design Pattern...
2
Outline
Definition and Description of a Design Pattern
Discussion of Selected PatternsKinds of Patterns
Reference: Gamma et al (“Gang-of-4”), Design Patterns
3
Design Pattern
Solution to a particular kind of problemHow to combine classes and methodsBased on design experienceUse requires understanding of the
appropriate problem and being able to recognize when such problems occur
4
Describing a Pattern
NameIntent
Situation (problem) and contextSolution
UML diagrams (class relationships and responsibilities) and code implications
ConsequencesResults, variations, and tradeoffs
6
Singleton
Intent: ensure a class has only one instance, and provide a global point of access to it
Design Solution:Singleton
static uniqueinstanceSingleton attributes…
static getinstance()Singleton methods…
7
Singleton Example (Java)
Database
Database
static Database* DBinstance 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
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
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
10
Abstract Factory
Intent: provide an interface for creating objects without specifying their concrete classes
Example: Stacks, Queues, and other data structuresWant users to not know or care how
these structures are implemented (separation)
11
Solutions in C++
Use of header file (class declarations) and implementation file (method definitions) ok but limitedHeader file usually contains private declarations
which are technically part of the implementationChange in implementation requires that the
application using the data structure be recompiled
Alternative: create an abstract superclass with pure virtual data structure methods
12
Design Solution for Abstract Factory
Factory
createProduct()
Product
virtual methods
ConcreteProdA
methods
ConcreteProdB
methods
Client
Note: this is anabbreviated design
13
Stack Example (C++)
Stack class defines virtual methodspush(), 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 stackStack *createStack() { return new ArrayStack(); }
Client programs need to be aware of Stack and StackFactory classes only No need to know about ArrayStack()
14
Factories in Java
Stack is an InterfaceArrayStack and LinkedStack
implement StackStackFactory returns objects of type
Stack through its factory methods
15
Abstract FactoryConsequences
Factory class or method can be altered without affecting the applicationConcrete classes are isolated
Factory class can be responsible for creating different types of objectse.g., DataStructure factory that returns
stacks, queues, lists, etc. “product families”
16
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
17
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 containersNote that you can have several iterator
objects for a container and that the iterators are separate classes
18
Kinds of Patterns
CreationalObject creation; e.g., Factory and
SingletonStructural
Object structure; e.g., CompositeBehavioral
Object interaction and distribution of responsibilities; e.g., Iterator
21
Behavioral Patterns
Chain Of ResponsibilityCommandInterpreterIteratorMediatorMementoAnd a few more …
22
Summary
Main point: to recognize that there are proven solutions to problems that a designer/ programmer may encounterSolutions are results of others’ experiencesTowards “standard approaches”
Search for such solutions firstAlthough there is some merit attempting to
create the solution yourself
Becoming a design architect