Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Design Patterns CS is not simply about programming
Recommendation: Play the game and attempt to answer the questions yourself without looking at the answers. You’ll learn much less if you just look at the.
CPSC150 Abstract Classes Chapter 10. CPSC150 Directory Example (note: your assignment does not have all of this) DirectoryEntry name phone public void.
Dept. of Computer Engineering, Amir-Kabir University 1 Design Patterns Dr. Noorhosseini Lecture 2.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Reuse Activities Selecting Design Patterns and Components
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Design Patterns.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
Case Studies on Design Patterns Design Refinements Examples.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Object Orientation Yaodong Bi, Ph.D. Department of Computer Sciences University of Scranton October 18, 2015October 18, 2015October 18, 2015.
Effective C#, Chapter 1: C# Language Elements Last Updated: Fall 2011.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
ECE450S – Software Engineering II
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Design Patterns Yonglei Tao. Design Patterns  A design pattern describes a recurring design problem, a solution, and the context in which that solution.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object Oriented Programming
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Design Patterns Introduction
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Classes, Interfaces and Packages
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Module 9. Dealing with Generalization Course: Refactoring.
PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
Design Patterns: MORE Examples
Chapter 10 Design Patterns.
Interfaces.
Inheritance and Polymorphism
Behavioral Design Patterns
object oriented Principles of software design
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
Java Inheritance.
Object-Oriented Programming
Presentation transcript:

Design Patterns

Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview

Purpose To provide programmers with already documented solutions to common problems. documented solutions to common problems. Gives the programmers a common language. COMPOSITION?

Patterns != Frameworks A framework provides actual code. You use patterns in a framework to create the code. A framework provides actual code. You use patterns in a framework to create the code. If someone gives you a pattern you’ll get a list of diagrams, it’s a concept. A framework consists of actual code. (Remove) If someone gives you a pattern you’ll get a list of diagrams, it’s a concept. A framework consists of actual code. (Remove)

Useful Definitions Object – A package for both data and procedures (methods, functions) that operate on that data Object – A package for both data and procedures (methods, functions) that operate on that data Class – Definition of an object implementation Class – Definition of an object implementation Encapsulation – Abstract away implementation details of a given object Encapsulation – Abstract away implementation details of a given object Interface – All of the method signatures of a given object Interface – All of the method signatures of a given object

Useful Definitions (cont’d) Inheritance – Sub-classing one object to another so it can inherit some properties of its parent while creating more specific details for itself Inheritance – Sub-classing one object to another so it can inherit some properties of its parent while creating more specific details for itself  Good: Subclasses are nice. A simple concept and easy to use.  Bad: Static, tied to it. When you change one thing you might have to change lots of classes. Inheritance is determined at compile time, while aggregation is determined at run time. Dynamic Binding – The run-time association of a request to an object and one of its operations (methods) Dynamic Binding – The run-time association of a request to an object and one of its operations (methods) Polymorphism – The ability to substitute one object for another without having to change any implementation details Polymorphism – The ability to substitute one object for another without having to change any implementation details

Useful Definitions (cont’d) Instantiation – The act of creating an object (a.k.a. an instance of a class) Instantiation – The act of creating an object (a.k.a. an instance of a class) Abstract class – A class whose main purpose is to define a common interface for its subclasses Abstract class – A class whose main purpose is to define a common interface for its subclasses Abstract operation – A declaration of a method with no implementation details Abstract operation – A declaration of a method with no implementation details Concrete classes – A class that contains implementation details. Concrete classes – A class that contains implementation details. Override – Allowing a subclass to handle its method calls on its own by changing the implementation of its parent Override – Allowing a subclass to handle its method calls on its own by changing the implementation of its parent

Useful Definitions (cont’d) Aggregation – One object owns or is responsible for another object. The second object is a part of the first. Both objects have identical lifespans Aggregation – One object owns or is responsible for another object. The second object is a part of the first. Both objects have identical lifespans Aquaintance – One object knows of another, so it can make method calls to it, however, neither object’s lifespan is dependent on the other’s. Aquaintance – One object knows of another, so it can make method calls to it, however, neither object’s lifespan is dependent on the other’s.

Pattern Overview State State Template Template Composite Composite Command Command Strategy Strategy Mediator Mediator

State Pattern Intent: Intent:  Provide the ability for an object to change its behavior in response to internal state changes.

Library State Pattern

Library Asset DVDBookVideo State Pattern A A = abstract * = acquaintance

Library AssetState DVDBookVideoCheckedOutOnShelfOnReserve State Pattern AA A = abstract *1 = acquaintance= aggregation

State Pattern public class Library { private List assets = new List (); …} abstract public class Asset { State state = new onShelf(); protected boolean checkOut() { if(state.checkOut() = = true) { changeState(new CheckedOut()); changeState(new CheckedOut()); return true; return true; }else return false; return false;} protected boolean putOnShelf() { … } protected boolean putOnReserve() { … } protected void changeState(State newState) { state = newState; }}

