JAVA Design Patterns.

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.
Creational Patterns, Abstract Factory, Builder Billy Bennett June 11, 2009.
CSE3308/CSC Software Engineering: Analysis and DesignLecture 5B.1 Software Engineering: Analysis and Design - CSE3308 Patterns CSE3308/CSC3080/DMS/2000/12.
Plab – Tirgul 12 Design Patterns
. Plab – Tirgul 12 Design Patterns. Design Patterns u The De-Facto Book on Design Patterns:
CSE Software Engineering: Analysis and Design, 2002Lecture 7B.1 Software Engineering: Analysis and Design - CSE3308 Patterns CSE3308/DMS/2002/15.
Satzinger, Jackson, and Burd Object-Orieneted Analysis & Design
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Overview of Design Patterns & The MVC Design Pattern Sapana Mehta.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Abstract Factory Pattern.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Design Patterns.
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 Components Creational Patterns.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
CSC 480 Software Engineering Design With Patterns.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.
Design Patterns Introduction
Advanced Object-oriented Design Patterns Creational Design Patterns.
Chapter 8 Object Design Reuse and Patterns. More Patterns Abstract Factory: Provide manufacturer independence Builder: Hide a complex creation process.
Reference – Object Oriented Software Development Using Java - Jia COP 3331 Object Oriented Analysis and Design Chapter 10 – Patterns Jean Muhammad.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
The Abstract Factory Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Abstract Factory pattern Intent Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
J2EE Platform Overview (Application Architecture)
Design Patterns: MORE Examples
Abstract Factory Pattern
Design Patterns: Brief Examples
Design Patterns A brief introduction to what they are, why they are useful, and some examples of those that are commonly used.
Chapter 5:Design Patterns
University of Central Florida COP 3330 Object Oriented Programming
Factory Patterns 1.
Introduction to Design Patterns
Chapter 11 Object-Oriented Design
Software Design and Architecture
Design Patterns with C# (and Food!)
Abstract Factory Pattern
What’s a Design Pattern?
Intent (Thanks to Jim Fawcett for the slides)
Design Patterns - A few examples
Software Engineering Lecture 7 - Design Patterns
Model-View-Controller Patterns and Frameworks
CSC 480 Software Engineering
Design Patterns – Part 3 Sources:
Review: Design Pattern Structure
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Lesson 5: More on Creational Patterns
Fundaments of Game Design
Design pattern Lecture 6.
Creational Patterns.
Informatics 122 Software Design II
CSC 480 Software Engineering
Presentation transcript:

JAVA Design Patterns

Interfaces Interfaces define a contract Contain methods and their signatures Can also include static final constants (and comments) But no implementation Very similar to abstract classes One interface can extend another, but An interface cannot extend a class A class cannot extend an interface Classes implement an interface

Defining Interfaces Use the interface keyword public interface Vehicle { public void turnLeft(); public void turnRight(); } Like abstract methods, the signature is terminated with a semi-colon

