Unit II-Chapter No. : 5- design Patterns

Slides:



Advertisements
Similar presentations
COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.
Advertisements

Chapter 6: Using Design Patterns
Design Patterns. What are design patterns? A general reusable solution to a commonly occurring problem. A description or template for how to solve a problem.
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
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.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns 1.
Advanced Programming Rabie A. Ramadan 7.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
ECE450S – Software Engineering II
Design Patterns -- Omkar. Introduction  When do we use design patterns  Uses of design patterns  Classification of design patterns  Creational design.
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.
CS 160: Software Engineering October 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
DESIGN PATTERNS -CREATIONAL PATTERNS WATTANAPON G SUTTAPAK Software Engineering, School of Information Communication Technology, University of PHAYAO 1.
Design Patterns Introduction
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Creational Patterns C h a p t e r 3 – P a g e 14 Creational Patterns Design patterns that deal with object creation mechanisms and class instantiation,
TEMPLATE METHOD DESIGN PATTERN -SWAPNIL SHAH. WHAT IS A DESIGN PATTERN… A design pattern is a general reusable solution to a commonly occurring problem.
Factory Method. Intent/Purpose Factory Method is used to deal with a problem of creating objects without specifying the EXACT class of object that we.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Patterns in programming
Modern Programming Tools And Techniques-I
Design Patterns: MORE Examples
Web Design & Development Lecture 9
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
Software Design Patterns
MPCS – Advanced java Programming
Design Patterns C++ Java C#.
Week 2, Day 1: The Factory Method Pattern
Factory Patterns 1.
Objects as a programming concept
Design Patterns Introduction
Design Patterns C++ Java C#.
Design Patterns Damian Gordon.
Design Patterns Based on slides provided by Abbie Jarrett
Software Design and Architecture
Polymorphism.
object oriented Principles of software design
The Object-Oriented Thought Process Chapter 08
Chapter 6: Using Design Patterns
ABSTRACT FACTORY.
Object Oriented Programming
Advanced Programming Behnam Hatami Fall 2017.
Software Engineering Lecture 7 - Design Patterns
Week 6 Object-Oriented Programming (2): Polymorphism
Object Oriented Design Patterns - Creational Patterns
Factory Pattern.
Polymorphism Pattern.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
DESIGN PATTERNS : Introduction
CS 350 – Software Design Singleton – Chapter 21
Strategy Design Pattern
Lesson 5: More on Creational Patterns
Inheritance and Polymorphism
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Introduction To Design Patterns
Chapter 8, Design Patterns Introduction
Final and Abstract Classes
Decorator Pattern.
CS 151: Object-Oriented Design October 8 Class Meeting
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Unit II-Chapter No. : 5- design Patterns

Topic Learning Outcomes Explain java design patterns Apply design pattern to solve a given problem Apply suitable design pattern solution for a given problem specification School of Computer Science & Engineering

School of Computer Science & Engineering Content Introduction to design patterns Creational Patterns: Singleton pattern, Factory method pattern Behaviour pattern: Strategy pattern, Template method pattern Structural pattern: Adapter pattern, Composite pattern. School of Computer Science & Engineering

School of Computer Science & Engineering Introduction The design pattern is a general reusable solution to a commonly occurring problem in software design. OR , The design pattern are just a formal language to define common ways to solve software engineering problems. We (most people)designed solutions (class diagrams) using design patterns, without knowing that they are. People will naturally come up with solutions that will fit into a design pattern on their own, but the design patterns help to define terminology and standard ideas that can be helpful. The main benefit of design patterns is that, they are ideas that can be defined in ways that are repeatedly useful (reusable). School of Computer Science & Engineering

Design Patterns: Limitations One of the limitation (due to reusability) Is that, the design patterns stop people from coming up with proper solution to a specific problem. Instead of thinking about how to solve that particular problem, people try applying one design pattern after the other. However, if we know language of design patterns (UML) and standard design patterns, we can take advantage of reusability and we can design our own solutions. School of Computer Science & Engineering

Creational Design Pattern: In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.  Singleton pattern, Factory method pattern School of Computer Science & Engineering

