1 / 23

Design Patterns

Design Patterns. Composite: Shapes Organisation State Observer. 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{

nero
Download Presentation

Design Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design Patterns Composite: Shapes Organisation State Observer

  2. 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?

  3. abstract public class Shape{ protected Position pos; //figurens position protected Colorcolor; //figurensfarve //øvrigeattributter 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

  4. 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;

  5. 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 Statisk type Dynamisk type definerer Show() demos\Shapes

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

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

  8. 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.

  9. 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

  10. Eksempel: Organisation demos\CompositeOrgStruct

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

  12. 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 • …..

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

  14. 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.

  15. DFA defining Integers

  16. 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!

  17. Integer Scanner

  18. 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; } Let’s try input =+123 View the C# Code

  19. Observer Pattern

  20. 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.

  21. 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

  22. 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.

More Related