Composite: Shapes Organisation State Observer Design Patterns.

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

Object Oriented Game Framework Design Riz Verghese Joj.
Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
Spring 2007NOEA - Computer Science1 Design Patterns Regular Expressions (Regular Languages) State Machines Implementation of State Machine State Pattern.
And so to Code. Forward, Reverse, and Round-Trip Engineering Forward Engineering Reverse Engineering Round-Trip Engineering.
©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.
Design Patterns Examples of smart use of inheritance and polymorphism: Composite Pattern State Pattern FEN 2014UCN Teknologi/act2learn1.
Object-Oriented Design Patterns Composite Singleton State Observer … Autumn 2012UCN Technology: IT/Computer Science1.
Software Reuse Building software from reusable components Objectives
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Ralph Johnson - University of Illinois1 Patterns: What They Are, and How to Write Them Ralph Johnson University of Illinois at Urbana-Champaign
Satzinger, Jackson, and Burd Object-Orieneted Analysis & Design
Chapter 10 Classes Continued
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.
Like our natural language. Designed to facilitate the expression and communication ideas between people and computer Like our natural language. Designed.
Introduction to software design patterns For CSE 3902 By: Matt Boggus.
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 18 Slide 1 Software Reuse.
Design Patterns.
Session 05: C# Patterns Algorithm Patterns: Sweep Search FEN AK IT: Softwarekonstruktion.
CSCI-383 Object-Oriented Programming & Design Lecture 4.
1 Copyright © 2014 Atego. Patterns INCOSE MBSE WG – Simon A. Perry - Atego.
An Introduction to Software Architecture
Comp2110 Software Design lecture 13Detailed Design (1)  detailed design contrasted with high level design  introducing the Observer Design Pattern 
Chapter Five An Introduction to Design Patterns Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information.
Unified Modeling Language, Version 2.0
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.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
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.
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.
Design Patterns Introduction General and reusable solutions to common problems in software design SoftUni Team Software University
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
CSC 480 Software Engineering Design With Patterns.
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.
Session 07: C# Design Patterns
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.
1 Design Patterns Delegates Visitor Pattern. 2 Observer Pattern Observer Pattern is an object-oriented design that simulates void-pointers in for instance.
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.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
Unified Modeling Language (UML)
1 Good Object-Oriented Design Dr. Radu Marinescu Lecture 4 Introduction to Design Patterns.
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
Software Design Patterns
MPCS – Advanced java Programming
Design Patterns Introduction
Polymorphism.
Object Oriented Programming in Java
Object-Orientated Programming
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Object Oriented Analysis and Design
Advanced Programming Behnam Hatami Fall 2017.
Introduction to Design Patterns
Presentation transcript:

Composite: Shapes Organisation State Observer Design Patterns

Designeksempel: Composite-pattern Composite: Grafisk editor, Tekstbehandling, Køkkenlager mmm. Hvordan ser en Show-metode ud på Shape, Circle og Picture Har I en løsning?

abstract public class Shape{ protected Position pos; //figurens position protected Color color; //figurens farve //øvrige attributter public virtual void MoveTo(Position newPos){ // PRE none // POST pos'=newPos } public abstract void Show(); // Abstrakt operation // - kan ikke implementeres for en vilkårlig figur. // PRE none // POST figuren er tegnet public abstract void Hide(); // Abstrakt operation // - kan ikke implementeres for en vilkårlig figur. // PRE none // POST figuren er skjult // øvrige operationer }//end Shape

public class Circle: Shape{ private int r; //radius //øvrige attributter - pos og color arves public override void Show(){ //PRE none //POST cirklen er tegnet //Denne operation kan nu implementeres for en cirkel //ved hjælp af en passende grafikrutine. } public override void Hide(){ //PRE none //POST cirklen er skjult //Denne operation kan nu implementeres for en cirkel //ved hjælp af en passende grafikrutine. } // øvrige operationer - MoveTo() arves} }//end Circle;

public class Picture: Shape{ private ArrayList shapes; // operationer til at tilføje og slette figurer mv. public void override Show(){ //PRE none //POST den sammensatte figur er tegnet foreach(Shape s in shapes) s.Show(); } public void override Hide(){ //PRE none //POST den sammensatte figur er skjult foreach(Shape s in shapes) s.Hide(); } public void MoveTo(Position newPos){ //PRE none //POST pos'=newPos foreach(Shape s in shapes) s.MoveTo(newPos); } }//end Picture Dynamisk type definerer Show() Statisk type demos\Shapes

(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.)

(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 )

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.

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

Eksempel: Organisation demos\CompositeOrgStruct

The Object-Oriented Implementation: Composite Pattern AbstractTree Leaf Tree 0..* demos\RecursiveDirectoryTraverse

State Pattern Implements state machines (DFA) encapsulating state. Provides addition of new states without changing existing code. Examples: Dialog box for editing parameters to a program XML Parsing protocols Parser/scanner in a compiler or a browser or… Event handling in a windows system …..

State Pattern Implements the loop that gets next state and calls any operations connected to current state

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.

DFA defining Integers

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 input symbol action(-) – if any processing is to be done when a transistion occurs (code generation, event handling etc.) each concrete state inherits from State and implements the abstract methods The parser loop uses references having the abstract class State as static type. Polymorphism and dynamic binding handles the rest!

Integer Scanner

OO Parser Loop public bool Scan(string input) { //input.Length>0 bool ok = false; int i = 0; char nextChar = input[i]; State currState = s1.Transition(nextChar); while (currState != s5 && currState != s4) { i++; if (i == input.Length) nextChar = '\0'; else nextChar = input[i]; currState = currState.Transition(nextChar); } if (currState == s5) ok = true; return ok; } View the C# Code Let’s try input = +123

Observer Pattern

Observer Pattern (Call-back) A number of different objects (observers) wants to be notified when some given object (observable or subject) changes state. Subject must allow observers to enroll dynamically, and subject shall have no knowledge about the observers. This pattern is in sometimes known as call-back.

Observer Pattern Example: a number of objects are interested in being told when some other object is changing state. These objects (the observers) must implement the interface IObserver. This means implementing the method NotifyMe. This method will be called when the observed object (subject) changes state. Subject must maintain a collection of observers and be able to notify all objects in the collection (IObservable). Subject calls NotifyAll, when a change in state happens View source

Exercise Add a Realist to the example. A realist will act as a pessimist, if the bottle amount is less than 50 cl. and like an optimist otherwise.