Software Design Lecture : 28.

Slides:



Advertisements
Similar presentations
GoF State Pattern Aaron Jacobs State(305) Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
Advertisements

Creational Patterns (2) CS350/SE310 Fall, Lower the Cost of Maintenance Economic Goal Coupling-Cohesion, Open-Close, Information-Hiding, Dependency.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:
 Consists of Creational patterns  Each generator pattern has a Client, Product, and Generator.  The Generator needs at least one operation that creates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
More design patterns The ADAPTER Pattern Actions and the COMMAND Pattern Actions and the COMMAND Pattern The FACTORY METHOD Pattern The PROXY Pattern The.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
+ Informatics 122 Software Design II Lecture 8 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Strategy Design Patterns CS 590L - Sushil Puradkar.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Mohammed Al-Dhelaan CSci 253 Object Oriented Design Instructor: Brad Taylor 06/02/2009 Factory Method Pattern.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
Design Patterns -- Omkar. Introduction  When do we use design patterns  Uses of design patterns  Classification of design patterns  Creational design.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
Design Patterns Yonglei Tao. Design Patterns  A design pattern describes a recurring design problem, a solution, and the context in which that solution.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
STATE PATTERN Presented by Bharavi Mishra Bharavi Mishraise
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
The State Design Pattern A behavioral design pattern. Shivraj Persaud
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Modern Programming Tools And Techniques-I
Design Patterns: MORE Examples
Object-Oriented Design
Design Patterns: Brief Examples
Unit II-Chapter No. : 5- design Patterns
Classes (Part 1) Lecture 3
Strategy Design Pattern
MPCS – Advanced java Programming
Design Patterns C++ Java C#.
Low Budget Productions, LLC
Factory Patterns 1.
Final and Abstract Classes
Inheritance and Polymorphism
Creational Pattern: Prototype
Design Patterns C++ Java C#.
Software Design and Architecture
Advanced Programming Behnam Hatami Fall 2017.
Object Oriented Analysis and Design
Behavioral and Structural Patterns
State Design Pattern 1.
Object Oriented Design Patterns - Creational Patterns
METHOD OVERRIDING in JAVA
Multithreaded Programming
Software Design Lecture : 12.
Design pattern Lecture 9.
Lesson 5: More on Creational Patterns
Software Design Lecture : 35.
Software Design Lecture : 27.
Software Design Lecture : 39.
Software Design Lecture : 38.
Presentation transcript:

Software Design Lecture : 28

Categories of Design Patterns Creational Structural Behavioral

Creational Design Patterns Deal with one of the most commonly performed tasks in an OO application, the creation of objects. Support a uniform, simple, and controlled mechanism to create objects. Allow the encapsulation of the details about what classes are instantiated and how these instances are created. Encourage the use of interfaces, which reduces coupling.

Factory Pattern Problem Statement: When a client object does not know which class to instantiate, it can make use of the factory method to create an instance of an appropriate class from a class hierarchy or a family of related classes. The factory method may be designed as part of the client itself or in a separate class. The class that contains the factory method or any of its subclasses decides on which class to select and how to instantiate it.

Factory Pattern - Description If there exist class hierarchies i-e super / sub classes then client object usually know which class /sub class to instantiate but at times client object know that it needs t instantiate the object but of which class it does not know ; it may be due to many factors The state of the running application Application configuration settings Expansion of requirements or enhancements In such cases, an application object needs to implement the class selection criteria to instantiate an appropriate class from the hierarchy to access its services

Disadvantage of this Approach Because every application object that intends to use the services offered by the class hierarchy needs to implement the class selection criteria, it results in a high degree of coupling between an application object and the service provider class hierarchy. Whenever the class selection criteria change, every application object that uses the class hierarchy must undergo a corresponding change. Shown in the next picture

Solution :Factory Pattern “Factory Pattern defines an interface for creating the object but let the subclass decide which class to instantiate. Factory pattern let the class defer instantiation to the sub class “ It works on the principle of delegation

Solution Description Encapsulating the functionality required, to select and instantiate an appropriate class separately. Selects an appropriate class from a class hierarchy based on the application context and other influencing factor Instantiates the selected class and returns it as an instance of the parent class type This approach will decouple the client from object creation

Factory Method Application objects can make use of the factory method to get access to the appropriate class instance. This eliminates the need for an application object to deal with the varying class selection criteria. Besides the class selection criteria, the factory method also implements any special mechanisms required to instantiate the selected class. This is applicable if different classes in the hierarchy need to be instantiated in different ways. The factory method hides these details from application objects and eliminates the need for them to deal with these intricacies.

Possible Class Diagram of Factory Pattern

Simple Example with Java Code Problem Statement: We want the user to enter the name in either “first name last name or last name, first name” format. We have made the assumption that there will always be a comma between last name and first name and space between first name last name

Class Diagram NameFactory Application Object Namer Passes Parameter Firstfirst Lastfist Handles Creation of Subclass object and return relevant instance of subclass

Sample Code in Java

Namer Class class Namer { protected String last; //store last name here protected String first; //store first name here public String getFirst() return first; //return first name } public String getLast() { return last; //return last name }}

class FirstFirst extends Namer { //split first last public FirstFirst(String s) { int i = s.lastIndexOf(" "); //find sep space if (i > 0) //left is first name first = s.substring(0, i).trim(); //right is last name last =s.substring(i+1).trim(); } else first = “”; // put all in last name last = s; // if no space }}}

class LastFirst extends Namer { //split last, first public LastFirst(String s) { int i = s.indexOf(","); //find comma if (i > 0) //left is last name last = s.substring(0, i).trim(); //right is first name first = s.substring(i + 1).trim(); } else { last = s; // put all in last name first = ""; // if no comma }}}

Building the Factory

class NameFactory { //returns an instance of LastFirst or FirstFirst //depending on whether a comma is found public Namer getNamer(String entry) { int i = entry.indexOf(","); //comma determines name order if (i>0) return new LastFirst(entry); //return one class else return new FirstFirst(entry); //or the other }}

Testing the Factory

public class testFactory { public static void main(String args[]) NameFactory nfactory = new NameFactory(); String name=“Ali khan”; Namer namer = nfactory.getNamer(name); - Delegation //compute the first and last names //using the returned class System.out.println(namer.getFirst()); System.out.println(namer.getLast()); }}