Implementing Interfaces Use the implements keyword public class MotorBike implements Vehicle { //include methods from Vehicle interface } Class must implement all methods of the interface OR declare itself to be abstract Classes can implement any number of interfaces public class MotorBike implements Vehicle, Motorised Possible to combine extends and implements public class MotorBike extends WheeledVehicle implements Vehicle, Motorised

Sapana Mehta (CS-6V81) Design Patterns A pattern is a proven solution to a problem in a context. Christopher Alexander says each pattern is a three-part rule which expresses a relation between a certain context, a problem, and a solution. Design patterns represent a solutions to problems that arise when developing software within a particular context.

Categorizing Pattern Design Architectural Analysis Creational Patterns, then, represent expert solutions to recurring problems in a context and thus have been captured at many levels of abstraction and in numerous domains. Numerous categories are: Design Architectural Analysis Creational Structural Behavioral

Sun’s J2EE Framework Components Containers and Connectors: Hiding Complexity, Enhancing Portability Components are the key focus of application developers Containers intercede between clients and components, providing services transparently to both, including transaction support and resource pooling. Connectors sit beneath the J2EE platform, defining a portable service API to plug into existing enterprise vendor offerings.

J2EE and Design Patterns J2EE: AN OPERATING SYSTEM FOR THE WEB Enterprise web applications, which live on networks and are accessible through browsers, are redefining Enterprise Web Software. This is the next wave of computing. The J2EE architecture is built to enable component developers Design Pattern.

Java Pet Store- MVC Design Pattern The Java Pet Store is a reference application that demonstrates J2EE technologies. The Pet Store application implements MVC (Model-View-Controller) design, and demonstrates one way to design an application that should scale well.

ShoppingCart.jsp Java Server Pages (JSP)

Elements of Design Patterns Design patterns have 4 essential elements: Pattern name: increases vocabulary of designers Problem: intent, context, when to apply Solution: UML-like structure, abstract code Consequences: results and tradeoffs

Abstract Factory

Abstract Factory Provide an interface for creating families of related or dependent objects without specifying their concrete classes. The real-world example demonstrates the creation of different animal worlds for a computer game using different factories. Although the animals created by the Continent factories are different, the interactions among the animals remain the same.

Abstract Factory

Abstract Factory: Participants AbstractFactory Declares an interface for operations that create abstract products ConcreteFactory Implements the operations to create concrete product objects: usually instantiated as a Singleton AbstractProduct Declares an interface for a type of product object; Concrete Factories produce the concrete products ConcreteProduct Defines a product object to be created by the corresponding concrete factory

abstract class AbstractProductA{ public abstract void operationA1(); public abstract void operationA2(); } class ProductA1 extends AbstractProductA{ ProductA1(String arg){ System.out.println("Hello "+arg); } // Implement the code here public void operationA1() { }; public void operationA2() { };

class ProductA2 extends AbstractProductA{ ProductA2(String arg){ System.out.println("Hello "+arg); } // Implement the code here public void operationA1() { }; public void operationA2() { }; }

abstract class AbstractProductB{ //public abstract void operationB1(); //public abstract void operationB2(); } class ProductB1 extends AbstractProductB{ ProductB1(String arg){ System.out.println("Hello "+arg); } // Implement the code here

class ProductB2 extends AbstractProductB{ ProductB2(String arg){ System.out.println("Hello "+arg); } // Implement the code here }

abstract class AbstractFactory{ abstract AbstractProductA createProductA(); abstract AbstractProductB createProductB(); } class ConcreteFactory1 extends AbstractFactory{ AbstractProductA createProductA(){ return new ProductA1("ProductA1"); AbstractProductB createProductB(){ return new ProductB1("ProductB1");

class ConcreteFactory2 extends AbstractFactory{ AbstractProductA createProductA(){ return new ProductA2("ProductA2"); } AbstractProductB createProductB(){ return new ProductB2("ProductB2");

//Factory creator - an indirect way of instantiating the factories class FactoryMaker{ private static AbstractFactory pf=null; static AbstractFactory getFactory(String choice){ if(choice.equals("a")){ pf=new ConcreteFactory1(); }else if(choice.equals("b")){ pf=new ConcreteFactory2(); } return pf; }

// Client public class Client{ public static void main(String args[]){ AbstractFactory pf=FactoryMaker.getFactory("a"); AbstractProductA product=pf.createProductA(); //more function calls on product }

Consequences 1. Concrete class isolation (Good) Client does not interact with the implementation classes Client only manipulates instances through the abstract interfaces

Consequences 2. Product families easily exchanged (Good) Only have to change the concrete factory Can be done at run time

Consequences 3. Products are more consistent (Good) Helps the products in each product family consistently be applied together (assuming they work well together) Only one family at a time

Consequences 4. Difficult to support new kinds of products (Bad) Extending existing abstract factories to make new products is difficult and time consuming The family of products available is fixed by Abstract Factory interface

Singleton

Singleton Do we need to limit the no of objects of a class

Singleton Intent: Ensure that a class only has one instance, and provide a global point of access to it Applicability: There must be exactly one instance of a class, and it must be accessible to clients from a well-known access point When the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code

Singleton

Singleton Pattern Problem Context Solution core Want to ensure a single instance of a class, that’s shared by all uses throughout a program (e.g., the Portfolio) Context Need to address initialization versus usage ordering Solution core Provide a global access method (static member function) First use of the access method instantiates the class

Singleton Pattern Solution core Consequences Constructors for instance can be hidden (made private) Can hide destructor too if a “fini” method is also provided Consequences Object is never created if it’s never used Object is shared efficiently among all uses

Observer pattern Intent: Used in Model-View-Controller framework Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically Used in Model-View-Controller framework Model is problem domain View is windowing system Controller is mouse/keyboard control How can Observer pattern be used in other applications? JDK’s Abstract Window Toolkit (listeners) Java’s Thread monitors, notify(), etc.

Structure of Observer Pattern

Creational Patterns Abstract Factory: Builder: Factory Method: Factory for building related objects Builder: Factory for building complex objects incrementally Factory Method: Method in a derived class creates associates Prototype: Factory for cloning new instances from a prototype Singleton: Factory for a singular (sole) instance

Adapter Pattern Problem Have an object with an interface that’s close to (but is not exactly) what we need Context Want to re-use an existing class Can’t change its interface It’s impractical to extend class hierarchy more generally Solution Core Wrap a particular class or object with the interface needed (2 forms: class form and object forms)

Adapter «Adaptee» «Superclass» «Adapter» adaptedMethod polymorphicMethod «Adapter» polymorphicMethod() return adaptee.adaptedMethod(); { }

Adapter Structure (Class Form) Interface Impl method () = 0; impl_method (); public private Adapter method () { impl_method (); }; Interface abstract base class provides desired interface Impl concrete class provides the implementation Adapter glues them together via inheritance

Adapter Structure (Object Form) Interface method () = 0; impl_ Adapter Impl method () { impl_->impl_method(); }; impl_method (); Interface abstract base class provides desired interface Impl concrete class provides the implementation Adapter glues them together via delegation

Adapter Example TimsTorus ThreeDShape Sphere Torus volume() { return calcVolume ThreeDShape volume Sphere Torus volume() return adaptee.calcVolume(); { }

objects whose state can be watched Pattern: Observer objects whose state can be watched

Observer Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. The object that changes state is called the subject and the other objects are the observers.

Observer design pattern In many programs, when object changes state, other objects may have to be notified Example: when a car in a game is moved The graphics engine needs to know to re-render item The traffic computation routines need to re-compute the traffic pattern Another example: data in a spreadsheet The display must be updated Possibly multiple graphs that use that data need to re-draw themselves This pattern answers the question: How best to notify those objects when the subject changes?

Motivation Separate presentation aspects of the UI from the underlying application data. e.g., spreadsheet view and bar chart view don't know about each other they act as if they do: changing one changes the other.

Observer: Structure

Observer pattern The key participants in this pattern are: The Subject, which provides an (virtual) interface for attaching and detaching observers The Observer, which defines the (virtual) updating interface The ConcreteSubject, which is the class that inherits/extends/implements the Subject The ConcreteObserver, which is the class that inherits/extends/implements the Observer