Design Patterns: Brief Examples

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
 Recent researches show that predicative programming can be used to specify OO concepts including classes, objects, interfaces, methods, single and multiple.
Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)
Plab – Tirgul 12 Design Patterns
(c) 2009 University of California, Irvine – André van der Hoek1June 13, 2015 – 21:42:16 Informatics 122 Software Design II Lecture 8 André van der Hoek.
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
DESIGN PATTERNS Redesigning Applications And
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
(c) 2010 University of California, Irvine – André van der Hoek1June 29, 2015 – 08:55:05 Informatics 122 Software Design II Lecture 8 André van der Hoek.
Chapter 8 Object Design Reuse and Patterns. Finding Objects The hardest problems in object-oriented system development are: –Identifying objects –Decomposing.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Patterns Discussion of pages: xi-11 Sections: Preface, Forward, Chapter
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Design Patterns.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
Strategy Design Patterns CS 590L - Sushil Puradkar.
GoF Sections Design Problems and Design Patterns.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Patterns Introduction General and reusable solutions to common problems in software design SoftUni Team Software University
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
ECE450S – Software Engineering II
05/26/2004www.indyjug.net1 Indy Java User’s Group May Knowledge Services, Inc.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Design Pattern Catalog - Page L3-1 PS95&96-MEF-L10-1 Dr. M.E. Fayad Creationa.
Design Patterns References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++
CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.
Design Patterns Introduction
Design Patterns Introduction “Patterns are discovered, not invented” Richard Helm.
Advanced Object-oriented Design Patterns Creational Design Patterns.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns: MORE Examples
Strategy Design Pattern
Chapter 10 Design Patterns.
Software Design Patterns
MPCS – Advanced java Programming
Introduction to Design Patterns
Design Patterns Lecture part 2.
Factory Patterns 1.
Presented by Igor Ivković
Software Engineering Lecture 7 - Design Patterns
Informatics 122 Software Design II
Object Oriented Design Patterns - Structural Patterns
CSC 480 Software Engineering
Behavioral Design Pattern
Structural Patterns: Adapter and Bridge
Strategy Design Pattern
Introduction to Design Patterns
Informatics 122 Software Design II
CSC 480 Software Engineering
Chapter 8, Design Patterns Introduction
Presented by Igor Ivković
Presentation transcript:

Design Patterns: Brief Examples

Recall What a design pattern is “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.” – Christopher Alexander, et al. A Pattern Language, Oxford Univ. Press, New York, 1977.

What is a design pattern? An OO design pattern names, explains, and evaluates an important, recurring design in OO systems.

Common Causes of Redesign Creating an object by specifying a class explicitly. Dependence on specific operations. Dependence on hardware and software platform. Dependence on object representations or implementations. Algorithmic dependencies. Tight coupling. Extending functionality by subclassing. Inability to alter classes conveniently.

Organization of design patterns Design patterns can be placed into one of three broad categories: Creational Structural Behavioral Design patterns can have class scope or object scope.

Essential Elements of a Pattern Pattern name Descriptive name indicative of the essence of the pattern and the problem for which it applies. Problem Describes when to apply the pattern. Solution Describes the elements of the design, their relationships, responsibilities and collaborations. It’s like a template that can be applied to many different situations. Consequences Describes results and trade-offs of applying the pattern.

An Example design pattern: Singleton Intent Ensure a class has one instance that has a global point of access. Motivation Some classes need to have exactly one instance. Make the class responsible for Keeping track of its sole instance, Ensuring that no other instance can be created, and Providing access to the instance. Applicability: Use the Singleton pattern when There must be exactly one instance of a class that is accessible to clients from a well-known access point. The sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying code.

Singleton design pattern (cont’d) Some Consequences (benefits): Controlled access to sole instance. Permits refinement of operations and representation. The Singleton class may be subclassed to configure the application with an instance of the class you need at run-time. Permits a variable number of instances (if you change your mind).

Singleton example in C++ (Declaration) class Singleton { public: static Singleton* Instance(); protected: Singleton(); private: static Singlton* _instance; };

Singleton example in C++ (implementation) Singleton* Singleton::_instance=0; Singleton* Singleton::Instance (){ if (_instance ==0) { _instance= new Singleton: } return _instance; }

