composite: shapes organisation state observer design patterns

23
Composite: Shapes Organisation State Observer Design Patterns

Upload: devin-rivera

Post on 01-Apr-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Composite: Shapes Organisation State Observer Design Patterns

Composite:Shapes

OrganisationStateObserver

Design Patterns

Page 2: Composite: Shapes Organisation State Observer Design Patterns

Designeksempel: Composite-pattern

1

Circle Rectangle Picture

Shape

0..*0..*

Position

1

Composite: Grafisk editor, Tekstbehandling, Køkkenlager mmm.

Hvordan ser en Show-metode ud på Shape, Circle og Picture

Har I en løsning?

Page 3: Composite: Shapes Organisation State Observer Design Patterns

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

Page 4: Composite: Shapes Organisation State Observer Design Patterns

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;

Page 5: Composite: Shapes Organisation State Observer Design Patterns

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

Page 6: Composite: Shapes Organisation State Observer Design Patterns

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

Page 7: Composite: Shapes Organisation State Observer Design Patterns

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

Page 8: Composite: Shapes Organisation State Observer Design Patterns

The benefits of patterns

A pattern captures a proven good design:A pattern is based on experienceA 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 levelto document and communicate design

One doesn’t have to reinvent solutions over and over againPatterns facilitate reuse not only of code fragments, but of ideas.

Page 9: Composite: Shapes Organisation State Observer Design Patterns

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 schoolPatterns 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

Page 10: Composite: Shapes Organisation State Observer Design Patterns

Eksempel: Organisation

demos\CompositeOrgStruct

Page 11: Composite: Shapes Organisation State Observer Design Patterns
Page 12: Composite: Shapes Organisation State Observer Design Patterns

The Object-Oriented Implementation: Composite Pattern

AbstractTree

Leaf Tree

0..*

demos\RecursiveDirectoryTraverse

Page 13: Composite: Shapes Organisation State Observer Design Patterns

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 programXMLParsing protocolsParser/scanner in a compiler or a browser or…Event handling in a windows system…..

Page 14: Composite: Shapes Organisation State Observer Design Patterns

State Pattern

Implements the loop that gets next state and calls

any operations connected to current state

Page 15: Composite: Shapes Organisation State Observer Design Patterns

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.

Page 16: Composite: Shapes Organisation State Observer Design Patterns

DFA defining Integers

Page 17: Composite: Shapes Organisation State Observer Design Patterns

OO Implementation

State is an objectState 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!

Page 18: Composite: Shapes Organisation State Observer Design Patterns

Integer Scanner

Page 19: Composite: Shapes Organisation State Observer Design Patterns

OO Parser Looppublic 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 tryinput = +123

Page 20: Composite: Shapes Organisation State Observer Design Patterns

Observer Pattern

BottleState

ChangeAmount()

IObserver

NotifyMe()

IObservable

NotifyAll()Add()Remove()

0..*0..1 0..*0..1

Optimist Pessimist

Page 21: Composite: Shapes Organisation State Observer Design Patterns

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.

BottleState

ChangeAmount()

IObserver

NotifyMe()

IObservable

NotifyAll()Add()Remove()

0..*0..1 0..*0..1

Optimist Pessimist

Page 22: Composite: Shapes Organisation State Observer Design Patterns

Observer PatternExample: 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

BottleState

ChangeAmount()

IObserver

NotifyMe()

IObservable

NotifyAll()Add()Remove()

0..*0..1 0..*0..1

Optimist Pessimist

View source

Page 23: Composite: Shapes Organisation State Observer Design Patterns

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.