TDDB84 Design Patterns Lecture 09. State, Prototype, Visitor, Flyweight
|
|
- Charity Banks
- 6 years ago
- Views:
Transcription
1 TDDB84 Design Patterns Lecture 09 State, Prototype, Visitor, Flyweight Peter Bunus Department of Computer and Information Science Linköping University, Sweden
2 TDDB84 Design Patterns Slide 2 State
3 TDDB84 Design Patterns Slide 3 State Non Software Example
4 The Gumball Vending Machine Has Quarter Inserts quarter ejects quarter turns crank Out of gumballs No Quarter gumballs=0 gumballs>0 Gumball sold dispense gumballs TDDB84 Design Patterns Slide 4
5 Writting Code public class GumballMachine { final static int SOLD_OUT = 0; final static int NO_QUARTER = 1; final static int HAS_QUARTER = 2; final static int SOLD = 3; Has Quarter Inserts quarter ejects quarter int state = SOLD_OUT; int count = 0; public GumballMachine(int count) { this.count = count; if (count > 0) { state = NO_QUARTER; Out of gumballs No Quarter gumballs=0 turns crank gumballs>0 dispense gumballs Gumball sold TDDB84 Design Patterns Slide 5
6 Writting the Code public void insertquarter() { if (state == HAS_QUARTER) { System.out.println("You can't insert another quarter"); else if (state == NO_QUARTER) { state = HAS_QUARTER; System.out.println("You inserted a quarter"); else if (state == SOLD_OUT) { System.out.println("You can't insert a quarter, the machine is sold out"); else if (state == SOLD) { System.out.println("Please wait, we're already giving you a gumball"); Out of gumballs Has Quarter Inserts quarter ejects quarter turns crank No Quarter gumballs=0 gumballs>0 Gumball sold dispense gumballs TDDB84 Design Patterns Slide 6
7 Writting the Code public void ejectquarter() { if (state == HAS_QUARTER) { System.out.println("Quarter returned"); state = NO_QUARTER; else if (state == NO_QUARTER) { System.out.println("You haven't inserted a quarter"); else if (state == SOLD) { System.out.println("Sorry, you already turned the crank"); else if (state == SOLD_OUT) { System.out.println("You can't eject, Has Quarter you haven't inserted a quarter yet"); Inserts quarter ejects quarter turns crank Out of gumballs No Quarter gumballs=0 gumballs>0 Gumball sold dispense gumballs TDDB84 Design Patterns Slide 7
8 Testing the GumBall Machine public class GumballMachineTestDrive { public static void main(string[] args) { GumballMachine gumballmachine = new GumballMachine(5); System.out.println(gumballMachine); gumballmachine.insertquarter(); gumballmachine.turncrank(); System.out.println(gumballMachine); gumballmachine.insertquarter(); gumballmachine.ejectquarter(); gumballmachine.turncrank(); System.out.println(gumballMachine); gumballmachine.insertquarter(); gumballmachine.insertquarter(); gumballmachine.turncrank(); gumballmachine.insertquarter(); gumballmachine.turncrank(); gumballmachine.insertquarter(); gumballmachine.turncrank(); System.out.println(gumballMachine); TDDB84 Design Patterns Slide 8
9 You expected it. Isn t it? Joe, we need to reward somehow customers that are constantly buying gums from our machines. Let s implement reward system that gives you a free gum if you already purchased 10 gums. TDDB84 Design Patterns Slide 9
10 What we need to do public class GumballMachine { final static int SOLD_OUT = 0; final static int NO_QUARTER = 1; final static int HAS_QUARTER = 2; final static int SOLD = 3; int state = SOLD_OUT; int count = 0; public void insertquarter(){ //insert quarter code here public void ejectquarter(){ //insert quarter code here public void turncrank(){ //insert quarter code here public void dispense(){ //insert quarter code here TDDB84 Design Patterns Slide 10 You have to add a new WINNER state here...but then, you d have to add a new conditional in every single method to handle the WINNER state, that s a lot of code to modify turncrank() will get especially messy, because you d have to add code to check to see whether you ve got a winner and then switch to either the WINNER state or SOLD state
11 Redesigning the Gumball Machine «interface» State +insertquarter() +ejectquarter() +turncrank() +dispense() SoldState SoldOutState NoQuarterState HasQuarterState +insertquarter() +ejectquarter() +turncrank() +dispense() +insertquarter() +ejectquarter() +turncrank() +dispense() +insertquarter() +ejectquarter() +turncrank() +dispense() +insertquarter() +ejectquarter() +turncrank() +dispense() Has Quarter Inserts quarter ejects quarter turns crank Out of gumballs No Quarter gumballs=0 gumballs>0 Gumball sold TDDB84 Design Patterns Slide 11 dispense gumballs
12 NoQuarterState Has Quarter NoQuarterState Go to HasQuarterState Out of gumballs Inserts quarter ejects quarter turns crank No Quarter +insertquarter() +ejectquarter() +turncrank() +dispense() gumballs=0 gumballs>0 Gumball sold Tell the customer: You haven t inserted a quarter dispense gumballs Tell the customer: You turned, but there's no quarter Tell the customer: You need to pay first TDDB84 Design Patterns Slide 12
13 NoQuarterState public class NoQuarterState implements State { GumballMachine gumballmachine; public NoQuarterState(GumballMachine gumballmachine) { this.gumballmachine = gumballmachine; public void insertquarter() { System.out.println("You inserted a quarter"); gumballmachine.setstate(gumballmachine.gethasquarterstate()); public void ejectquarter() { System.out.println("You haven't inserted a quarter"); public void turncrank() { System.out.println("You turned, but there's no quarter"); public void dispense() { System.out.println("You need to pay first"); public String tostring() { return "waiting for quarter"; TDDB84 Design Patterns Slide 13
14 The State Gumball Machine public class GumballMachine { final static int SOLD_OUT = 0; final static int NO_QUARTER = 1; final static int HAS_QUARTER = 2; final static int SOLD = 3; int state = SOLD_OUT; int count = 0; public class GumballMachine { State soldoutstate; State noquarterstate; State hasquarterstate; State soldstate; State state = soldoutstate; int count = 0; TDDB84 Design Patterns Slide 14
15 The State Gumball Machine public class GumballMachine { State soldoutstate; State noquarterstate; State hasquarterstate; State soldstate; State state = soldoutstate; int count = 0; public GumballMachine(int numbergumballs) { soldoutstate = new SoldOutState(this); noquarterstate = new NoQuarterState(this); hasquarterstate = new HasQuarterState(this); soldstate = new SoldState(this); this.count = numbergumballs; if (numbergumballs > 0) { state = noquarterstate; TDDB84 Design Patterns Slide 15 public void insertquarter() { state.insertquarter(); public void ejectquarter() { state.ejectquarter(); public void turncrank() { state.turncrank(); state.dispense(); void setstate(state state) { this.state = state; void releaseball() { System.out.println("A gumball comes rolling out the slot..."); if (count!= 0) { count = count - 1;
16 The State Design Pattern The Context is the class that can have a number of internal states. The State interface defines common interface for all concrete states; the states all implement the same interface, so they are interchangeable Whenever the request() is made on the Context it is delegated to the state to handle TDDB84 Design Patterns Slide 16 Each concrete class provides its own implementation for a request. In this way, when the Context changes state, its behavior changes as well
17 How about my extra gumball? turns crank winner=0 && gumballs>1 Winner dispense 2 gumballs Has Quarter gumballs=0 gumballs>0inserts quarter ejects quarter turns crank Out of gumballs No Quarter gumballs=0 gumballs>0 Gumball sold dispense gumballs TDDB84 Design Patterns Slide 17
18 The Winner State public class WinnerState implements State { GumballMachine gumballmachine; public WinnerState(GumballMachine gumballmachine) { this.gumballmachine = gumballmachine; public void insertquarter() { System.out.println("Please wait, we're already giving you a Gumball"); public void ejectquarter() { System.out.println("Please wait, we're already giving you a Gumball"); public void turncrank() { System.out.println("Turning again doesn't get you another gumball!"); public void dispense() { System.out.println("YOU'RE A WINNER! You get two gumballs for your quarter"); gumballmachine.releaseball(); if (gumballmachine.getcount() == 0) { gumballmachine.setstate(gumballmachine.getsoldoutstate()); else { gumballmachine.releaseball(); if (gumballmachine.getcount() > 0) { gumballmachine.setstate(gumballmachine.getnoquarterstate()); else { System.out.println("Oops, out of gumballs!"); gumballmachine.setstate(gumballmachine.getsoldoutstate()); TDDB84 Design Patterns Slide 18
19 HasQuarterState public class HasQuarterState implements State { Random randomwinner = new Random(System.currentTimeMillis()); GumballMachine gumballmachine; public HasQuarterState(GumballMachine gumballmachine) { this.gumballmachine = gumballmachine; public void insertquarter() { System.out.println("You can't insert another quarter"); public void ejectquarter() { System.out.println("Quarter returned"); gumballmachine.setstate(gumballmachine.getnoquarterstate()); public void turncrank() { System.out.println("You turned..."); int winner = randomwinner.nextint(10); if ((winner == 0) && (gumballmachine.getcount() > 1)) { gumballmachine.setstate(gumballmachine.getwinnerstate()); else { gumballmachine.setstate(gumballmachine.getsoldstate()); public void dispense() { System.out.println("No gumball dispensed"); TDDB84 Design Patterns Slide 19
20 Gumball Machine TestDrive public class GumballMachineTestDrive { public static void main(string[] args) { GumballMachine gumballmachine = new GumballMachine(10); System.out.println(gumballMachine); gumballmachine.insertquarter(); gumballmachine.turncrank(); gumballmachine.insertquarter(); gumballmachine.turncrank(); System.out.println(gumballMachine); gumballmachine.insertquarter(); gumballmachine.turncrank(); gumballmachine.insertquarter(); gumballmachine.turncrank(); System.out.println(gumballMachine);... TDDB84 Design Patterns Slide 20
21 Prototype 21
22 Prototype Non Software Example 22
23 Motivational Example 23
24 The Prototype Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype 24
25 Motivational Example - Dragons 25
26 The Dragon Classes class DragonPrototype{ protected: string name; public: ; DragonPrototype(){; DragonPrototype(const DragonPrototype& copy); ~DragonPrototype(){; virtual DragonPrototype* clone() const; void setname(string name); string getname() const; class MultipleHeadedDragon : public DragonPrototype{ protected: int numberofheads; public: MultipleHeadedDragon(){; MultipleHeadedDragon(const MultipleHeadedDragon& copy); ~MultipleHeadedDragon(); virtual DragonPrototype* clone() const; void setnumberofheads(int noheads); int getnumberofheads(); ; 26
27 The Dragon Classes DragonPrototype::DragonPrototype(const DragonPrototype& copy){ name = copy.name; void DragonPrototype::setName(string p_name){ name = p_name; DragonPrototype* DragonPrototype::clone() const{ return new DragonPrototype(*this); MultipleHeadedDragon::MultipleHeadedDragon(const MultipleHeadedDragon ©): DragonPrototype(copy){ numberofheads = copy.numberofheads; DragonPrototype* MultipleHeadedDragon::clone() const{ return new MultipleHeadedDragon(*this); void MultipleHeadedDragon::setNumberOfHeads(int noheads){ numberofheads = noheads; ; 27
28 The Dragon Client cout << "Testing the dragon prototypes" << endl; MultipleHeadedDragon *dragonmh = new MultipleHeadedDragon(); dragonmh->setname("ziggy"); DragonPrototype *anotherdragon = dragonmh->clone(); anotherdragon->setname("zippy"); dragonmh->setnumberofheads(7); 28
29 The Tree Stooges class Stooge { public: virtual void slap_stick() = 0; ; class Larry: public Stooge { public: void slap_stick(){ cout << "Larry: poke eyes\n"; ; class Moe: public Stooge{ public: void slap_stick(){ cout << "Moe: slap head\n"; ; class Curly: public Stooge{ public: void slap_stick(){ cout << "Curly: suffer abuse\n"; ; 29
30 The Tree Stooges int main(void) { vector roles; int choice; while (true) { cout << "Larry(1) Moe(2) Curly(3) Go(0): "; cin >> choice; if (choice == 0) break; else if (choice == 1) roles.push_back(new Larry); else if (choice == 2) roles.push_back(new Moe); else roles.push_back(new Curly); for (int i = 0; i < roles.size(); i++) roles[i]->slap_stick(); for (int i = 0; i < roles.size(); i++) delete roles[i]; Larry(1) Moe(2) Curly(3) Go(0): 2 Larry(1) Moe(2) Curly(3) Go(0): 1 Larry(1) Moe(2) Curly(3) Go(0): 3 Larry(1) Moe(2) Curly(3) Go(0): 0 Moe: slap head Larry: poke eyes Curly: suffer abuse 30
31 The Tree Stooges Prototype Variant class Stooge { public: ; virtual Stooge* clone() = 0; virtual void slap_stick() = 0; class Larry: public Stooge { public: Stooge* clone() {return new Larry; void slap_stick(){ cout << "Larry: poke eyes\n"; ; class Moe: public Stooge{ public: Stooge* clone() { return new Moe(*this); void slap_stick(){ cout << "Moe: slap head\n"; ; class Curly: public Stooge{ public: Stooge* clone() {return new Curly; void slap_stick(){ cout << "Curly: suffer abuse\n"; ; 31
32 The Tree Stooges Prototype Variant class Factory { public: static Stooge* make_stooge( int choice ); private: static Stooge* s_prototypes[4]; ; Stooge* Factory::s_prototypes[] = { 0, new Larry, new Moe, new Curly ; Stooge* Factory::make_stooge( int choice ) { return s_prototypes[choice]->clone(); 32
33 The Tree Stooges Prototype Variant Larry(1) Moe(2) Curly(3) Go(0): 2 Larry(1) Moe(2) Curly(3) Go(0): 1 Larry(1) Moe(2) Curly(3) Go(0): 3 Larry(1) Moe(2) Curly(3) Go(0): 0 int main( void ) { vector roles; int choice; while (true) { cout << "Larry(1) Moe(2) Curly(3) Go(0): "; cin >> choice; if (choice == 0) break; roles.push_back( Factory::make_stooge( choice ) ); for (int i=0; i < roles.size(); ++i) roles[i]->slap_stick(); for (int i=0; i < roles.size(); ++i) delete roles[i]; Moe: slap head Larry: poke eyes Curly: suffer abuse 33
34 Prototype Pattern Interaction Diagram 34
35 Applicability! The prototype pattern is used when a system should be independent of how its products are created, composed, and represented; and when the classes to instantiate are specified at run-time; for example, the dynamic loading to avoid building a class hierarchy of factories that parallels the class hierarchy of products; or when instances of a class can have one of only a few different combinations of state 35
36 Consequences! Isolating concrete product classes from the client.! Dynamically adding and removing products at run-time.! Specifying new objects by varying values.! Specifying new objects by varying structure.! Reducing the need for sub-classing.! Configuring an application with classes dynamically.! Main liability: Clone() needed. 36
37 Visitor 37
38 Visitor Non Software Example 38
39 Visitor Pattern Structure 39
40 Motivational Example! Asssume that we have a binary tree with an integer in each node and each leaf! We want: To print all the numbers Compute their sum using separate methods
41 Java Tree Implementation -n : int Tree Leaf Node 1 abstract class Tree { protected int n; abstract void print(); abstract int sum(); class Node extends Tree { private Tree left, right; void print() { left.print(), System.out.print(n); right.print(); int sum() { return left.sum() + n + right.sum(); class Leaf extends Tree { void print() { System.out.print(n); int sum() { return n; 41
42 Tree Visitor interface TreeVisitor { void visit(node node); void visit(leaf leaf); Each tree class must have an accept method abstract class Tree { protected int n; abstract void accept(treevisitor visitor); class Leaf extends Tree { void accept(treevisitor visitor) { visitor.visit(this); class Node extends Tree { private Tree left, right; void accept(treevisitor visitor) { left.accept(visitor), visitor.visit(this); right.accept(visitor); 42
43 Tree Print Visitor interface TreeVisitor { void visit(node node); void visit(leaf leaf); class PrintVisitor extends TreeVisitor { public void visit(node node) { System.out.print(node.getN()); public void visit(leaf leaf) { // Usually the visit methods differ. System.out.print(leaf.getN()); The Client TreeVisitor visitor = new PrintVisitor(); Tree tree =... tree.accept(visitor); 43
44 The Sum Visitor interface TreeVisitor { void visit(node node); void visit(leaf leaf); class SumVisitor implements TreeVisitor { int sum = 0; public void visit(node node) { sum += node.getn(); public void visit(leaf leaf) { sum += leaf.getn(); The Client TreeVisitor v = new SumVisitor(); Tree tree =... tree.accept(visitor); 44
45 Visitor Benefits/Drawbacks! Allows you to add operations to a Composite structure without changing the structure itself! Adding new operations is relativelly easy! The code for operations performed by the Visitor is centralized! The Composite classes encapsulation is broken when the Visitor is used! Because the traversal function is involved, changes to Composite are more difficult. 45
46 Flyweight 46
47 The FlyWeight Design Pattern! The Flyweight pattern describes how to share objects to allow their use at fine granularities without prohibitive cost.! A flyweight" object has state-dependent (extrinsic) part (stored or computed by client objects) state-independent (intrinsic) part (stored in the Flyweight) 48
48 Flyweight 50
49 Flyweight Example public interface IAlien { string Shape { get; //intrinsic state Color GetColor(int madlevel); //extrinsic state public class LargeAlien : IAlien { private string shape = "Large Shape"; //intrinsic state string IAlien.Shape{ get { return shape; Color IAlien.GetColor(int madlevel) //extrinsic state { if (madlevel == 0) return Color.Green; else if (madlevel == 1) return Color.Red; else return Color.Blue; public class LittleAlien : IAlien { private string shape = "Little Shape"; //intrinsic state string IAlien.Shape{ get { return shape; Color IAlien.GetColor(int madlevel) //extrinsic state { if (madlevel == 0) return Color.Red; else if (madlevel == 1) return Color.Blue; else return Color.Green; 51
50 public class AlienFactory { private Dictionary<int,> list = new Dictionary<int,>(); public void SaveAlien(int index, IAlien alien){ list.add(index, alien); Flyweight Example public IAlien GetAlien(int index){ return list[index]; 52
51 class Program { static void Main(string[] args) { //create Aliens and store in factory AlienFactory factory = new AlienFactory(); factory.savealien(0, new LargeAlien()); factory.savealien(1, new LittleAlien()); //now access the flyweight objects IAlien a = factory.getalien(0); IAlien b = factory.getalien(1); //show intrinsic states, all accessed in memory without calculations Console.WriteLine("Showing intrinsic states..."); Console.WriteLine("Alien of type 0 is " + a.shape); Console.WriteLine("Alien of type 1 is " + b.shape); //show extrinsic states, need calculations Console.WriteLine("Showing extrinsic states..."); Console.WriteLine("Alien of type 0 is " + a.getcolor(0).tostring()); Console.WriteLine("Alien of type 0 is " + a.getcolor(1).tostring()); Console.WriteLine("Alien of type 1 is " + b.getcolor(0).tostring()); Console.WriteLine("Alien of type 1 is " + b.getcolor(1).tostring()); 53
52 The Constitution of Software Architects! Encapsulate what varies! Program through an interface not to an implementation! Favor Composition over Inheritance! Classes should be open for extension but closed for modification! Don t call us, we ll call you! A Class should have only one reason to change! Depend upon abstractions. Do not depend upon concrete classes.! Strive for loosely coupled designs between objects that interact 54! Only talk to your friends
53 Seven Layers of Architecture Enterprise-Architecture Global-Architecture System-Architecture OO Architecture Application-Architecture Subsystem Macro-Architecture Frameworks Micro-Architecture Design-Patterns Objects OO Programming 55
54 Antipatterns Sources 56
55 Congratulations: You have now completed TDDB84 57
TDDB84 Design Patterns Lecture 10. Prototype, Visitor
Lecture 10 Prototype, Visitor Peter Bunus Dept of Computer and Information Science Linköping University, Sweden petbu@ida.liu.se Prototype Peter Bunus 2 Prototype Non Software Example Peter Bunus 3 Motivational
More informationTDDB84 Design Patterns Lecture 10. Prototype, Visitor
Lecture 10 Prototype, Visitor Peter Bunus Dept of Computer and Information Science Linköping University, Sweden petbu@ida.liu.se Prototype Peter Bunus 2 1 Prototype Non Software Example Peter Bunus 3 Motivational
More informationThe State Pattern. State Design Pattern: Motivation
The State Pattern The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class. Toni Sellarès Universitat de Girona State Design
More informationTDDB84: Lecture 7. State, Visitor, Interpreter, DSL. fredag 4 oktober 13
TDDB84: Lecture 7 State, Visitor, Interpreter, DSL Creational Prototype SOLID Structural Facade Flyweight Behavioral State Visitor Interpreter Summary The exam LE7 LE8 LE9 Several different patterns GUI
More informationA brief introduction to the State Pattern
A brief introduction to the State Pattern (a software engineering digression) Emily Fortuna (motivating example borrowed from Head First Design Patterns) Suppose electronic gumball machines are the next
More informationState, Flyweight & Proxy Patterns
State, Flyweight & Proxy Patterns Kenneth M. Anderson University of Colorado, Boulder CSCI 4448/5448 Lecture 24 11/12/2009 University of Colorado, 2009 1 Lecture Goals Cover Material from Chapters 10 and
More informationMORE DESIGN PATTERNS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 24 11/10/2011
MORE DESIGN PATTERNS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 24 11/10/2011 Kenneth M. Anderson, 2011 1 Goals of the Lecture Cover the material in Chapters 18 & 19 of our textbook Observer
More informationThe Strategy Pattern Design Principle: Design Principle: Design Principle:
Strategy Pattern The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Design
More informationBehavioral Design Patterns. CSE260, Computer Science B: Honors Stony Brook University
Behavioral Design Patterns CSE260, Computer Science B: Honors Stony Brook University http://www.cs.stonybrook.edu/~cse260 Behavioral Design Patterns Design patterns that identify common communication patterns
More informationHowever, some time later, suppose you decided to use DFS instead of BFS. So, you ll have to modify the code as follows:
Strategy Pattern Example Problem Scenario Suppose, you need to traverse the files and folders inside a given directory. So, you ve decided to apply the BFS algorithm. Following is a sample pseudo-code
More informationModellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico
Modellistica Medica Maria Grazia Pia INFN Genova Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico 2002-2003 Lezione 9 OO modeling Design Patterns Structural Patterns Behavioural Patterns
More informationCSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich
CSCD01 Engineering Large Software Systems Design Patterns Joe Bettridge Winter 2018 With thanks to Anya Tafliovich Design Patterns Design patterns take the problems consistently found in software, and
More informationCSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich
CSCD01 Engineering Large Software Systems Design Patterns Joe Bettridge Winter 2018 With thanks to Anya Tafliovich Design Patterns Design patterns take the problems consistently found in software, and
More informationDesign Patterns: State, Bridge, Visitor
Design Patterns: State, Bridge, Visitor State We ve been talking about bad uses of case statements in programs. What is one example? Another way in which case statements are sometimes used is to implement
More informationSDC Design patterns GoF
SDC Design patterns GoF Design Patterns The design pattern concept can be viewed as an abstraction of imitating useful parts of other software products. The design pattern is a description of communicating
More informationTDDB84: Lecture 6. Adapter, Bridge, Observer, Chain of Responsibility, Memento, Command. fredag 4 oktober 13
TDDB84: Lecture 6 Adapter, Bridge, Observer, Chain of Responsibility, Memento, Command Creational Abstract Factory Singleton Builder Structural Composite Proxy Bridge Adapter Template method Behavioral
More informationTDDB84 Design Patterns Lecture 05. Builder, Singleton, Proxy. pelab
Lecture 05 Builder, Singleton, Proxy Peter Bunus Dept of Computer and Information Science Linköping University, Sweden petbu@ida.liu.se The Constitution of Software Architects Encapsulate what varies.
More informationDesign Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)
Design Pattern CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.) A. Design Pattern Design patterns represent the best practices used by experienced
More informationThe State and Strategy Patterns. Design Patterns In Java Bob Tarr
The State and Strategy Patterns The State Pattern Intent Allow an object to alter its behavior when its internal state changes. The object will appear to change its class. Motivation 2 The State Pattern
More informationObject-Oriented Design
Object-Oriented Design Lecturer: Raman Ramsin Lecture 20: GoF Design Patterns Creational 1 Software Patterns Software Patterns support reuse of software architecture and design. Patterns capture the static
More informationIntroduction to Design Patterns
Introduction to Design Patterns First, what s a design pattern? a general reusable solution to a commonly occurring problem within a given context in software design It s not a finished design that can
More informationLast Lecture. Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/ Spring Semester, 2005
1 Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/6448 - Spring Semester, 2005 2 Last Lecture Design Patterns Background and Core Concepts Examples
More informationUsing Design Patterns in Java Application Development
Using Design Patterns in Java Application Development ExxonMobil Research & Engineering Co. Clinton, New Jersey Michael P. Redlich (908) 730-3416 michael.p.redlich@exxonmobil.com About Myself Degree B.S.
More informationLast Lecture. Lecture 26: Design Patterns (part 2) State. Goals of Lecture. Design Patterns
Lecture 26: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 6448 - Spring Semester, 2003 Last Lecture Design Patterns Background and Core Concepts Examples Singleton,
More informationWriting your own Java I/O Decorator p. 102 Tools for your Design Toolbox p. 105 Exercise Solutions p. 106 The Factory Pattern Baking with OO
Intro to Design Patterns Welcome to Design Patterns: Someone has already solved your problems The SimUDuck app p. 2 Joe thinks about inheritance... p. 5 How about an interface? p. 6 The one constant in
More informationKeywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.
Comparative Study In Utilization Of Creational And Structural Design Patterns In Solving Design Problems K.Wseem Abrar M.Tech., Student, Dept. of CSE, Amina Institute of Technology, Shamirpet, Hyderabad
More informationComposite Pattern. IV.4 Structural Pattern
IV.4 Structural Pattern Motivation: Compose objects to realize new functionality Flexible structures that can be changed at run-time Problems: Fixed class for every composition is required at compile-time
More informationINTERFACES IN JAVA. Prof. Chris Jermaine
INTERFACES IN JAVA Prof. Chris Jermaine cmj4@cs.rice.edu 1 Before We Begin... Couple more slides on checkers, then a challenge There is also an AIntelligence This encapsulates the idea of a checker-playing
More informationSoftware Design Fundamentals. CSCE Lecture 11-09/27/2016
Software Design Fundamentals CSCE 740 - Lecture 11-09/27/2016 Today s Goals Define design Introduce the design process Overview of design criteria What results in a good design? Gregory Gay CSCE 740 -
More informationDesign Patterns Reid Holmes
Material and some slide content from: - Head First Design Patterns Book - GoF Design Patterns Book Design Patterns Reid Holmes GoF design patterns $ %!!!! $ "! # & Pattern vocabulary Shared vocabulary
More informationA few important patterns and their connections
A few important patterns and their connections Perdita Stevens School of Informatics University of Edinburgh Plan Singleton Factory method Facade and how they are connected. You should understand how to
More informationPlan. A few important patterns and their connections. Singleton. Singleton: class diagram. Singleton Factory method Facade
Plan A few important patterns and their connections Perdita Stevens School of Informatics University of Edinburgh Singleton Factory method Facade and how they are connected. You should understand how to
More informationThink of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern
Think of drawing/diagramming editors ECE450 Software Engineering II Drawing/diagramming editors let users build complex diagrams out of simple components The user can group components to form larger components......which
More informationDesign Patterns. Decorator. Oliver Haase
Design Patterns Decorator Oliver Haase 1 Motivation Your task is to program a coffee machine. The machine brews plain coffee, coffee with cream, sugar, sweetener, and cinnamon. A plain coffee costs 0,90,
More informationdrawobject Circle draw
25 THE VISITOR PATTERN The Visitor pattern turns the tables on our object-oriented model and creates an external class to act on data in other classes. This is useful if there are a fair number of instances
More informationObject-oriented Software Design Patterns
Object-oriented Software Design Patterns Concepts and Examples Marcelo Vinícius Cysneiros Aragão marcelovca90@inatel.br Topics What are design patterns? Benefits of using design patterns Categories and
More informationDesign Patterns. Claus Jensen
Design Patterns Claus Jensen What is a Design Pattern? A design pattern Abstracts a recurring design structure Distils design experience Promotes reuse of design and code Gives an opportunity to see how
More informationTuesday, October 4. Announcements
Tuesday, October 4 Announcements www.singularsource.net Donate to my short story contest UCI Delta Sigma Pi Accepts business and ICS students See Facebook page for details Slide 2 1 Design Patterns Design
More informationSoftware Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.
Software Design Patterns Jonathan I. Maletic, Ph.D. Department of Computer Science Kent State University J. Maletic 1 Background 1 Search for recurring successful designs emergent designs from practice
More informationDesign Patterns Reid Holmes
Material and some slide content from: - Head First Design Patterns Book - GoF Design Patterns Book Design Patterns Reid Holmes GoF design patterns $ %!!!! $ "! # & Pattern vocabulary Shared vocabulary
More informationEPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns
EPL 603 TOPICS IN SOFTWARE ENGINEERING Lab 6: Design Patterns Links to Design Pattern Material 1 http://www.oodesign.com/ http://www.vincehuston.org/dp/patterns_quiz.html Types of Design Patterns 2 Creational
More informationDesign Pattern- Creational pattern 2015
Creational Patterns Abstracts instantiation process Makes system independent of how its objects are created composed represented Encapsulates knowledge about which concrete classes the system uses Hides
More informationThe GoF Design Patterns Reference
The GoF Design Patterns Reference Version.0 / 0.0.07 / Printed.0.07 Copyright 0-07 wsdesign. All rights reserved. The GoF Design Patterns Reference ii Table of Contents Preface... viii I. Introduction....
More informationTDDB84: Lecture 09. SOLID, Language design, Summary. fredag 11 oktober 13
TDDB84: Lecture 09 SOLID, Language design, Summary SOLID Single responsibility principle Open/closed principle Liskov substitution principle Interface segregation principle Depency inversion principle
More informationProduced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar
Design Patterns MSc in Communications Software Produced by Eamonn de Leastar (edeleastar@wit.ie) Department of Computing, Maths & Physics Waterford Institute of Technology http://www.wit.ie http://elearning.wit.ie
More informationDesign patterns. Jef De Smedt Beta VZW
Design patterns Jef De Smedt Beta VZW Who Beta VZW www.betavzw.org Association founded in 1993 Computer training for the unemployed Computer training for employees (Cevora/Cefora) 9:00-12:30 13:00-16:00
More informationOODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00
OODP Session 4 Session times PT group 1 Monday 18:00 21:00 room: Malet 403 PT group 2 Thursday 18:00 21:00 room: Malet 407 FT Tuesday 13:30 17:00 room: Malet 404 Email: oded@dcs.bbk.ac.uk Web Page: http://www.dcs.bbk.ac.uk/~oded
More informationLecture Material. Design Patterns. Visitor Client-Server Factory Singleton
Lecture Material Design Patterns Visitor Client-Server Factory Singleton 1 Design Patterns Pattern A named generalization describing the elements and relationships of a solution for a commonly occurring
More informationPRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 10. PUTTING IT ALL TOGETHER. Are we there yet?
PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 10. PUTTING IT ALL TOGETHER Are we there yet? Developing software, OOA&D style You ve got a lot of new tools, techniques, and ideas about how to develop
More informationDesign Patterns. An introduction
Design Patterns An introduction Introduction Designing object-oriented software is hard, and designing reusable object-oriented software is even harder. Your design should be specific to the problem at
More informationTDDB84: Lecture 5. Singleton, Builder, Proxy, Mediator. fredag 27 september 13
TDDB84: Lecture 5 Singleton, Builder, Proxy, Mediator Creational Abstract Factory Singleton Builder Structural Composite Proxy Bridge Adapter Template method Behavioral Iterator Mediator Chain of responsibility
More informationObject Oriented Methods with UML. Introduction to Design Patterns- Lecture 8
Object Oriented Methods with UML Introduction to Design Patterns- Lecture 8 Topics(03/05/16) Design Patterns Design Pattern In software engineering, a design pattern is a general repeatable solution to
More informationImprove Your SystemVerilog OOP Skills
1 Improve Your SystemVerilog OOP Skills By Learning Principles and Patterns Jason Sprott Verilab Email: jason.sprott@verilab.com www.verilab.com Experts in SV Learn Design Patterns 2 Look, you don t need
More informationCSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich
CSCD01 Engineering Large Software Systems Design Patterns Joe Bettridge Winter 2018 With thanks to Anya Tafliovich Design Patterns Design patterns take the problems consistently found in software, and
More informationProgrammazione. Prof. Marco Bertini
Programmazione Prof. Marco Bertini marco.bertini@unifi.it http://www.micc.unifi.it/bertini/ Design patterns Design patterns are bug reports against your programming language. - Peter Norvig What are design
More informationDesign Patterns Design patterns advantages:
Design Patterns Designing object-oriented software is hard, and designing reusable object oriented software is even harder. You must find pertinent objects factor them into classes at the right granularity
More informationCS342: Software Design. November 21, 2017
CS342: Software Design November 21, 2017 Runnable interface: create threading object Thread is a flow of control within a program Thread vs. process All execution in Java is associated with a Thread object.
More informationThe Visitor Pattern. Object interfaces are fixed and diverse, Need to allow new operations, without coupling.
The Visitor Pattern Visitor Pattern represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on
More informationTesting3. State-based Testing
Testing3 State-based testing Inheritance Testing interacting classes Communication diagrams Object relation graph (ORD) Regression testing GUI Testing 1 State-based Testing Natural representation with
More informationCS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4
CS32 - Week 4 Umut Oztok Jul 15, 2016 Inheritance Process of deriving a new class using another class as a base. Base/Parent/Super Class Derived/Child/Sub Class Inheritance class Animal{ Animal(); ~Animal();
More informationChapter 6. Object- Oriented Programming Part II
Chapter 6 Object- Oriented Programming Part II 6: Preview issues surrounding the object creational process the Abstract Factory design pattern private and protected inheritance multiple inheritance comparison
More informationTecniche di Progettazione: Design Patterns
Tecniche di Progettazione: Design Patterns GoF: Builder, Chain Of Responsibility, Flyweight 1 Design patterns, Laura Semini, Università di Pisa, Dipartimento di Informatica. Builder 2 Design patterns,
More informationSoftware Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns
Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns Today we are going to talk about an important aspect of design that is reusability of design. How much our old design
More informationTDDB84 Design Patterns Lecture 09. Interpreter, Facade
Lecture 09 Interpreter, Facade Peter Bunus Dept of Computer and Information Science Linköping University, Sweden petbu@ida.liu.se Interpreter Peter Bunus 2 1 The Interpreter Non Software Example Musical
More informationDesign Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1
What is a Design Pattern? Each pattern Describes a problem which occurs over and over again in our environment,and then describes the core of the problem Novelists, playwrights and other writers rarely
More informationUniversity of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Inheritance II Lecture 23, Thu Mar 30 2006 based on slides by Kurt Eiselt http://www.cs.ubc.ca/~tmm/courses/cpsc111-06-spr
More informationDesign Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011
Design Patterns Lecture 2 Manuel Mastrofini Systems Engineering and Web Services University of Rome Tor Vergata June 2011 Structural patterns Part 2 Decorator Intent: It attaches additional responsibilities
More informationBuilding custom components IAT351
Building custom components IAT351 Week 1 Lecture 1 9.05.2012 Lyn Bartram lyn@sfu.ca Today Review assignment issues New submission method Object oriented design How to extend Java and how to scope Final
More informationCS1004: Intro to CS in Java, Spring 2005
CS1004: Intro to CS in Java, Spring 2005 Lecture #13: Java OO cont d. Janak J Parekh janak@cs.columbia.edu Administrivia Homework due next week Problem #2 revisited Constructors, revisited Remember: a
More informationTDDB84 Design Patterns Lecture 02. Factory Method, Decorator
Lecture 02 Factory Method, Decorator Peter Bunus Dept of Computer and Information Science Linköping University, Sweden petbu@ida.liu.se Factory Method Pattern Peter Bunus 2 1 Time for Lunch Joe, with the
More informationCompositional Design Principles
Chapter 16 Compositional Design Principles Learning Objectives In this chapter, I will more formally introduce the three principles that form the 3-1- 2 process. The learning focus is understanding how
More informationDesign to interfaces. Favor composition over inheritance Find what varies and encapsulate it
Design Patterns The Gang of Four suggests a few strategies for creating good o-o designs, including Façade Design to interfaces. Favor composition over inheritance Find what varies and encapsulate it One
More informationCS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux
CS 231 Data Structures and Algorithms Fall 2018 Binary Search Trees Lecture 23 October 29, 2018 Prof. Zadia Codabux 1 Agenda Ternary Operator Binary Search Tree Node based implementation Complexity 2 Administrative
More information! labs last week. ! still time to work through lab 7 (midterm correction) ! can earn back up to 5 out of 70 points
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Interfaces, Inheritance Lecture 22, Tue Mar 28 2006! labs last week News! still time to work through lab 7 (midterm
More informationFollowing is the general form of a typical decision making structure found in most of the programming languages:
Decision Making Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined
More informationObject-Oriented Oriented Programming
Object-Oriented Oriented Programming Composite Pattern CSIE Department, NTUT Woei-Kae Chen Catalog of Design patterns Creational patterns Abstract Factory, Builder, Factory Method, Prototype, Singleton
More informationLecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University
Lecture 3 COMP1006/1406 (the Java course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments 1 (graded) & 2 3 (available now) & 4 (tomorrow) a quick look back primitive data types
More informationDesign Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011
Design Patterns Lecture 1 Manuel Mastrofini Systems Engineering and Web Services University of Rome Tor Vergata June 2011 Definition A pattern is a reusable solution to a commonly occurring problem within
More informationCompiler Design Spring 2017
Compiler Design Spring 2017 Patterns (again) Dr. Zoltán Majó Compiler Group Java HotSpot Virtual Machine Oracle Corporation Why? Remember questionnaire (from the beginning of the semester)? Question 7:
More informationCSCI 102 Fall 2010 Exam #1
Name: USC Username: CSCI 102 Fall 2010 Exam #1 Problems Problem #1 (14 points) Problem #2 (15 points) Problem #3 (20 points) Problem #4 (16 points) Problem #5 (35 points) Total (100 points) Problem 1 Short
More informationIntroduction to Software Engineering: Object Design I Reuse & Patterns
Introduction to Software Engineering: Object Design I Reuse & Patterns John T. Bell Department of Computer Science University of Illinois, Chicago Based on materials from Bruegge & DuToit 3e, Chapter 8,
More informationCOURSE 4 DESIGN PATTERNS
COURSE 4 DESIGN PATTERNS PREVIOUS COURSE Creational Patterns Factory Method defines an interface for creating objects, but lets subclasses decide which classes to instantiate Abstract Factory provides
More informationMore on Classes. The job of this method is to return a String representation of the object. Here is the tostring method from the Time class:
More on Classes tostring One special method in Java is the tostring method. The method (regardless of which class it s added to) has the following prototype: public String tostring(); The job of this method
More informationAbstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.
a and Interfaces Class Shape Hierarchy Consider the following class hierarchy Shape Circle Square Problem AND Requirements Suppose that in order to exploit polymorphism, we specify that 2-D objects must
More informationDesign Patterns. Visitor Pattern. Hans Vangheluwe and Alexandre Denault
Design Patterns Visitor Pattern Hans Vangheluwe and Alexandre Denault 3D Room Scene Graphs Universe Room 1 Room 2 Desk Bed Wardrobe Books Lamp Doors Drawers What if? I want to print out the content of
More informationLecture 8 " INPUT " Instructor: Craig Duckett
Lecture 8 " INPUT " Instructor: Craig Duckett Assignments Assignment 2 Due TONIGHT Lecture 8 Assignment 1 Revision due Lecture 10 Assignment 2 Revision Due Lecture 12 We'll Have a closer look at Assignment
More informationCreational Design Patterns
Creational Design Patterns Creational Design Patterns Structural Design Patterns Behavioral Design Patterns GoF Design Pattern Categories Purpose Creational Structural Behavioral Scope Class Factory Method
More informationSoftware Development
Software Development and Professional Practice Object-Oriented Design Part the Third: (in which we revisit several parts of the OOA&D process, culminating in some general design principles) Object Oriented
More informationCOSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14
COSC 2P91 Introduction Part Deux Week 1b Brock University Brock University (Week 1b) Introduction Part Deux 1 / 14 Source Files Like most other compiled languages, we ll be dealing with a few different
More informationClass and Functions. Reusable codes
Class and Functions Reusable codes Object Oriented Design First concept of object oriented programming is emerged at 60 s. Smalltalk language which is introduced at 1972 was first object oriented programming
More informationUniversity of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner Inheritance II Lecture 34, Mon Apr 12 2010 borrowing from slides by Kurt Eiselt http://www.cs.ubc.ca/~tmm/courses/111-10
More informationA Case Study of Gang of Four (GoF) Patterns : Part 7
A Case Study of Gang of Four (GoF) Patterns : Part 7 d.schmidt@vanderbilt.edu www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University
More informationWeek 7. Statically-typed OO languages: C++ Closer look at subtyping
C++ & Subtyping Week 7 Statically-typed OO languages: C++ Closer look at subtyping Why talk about C++? C++ is an OO extension of C Efficiency and flexibility from C OO program organization from Simula
More informationCSE 70 Final Exam Fall 2009
Signature cs70f Name Student ID CSE 70 Final Exam Fall 2009 Page 1 (10 points) Page 2 (16 points) Page 3 (22 points) Page 4 (13 points) Page 5 (15 points) Page 6 (20 points) Page 7 (9 points) Page 8 (15
More informationDesign Pattern and Software Architecture: IV. Design Pattern
Design Pattern and Software Architecture: IV. Design Pattern AG Softwaretechnik Raum E 3.165 Tele.. 60-3321 hg@upb.de IV. Design Pattern IV.1 Introduction IV.2 Example: WYSIWYG Editor Lexi IV.3 Creational
More informationSurvey #2. Variable Scope. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Scope Static.
University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Scope Static Readings This Week: Ch 8.3-8.8 and into Ch 9.1-9.3 (Ch 9.3-9.8 and Ch 11.1-11.3 in old 2 nd ed) (Reminder: Readings
More informationQuestion: Total Points: Score:
CS 170 Exam 1 Section 001 Fall 2014 Name (print): Instructions: Keep your eyes on your own paper and do your best to prevent anyone else from seeing your work. Do NOT communicate with anyone other than
More informationProject 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently.
Project 1 Computer Science 2334 Spring 2016 This project is individual work. Each student must complete this assignment independently. User Request: Create a simple movie data system. Milestones: 1. Use
More informationDesign Patterns. CSC207 Winter 2017
Design Patterns CSC207 Winter 2017 Design Patterns A design pattern is a general description of the solution to a well-established problem using an arrangement of classes and objects. Patterns describe
More informationWhat is Design Patterns?
Paweł Zajączkowski What is Design Patterns? 1. Design patterns may be said as a set of probable solutions for a particular problem which is tested to work best in certain situations. 2. In other words,
More information