Java – Bank assignment (no singleton) import banking.*; public class TestBanking { public static void main(String[] args) { Bank bank = new Bank(); bank.addCustomer("Jane", "Simms"); bank.addCustomer("Owen", "Bryant"); bank.addCustomer("Tim", "Soley"); bank.addCustomer("Maria", "Soley"); for ( int i = 0; i < bank.getNumOfCustomers(); i++ ) { Customer customer = bank.getCustomer(i); System.out.println("Customer [" + (i+1) + "] is " + customer.getLastName() + ", " + customer.getFirstName()); }

Java – Bank assignment (no singleton) package banking; public class Bank { private static int MAX_CUSTOMERS = 10; private Customer[] customers; private int numberOfCustomers; public Bank() { customers = new Customer[MAX_CUSTOMERS]; numberOfCustomers = 0; } public void addCustomer(String f, String l) { int i = numberOfCustomers++; customers[i] = new Customer(f, l); public Customer getCustomer(int customer_index) { return customers[customer_index]; public int getNumOfCustomers() { return numberOfCustomers;

Singleton example in Java (declaration) package banking.domain; /** * The Bank class implements the Singleton design pattern, because * there should be only one bank object. */ public class Bank { * The class variable that holds the single Bank instance. private static final Bank bankInstance = new Bank(); public static Bank getBank() { return bankInstance; } private static final int MAX_CUSTOMERS = 10; private static final double SAVINGS_RATE = 3.5; private Customer[] customers; private int numberOfCustomers; private Bank() { customers = new Customer[MAX_CUSTOMERS]; numberOfCustomers = 0; // Other code here . . .

Singleton example in Java import banking.domain.*; public class TestBanking { public static void main(String[] args) { Bank bank = Bank.getBank(); Customer customer; Account account; // Create two customers and their accounts bank.addCustomer("Jane", "Simms"); customer = bank.getCustomer(0); customer.addAccount(new SavingsAccount(500.00, 0.05)); customer.addAccount(new CheckingAccount(200.00, 500.00)); bank.addCustomer("Owen", "Bryant"); customer = bank.getCustomer(1); customer.addAccount(new CheckingAccount(200.00)); // Other code here . . . }

Singleton- an Example Creational Pattern Creational design patterns Abstract the instantiation process. Help make a system independent of how its objects are created, composed, and represented. Have two recurring themes: They encapsulate knowledge about concrete classes the system uses. They hide how instances of these classes are created and put together.

structural design patterns Are concerned with composing classes and objects to form larger structures. Structural class patterns use inheritance to compose interfaces or implementations. Structural object patterns describe ways to compose objects to realize new functionality.

Adapter- An example structural pattern Intent Convert the interface of a class into another interface clients expect. Adapter lets classes work together that have incompatible interfaces. Motivation A class designed for reuse isn’t reusable because its interface doesn’t match domain-specific interfaces that an application requires. Applicability When a class you want to use has an interface that doesn’t match what you need. When you want to create a reusable class the cooperates with unrelated or unforeseen classes. When you need to use existing subclasses, but it’s impractical to adapt their interface by subclassing every one.

Adapter- An example structural pattern Consequences Class adapter Adapts Adaptee to Target by committing to a concrete Adaptee class. Lets Adapter override some Adaptee’s behavior. Object adapter Lets a single Adapter work with the Adaptee and all of its subclasses. Makes it harder to override Adaptee behavior.

Behavioral design patterns Concerned with algorithms and the assignment of responsibilities between objects Describe the patterns of communication between objects. Characterize complex control flow. Behavioral class patterns use inheritance to distribute behavior between classes. Behavioral object patterns use composition.

Strategy-an example object behavioral design pattern Intent Define a family of algorithms, encapsulate each one, and make them interchangeable. Motivation Many algorithms may exist for a task, but it is impractical to “hard-wire” all such algorithms into classes that require them. We can avoid problems by defining classes that encapsulate different algorithms. An algorithm encapsulated in this way is called a strategy. Applicability When many related classes differ only in their behavior. When you need different variants of an algorithm When an algorithm uses data that clients shouldn’t see. When a class defines many behaviors that appear as multiple conditional statements in its operations.

Strategy-an example object behavioral design pattern Consequences Creates families of related algorithms. Inheritance can factor out common functionality. Provides an alternative to subclassing. Can eliminate conditional statements. Provides a choice of implementations. Clients must be aware of different Strategies. Communication overhead. Increased number of objects.

Summary Design patterns are used in many creative activities to attack recurring problems. OOP is one such activity for which design patterns are being catalogued. Design patterns enhance reusability and can reduce time required to produce a solution.

Summary Design patterns can be placed into one of three broad categories: Creational Structural Behavioral Some are class patterns, some are object patterns. In order to use them successfully Become familiar with them. Consider how they solve design problems. Scan Intent sections. Study how they interrelate. Study patterns of like purpose. Examine causes of redesign to see whether applicable to your problem. Consider what should be variable in your design.