School of Computer Science & Engineering Singleton pattern Singleton pattern is one of the simplest design patterns in Java, as this pattern provides one of the best ways to create an object. This pattern involves a single class which is responsible to create an object while making sure that only single object gets created. This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class. School of Computer Science & Engineering

Singleton pattern: implementation create a SingleObject class, it has its constructor as private and have a static instance of itself. SingleObject class provides a static method to get its static instance to outside world. SingletonPatternDemo class will use SingleObject class to get a SingleObject object. School of Computer Science & Engineering

Singleton pattern: class diagram School of Computer Science & Engineering

Singleton pattern: code Step 1 Create a Singleton Class. SingleObject.java School of Computer Science & Engineering

School of Computer Science & Engineering Step 2 Get the only object from the singleton class. SingletonPatternDemo.java School of Computer Science & Engineering

School of Computer Science & Engineering Step 3 Verify the output. Hello World! School of Computer Science & Engineering

Design Pattern - Factory Pattern Factory pattern is one of most used design pattern in Java. In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface. School of Computer Science & Engineering

Factory Pattern: class diagram School of Computer Science & Engineering

Factory Pattern: Implementation create a Shape interface and concrete classes implementing the Shape interface. A factory class ShapeFactory is defined as a next step. FactoryPatternDemo, our demo class will use ShapeFactory to get a Shapeobject. It will pass information (CIRCLE / RECTANGLE / SQUARE) toShapeFactory to get the type of object it needs. School of Computer Science & Engineering

School of Computer Science & Engineering Step 1 Create an interface. Shape.java public interface Shape { void draw();} Step 2 Create concrete classes implementing the same interface. Rectangle.java public class Rectangle implements Shape {  public void draw() { System.out.println("Inside Rectangle::draw() method."); } } School of Computer Science & Engineering

Factory Pattern: implementation Square.java public class Square implements Shape { public void draw() { System.out.println("Inside Square::draw() method."); }} Circle.java public class Circle implements Shape { public void draw() { System.out.println("Inside Circle::draw() method."); }} School of Computer Science & Engineering

School of Computer Science & Engineering ShapeFactory.java Step 3: Create a Factory to generate object of concrete class based on given information. School of Computer Science & Engineering

School of Computer Science & Engineering Step 4 Use the Factory to get object of concrete class by passing an information such as type. FactoryPatternDemo.java School of Computer Science & Engineering

Factory Pattern: output Step 5 Verify the output. Inside Circle::draw() method. Inside Rectangle::draw() method. Inside Square::draw() method. School of Computer Science & Engineering

Behaviour Pattern: strategy pattern In Strategy pattern, a class behaviour or its algorithm can be changed at run time. Example Base class reference variable/interface reference variable can be assigned sub class object The type of the object assigned decides which version (object) method has to be called. In Strategy pattern, we create objects which represent various strategies and a context object whose behavior varies as per its strategy object. The strategy object changes the executing algorithm of the context object. School of Computer Science & Engineering

Strategy pattern: class diagram School of Computer Science & Engineering

Strategy pattern: implementation Steps create a Strategy interface defining an action Create concrete strategy classes implementing the Strategy interface Context is a class which uses a Strategy. Create StrategyPatternDemo, a demo class, that will use Context and strategy objects to demonstrate change in Context behaviour based on strategy it deploys or uses. School of Computer Science & Engineering

Strategy pattern: code School of Computer Science & Engineering

Strategy pattern: code School of Computer Science & Engineering

Strategy pattern: code School of Computer Science & Engineering

Behaviour pattern: Template pattern In Template pattern, an abstract class exposes defined way(s)/template(s) to execute its methods Its subclasses can override the method implementation as per need but the invocation is to be in the same way as defined by an abstract class School of Computer Science & Engineering

Template pattern: class diagram School of Computer Science & Engineering

Template pattern: implementation School of Computer Science & Engineering

School of Computer Science & Engineering Template Pattern School of Computer Science & Engineering

School of Computer Science & Engineering Template Pattern School of Computer Science & Engineering