Design Patterns Examples of smart use of inheritance and polymorphism: Composite Pattern State Pattern FEN 2014UCN Teknologi/act2learn1.

Slides:



Advertisements
Similar presentations
Object-Oriented Application Frameworks Much of the cost and effort stems from the continuous re- discovery and re-invention of core concepts and components.
Advertisements

Composite: Shapes Organisation State Observer Design Patterns.
©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 12Slide 1 Software Design l Objectives To explain how a software design may be represented.
Design Patterns CMPS Design Patterns Consider previous solutions to problems similar to any new problem ▫ must have some characteristics in common.
Object-Oriented Design Patterns Composite Singleton State Observer … Autumn 2012UCN Technology: IT/Computer Science1.
Plab – Tirgul 12 Design Patterns
7M701 1 Software Engineering Object-oriented Design Sommerville, Ian (2001) Software Engineering, 6 th edition: Chapter 12 )
Software Reuse Building software from reusable components Objectives
Design Patterns Daniel McClain. Background What are they?  Way of recording solutions to recurring design problems History  “A Pattern Language: Towns,
Ralph Johnson - University of Illinois1 Patterns: What They Are, and How to Write Them Ralph Johnson University of Illinois at Urbana-Champaign
Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.
Adapters Presented By Zachary Dea. Definition A pattern found in class diagrams in which you are able to reuse an ‘adaptee’ class by providing a class,
Chapter 10 Classes Continued
Object Oriented Concepts. Movement toward Objects Instead of data-oriented or process-oriented Analysis, many firms are now moving to object-oriented.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Introduction to software design patterns For CSE 3902 By: Matt Boggus.
Introduction To System Analysis and design
Design Patterns.
Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN AK IT: Softwarekonstruktion.
CS212: Object Oriented Analysis and Design Lecture 1: Introduction.
Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science and Electrical Engineering, WVU.
1 Copyright © 2014 Atego. Patterns INCOSE MBSE WG – Simon A. Perry - Atego.
Chapter Five An Introduction to Design Patterns Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information.
Unified Modeling Language, Version 2.0
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
Design Patterns. Patterns “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution.
Autumn 2012UCN T&B - IT/Computer Science1 State Pattern Implementation of State Machines State Pattern.
Object-Oriented Design Principles and Patterns. © 2005, James R. Vallino2 How Do You Design? What principles guide you when you create a design? What.
Fall 2010 CS4310 Requirements Engineering A Brief Review of UML & OO Dr. Guoqiang Hu Department of Computer Science UTEP 1.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism © 2011 | PN AZRINA.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
CS 160: Software Engineering October 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Object-Oriented Modeling: Static Models. Object-Oriented Modeling Model the system as interacting objects Model the system as interacting objects Match.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Object-Oriented Programming. Objectives Distinguish between object-oriented and procedure-oriented design. Define the terms class and object. Distinguish.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Session 07: C# Design Patterns
Design Patterns: Elements of Reusable Object- Orientated Software Gamma, Helm, Johnson, Vlissides Presented By: David Williams.
Patterns Composite Pattern. Patterns All designers use patterns. Patterns in solutions come from patterns in problems. "A pattern is a solution to a problem.
S.Ducasse Stéphane Ducasse 1 Essential OO Concepts Stéphane Ducasse.
FEN 2014UCN Teknologi/act2learn1 Higher order functions Observer Pattern Delegates Events Visitor Pattern Lambdas and closures Lambdas in libraries.
1 Unified Modeling Language, Version 2.0 Chapter 2.
FEN NOEA/IT: Advanced Computer Studies 1 Patterns The concept of patterns originates from architecture (Christopher Alexander, 1977): “Each pattern.
OOP Review CS 124.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
1 Good Object-Oriented Design Dr. Radu Marinescu Lecture 4 Introduction to Design Patterns.
LECTURE 5 Scanning. SYNTAX ANALYSIS We know from our previous lectures that the process of verifying the syntax of the program is performed in two stages:
CSCE 240 – Intro to Software Engineering Lecture 3.
Slide 1 Unified Modeling Language, Version 2.0 Object-Oriented SAD.
GRASP – Designing Objects with Responsibilities
Design Patterns: MORE Examples
COMP2110 Software Design in lecture 14 Patterns(1) /Detailed Design
Design Patterns: Brief Examples
The Object-Oriented Thought Process Chapter 15
Design Patterns Introduction
Advanced Programming Behnam Hatami Fall 2017.
Copyright 2007 Oxford Consulting, Ltd
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
UML  UML stands for Unified Modeling Language. It is a standard which is mainly used for creating object- oriented, meaningful documentation models for.
Presentation transcript:

Design Patterns Examples of smart use of inheritance and polymorphism: Composite Pattern State Pattern FEN 2014UCN Teknologi/act2learn1

2 (OO) Design Patterns The concept of patterns originates from architecture (Christopher Alexander, 1977): “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 e. a.: “A Pattern Language”. Oxford University Press, New York, 1977.) FEN 2014