State Pattern abstract public class State { protected boolean checkOut() { return false; } protected boolean putOnShelf() { return false; } protected boolean putOnReserve() { return false; } } public class CheckedOut extends State{ private boolean putOnShelf() { return true; } } public class OnShelf extends State { private boolean checkOut() { return true; } private boolean putOnReserve() { return true; } } public class onReserve extends State { private boolean checkOut() { return true; } }

Template Method Intent: Intent:  Create a skeleton for an algorithm, while allowing subclasses to redefine certain steps.

Template Method TreeBaseClass A setName() getName() addChildren() A outputeHTML() A

TreeLeafClass addChildren() Template Method TreeBaseClass A setName() getName() addChildren() A outputeHTML() A

TreeLeafClass addChildren() Template Method TreeBaseClass A setName() getName() addChildren() A outputeHTML() A AuthorClass outputHTML() DateClass outputHTML() HoursClass outputHTML() CodeClass outputHTML() CodeClass outputHTML() CodeClass outputHTML()

Composite Pattern Compose an object into a tree structure. Let clients treat individual objects and compositions of objects as the same thing. Compose an object into a tree structure. Let clients treat individual objects and compositions of objects as the same thing.

Composite Pattern Component A setName(); getName(); getAllFiles(List theList); getContents(List theList); A = aggregation A = abstract

Composite Pattern * Component A setName(); getName(); getAllFiles(List theList); getContents(List theList); A Directory setName(); getName(); getAllFiles(List theList); getContents(List theList); = aggregation A = abstract

Composite Pattern TextFileImageFileVideoFileDirectory * Component A setName(); getName(); getAllFiles(List theList); getContents(List theList); A setName(); getName(); getAllFiles(List theList); getContents(List theList); setName(); getName(); getAllFiles(List theList); getContents(List theList); setName(); getName(); getAllFiles(List theList); getContents(List theList); setName(); getName(); getAllFiles(List theList); getContents(List theList); This object is also known as a Composite These objects are also known as leaves = aggregation A = abstract

Composite Pattern The component pattern will result in a tree structure. The component pattern will result in a tree structure. Component Leaf Component Leaf Component Etc…

Component Pattern abstract public class Component { // The component is the abstract String myName; // class that all other elements will // extend // extend private void setName(String theName) { myName = theName; myName = theName;} private String getName() { return myName; return myName;} private void getAllFiles(List theList) { // This method will loop through for all children { // all of the component’s children for all children { // all of the component’s children theList.append(child) // and add them to the list of files }} abstract private void getContents(List theList); }

Composite Pattern public class Directory extends Component { // The directory is a component private void getContents(List theList) { // Get the contents of this directory for all children { for all children {child.getContents(theList); }}} public class TextFile extends Component { // This is a leaf element private void getContents(List theList) { theList.append(this); // The leaf node adds itself to the theList.append(this); // The leaf node adds itself to the } // list its parent’s contents list }

Command Pattern Intent: Intent: Encapsulate a request as an object. This allows action to occur without knowing exactly what request is being made.

Command Pattern Document undo(); copy(); print(); Application add(Document) = acquaintance= aggregation *

Command Pattern Menu add(MenuItem) MenuItem clicked(); Application add(Document) = acquaintance= aggregation * ** Document undo(); copy(); print();

Command Pattern Menu add(MenuItem) MenuItem clicked();setTheCommand(String) Application add(Document) Command A Execute(); = acquaintance= aggregation * ** 1 Document undo(); copy(); print(); A = abstract

Command Pattern Command A execute() UndoCommand execute() CopyCommand execute() PrintCommand A = abstract

Command Pattern public class MenuItem { public Command command; public void Clicked() { command.execute();// Simply call execute and command.execute();// Simply call execute and }// the type of the command // determines what exactly occurs public void setTheCommand(String theCommand) { command = theCommand; command = theCommand;}}

Command Pattern abstract public class Command { abstract private void execute(); } public class UndoCommand { document.undo();} public class CopyCommand { document.copy();} public class PrintCommand { document.print();}

Strategy Pattern Intent: Intent: Encapsulate a family of algorithms and make them interchangeable. This allows the algorithm to very independently from the clients that will be using it

Mediator Intent: Intent: Define an object that encapsulates how a set of objects interact.

One Final Example // Template public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { super.execute(mapping, form, request, response); baseForm.setCommand(); // Factory prepareAction(request, baseForm); isFormValid = ValidationValidator.isFormValidCritereon(baseForm, getSearchCriterionValidations(baseForm)); performAction(request, baseForm); return baseForm.getCommand().searchActionForward(mapping, baseForm); } // Command Protected void performAction(HttpServletRequest request, BaseForm baseForm) throws Exception { this.baseForm.getCommand().performAction(this.baseForm, façade, this, request); }//Façade Public void performAction(BaseForm baseForm, WebFacade facade, BaseAction baseAction, HttpSerbletRequest request) throws Exception { this.checkForData(baseForm.getHandlesToAction());facade.holdSettlements(baseForm.getHandlesToAction());}