UCN Teknologi/act2learn3 (OO) Design Patterns A well known and widely accepted concept in software engineering Developed in the early 1990s and published by Gamma e.a. (“Gang of Four”, GoF) in 1995: “(…) design patterns (…) are descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context.” (Erich Gamma e.a.:”Design Patterns. Elements of Reusable Object-Oriented Software”. Addison-Wesley ) FEN 2014

UCN Teknologi/act2learn4 The benefits of patterns A pattern captures a proven good design: –A pattern is based on experience –A pattern is discovered – not invented It introduces a new (and higher) level of abstraction, which makes it easier: –to talk and reason about design on a higher level –to document and communicate design One doesn’t have to reinvent solutions over and over again Patterns facilitate reuse not only of code fragments, but of ideas. FEN 2014

UCN Teknologi/act2learn5 Patterns as a learning tool It is often said that good skills in software construction require experience and talent …and neither can be taught or learned at school Patterns capture experience (and talent) in a way that is communicable and comprehensible …and hence experience can be taught (and learned) So one should put a lot of effort in studying patterns FEN 2014

UCN Teknologi/act2learn6 Example: Composite-pattern Composite: Graphical editor, Word processor, Inventories etc.. (What will a Show()-method look like on Shape, Circle, Picture etc.) FEN 2014

UCN Teknologi/act2learn7 public abstract class Shape { private int id; private String color; private int xpos; private int ypos; public void MoveTo(Position newPos){ // PRE none // POST pos'=newPos } public abstract void Show(); // PRE none // POST the shape is drawn public abstract void Hide(); // PRE none // POST the shape is hidden public abstract float Area(); // PRE none // POST Computes the area with 4 digits. }//end Shape FEN 2014

UCN Teknologi/act2learn8 public class Circle: Shape{ private int r; //radius public void Show(){ //PRE none //POST the circle is drawn //Must be implemented using appropriate //graphical routines } public void Hide(){ //PRE none //POST the circle is hidden //Must be implemented using appropriate //graphical routines } public override float Area() { return (float)(r*r); } }//end Circle FEN 2014

UCN Teknologi/act2learn9 public class Picture : Shape{ List pictList; // operationer til at tilføje og slette figurer mv. public void Show(){ //PRE none //POST The composite shape is drawn foreach(Shape s in pictList) s.show(); } public override float Area() { float result=0; for(int i=0;i<pictList.Count;i++) { result= result+pictList[i].Area(); } return result; }//end Picture Dynamic type definesshow() Static type FEN 2014

Composite Pattern FEN UCN Teknologi/act2learn Can be used to model general tree structures

General Tree FEN UCN Teknologi/act2learn AbstractTreeElement Leaf Tree 0..*

More Examples/Exercises Organisational Structures (example)Organisational Structures Project Management (exercise)Project Management Composite Pattern can be found in the.NET libraries: demos\RecursiveDirectoryTraverse demos\RecursiveDirectoryTraverse UCN Teknologi/act2learn12FEN 2014

State Pattern Models state machines (DFAs: Deterministic Finite Automata). FEN 2014UCN Teknologi/act2learn13

Example: Controlling a printer (from: FEN 2014UCN Teknologi/act2learn14 To implement this, we need a set of events E, and a set of states S, and a transition function t: t(E, S) -> S that gives the next state.

Implementations of DFAs Choose an appropriate data structure to represent states: –A List or –A Dictionary –A …? FEN 2014UCN Teknologi/act2learn15 event state 2 states n-1 i 0 s1 Find a way to implement the transition function: –A matrix perhaps? Difficult to maintain.

OO Implementation State is an object State Pattern can be applied: –abstract class State specifies one or more abstract methods: transition(-) – returns state corresponding to next event action(-) – if any processing is to be done when a transition occurs (code generation, event handling etc.) each concrete state inherits from State and implements the abstract methods The controller loop uses references having the abstract class State as static type. Polymorphism and dynamic binding handles the rest! FEN 2014UCN Teknologi/act2learn16

Class Diagram FEN 2014UCN Teknologi/act2learn17

Note how the design reflects the structure of the state machine FEN 2014UCN Teknologi/act2learn18 View source Exercise: Add a new event: “Paper Jam”, which can occur in the state “Printing” and brings the printer in an error state where it remains stopped.

State Pattern FEN 2014UCN Teknologi/act2learn19

The Classes of the Pattern Context : Defines the objects that we want maintain state information about (for instance DialogBox). This class has a reference (static type: ContextState – the abstract super class) to some concrete state (that is an object of one of the sub classes – dynamic type). ContextState: The abstract super class defining a common interface to all the concrete states. ConcreteState1,...: The sub classes to ContextState. One sub class to each state in the DFA. The key to the design is in the processEvent-method, which takes an event as input and returns the next state. The method is abstract in ContextState and implemented (differently) in the subclasses. FEN 2014UCN Teknologi/act2learn20