Lecture 13 UML diagrams state-chart diagrams Design Pattern.

Slides:



Advertisements
Similar presentations
©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 12Slide 1 Software Design l Objectives To explain how a software design may be represented.
Advertisements

UMBC Some Additional Patterns CMSC 432. UMBC More Patterns2 Introduction Over the next few lectures we’ll have an introduction to a number of patterns.
Amirkabir University of Technology, Computer Engineering Faculty, Intelligent Systems Laboratory 1 Mediator Abbas Rasoolzadegan.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Lecture 14 Singleton pattern Fiaspel class diagram, sample code Course analysis.
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Design Pattern: Mediator Mediator Modified from Kyle Kimport’s: Design Patterns: Mediator Design Patterns: Mediator Ref:
Reza Gorgan Mohammadi AmirKabir University of Technology, Department of Computer Engineering & Information Technology Advanced design.
Observer Pattern Fall 2005 OOPD John Anthony. What is a Pattern? “Each pattern describes a problem which occurs over and over again in our environment,
Component-Level Design
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
Design Patterns CS is not simply about programming
Cam Quach Joel Derstine Mediator: Object Behavioral.
Copyright © Active Frameworks Inc. - All Rights Reserved.More On Behavioral Patterns - Page L9-1 PS95&96-MEF-L16-1 Dr. M.E. Fayad Creationa l.
OOMPA Lecture 10 Design Patterns Singleton Factory Method
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
Chapter 22 Object-Oriented Design
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.
BY VEDASHREE GOVINDA GOWDA
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Design Patterns.
A Behavior Object Pattern
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
Case Studies on Design Patterns Design Refinements Examples.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Mediator Pattern and Multiuser Protection Billy Bennett June 8 th, 2009.
CSC 395 – Software Engineering Lecture 13: Object-Oriented Analysis –or– Let the Pain Begin (At Least I’m Honest!)
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
Mediator Kensho Tsuchihashi. Mediator Page 2 Table of Contents 1.What is Mediator? 2.What problem does Mediator solve? 3.Advantage and Disadvantage 4.Additional.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 13 Creational Design Pattern SWE 316: Software Design and Architecture.
Behavioural Design Patterns Quote du jour: ECE450S – Software Engineering II I have not failed. I've just found 10,000 ways that won't work. - Thomas Edison.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Manali Joshi1 The Observer Design Pattern Presented By: Manali Joshi.
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Design Patterns Introduction
The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall Object-Oriented Systems Analysis and Design Using UML Systems Analysis and Design,
Advanced Object-oriented Design Patterns Creational Design Patterns.
Singleton Pattern Presented By:- Navaneet Kumar ise
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Behavioral Pattern: Mediator C h a p t e r 5 – P a g e 169 When a program is made up of several classes, the logic and computation is divided among these.
Kyung Hee University Class Diagramming Notation OOSD 담당조교 석사과정 이정환.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Mediator Design Pattern
Design Patterns: Brief Examples
Observer Design Pattern
More Design Patterns 1.
Multiuser Protection and the Mediator Pattern
Software Engineering Lecture 7 - Design Patterns
Mediator Design Pattern (Behavioral)
Design pattern Lecture 6.
Informatics 122 Software Design II
Presentation transcript:

Lecture 13 UML diagrams state-chart diagrams Design Pattern

Hat Problem Three persons each one wearing a hat that is either red or blue with 50% probability. A person can see the colours of the other two hats but not its own colour. Each player can either make no prediction or give a guess on the colour of its own hat. The team wins if at least one player guessed correctly and no player guessed a wrong answer. There is no form of communication between the players, in particular they do not know which decision any of the other players made. The players can agree beforehand on a common fixed strategy.

Hat Problem Simple strategy: Player A always guesses colour red, the other players make no guess. Probability of winning is 50%. Common strategy: Players agree that when a player sees two hats with identical colours, then he guesses the opposite colour, if he sees two different colours he makes no guess. Chance of winning as a team is 75% 25% RRR, BBB: all three players guess wrong 75% RRB, RBR, RBB, BRB, BBR, BRR : one player guesses correctly, the other two players make no guess

UML Statechart Diagrams Statechart diagrams lie within the behavioral view of a system and render the states and responses of a class. Statechart diagrams describe the behaviour of a class in response to external stimuli. Statechart diagrams describe the life cycle of an object. Statechart diagrams contain States Transitions

Finite State Machines (FSM) States An FSM is exactly in one current state. An FSM has an initial state. An FSM may change state due to a transition. Transitions Transitions connect states with each other. Transitions carry a label or a guard condition that triggers the transition. Transitions can be defined by a transition matrix.

FSM Example States : A,B,C, A is initial state Labels : 0,1 Transitions: (A,0) -> B, (A,1) ->C, (B,0)-> B (B,1) -> C, (C,0) ->C, (C,1)-> B A B C

FSM Example Input sequence: State sequence: A-B-C-B-B-C-C State B: even number of 1’s in sequence State C: odd number of 1’s in sequence A B C

Statechart Diagrams States are classes that define status conditions that an object may satisfy during its existence. States are used to model the situations during the life of an entity in which it satisfies some condition, performs some activity, or waits for some occurence. An entity remains in a state for a finite and non- instantaneous time.

Statechart Diagrams Transitions are associations between states. They are used to model relationships between the different states of an entity. An entity in one state will perform actions and possibly enter another state when an event occurs and a condition is satisfied (if specified), this occurence is known as the firing of a transition.

Statechart Diagrams Transitions are denoted as solid arrows between states and are labeled with a transition symbol. Transitions relate source state and target state vertices. Transitions may have square brackets containing a boolean guard condition that must be satisfied in order to enable the transition to fire. Transitions may have a forward slash followed by one or more actions that result when the transition fires.

Statechart Diagram Source State Target State Event [Guard] / Action

Example Statechart Diagram Not Selected transition creation click event [inside object] / add to selected graphics click event [outside object and not shift key] / delete from selected graphics click event [shift key pressed] initial pseudo state guard action state

Mediator Pattern The Mediator pattern defines an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explictly and it lets you vary their interaction independently.

Mediator Pattern Object-oriented design encourages the distribution of behavior among objects. Such distribution can result in an an object structure with many connections between objects, in the worst case every object ends up knowing about every other. Though partitioning a system into many objects generally enhances reusability, proliferating interconnections tends to reduce it again. Lots of interconnections make it less likely that an object can work without the support of others- the system acts as if it were monolithic.

Example Dialog Window Ek_ Name: Carlson Christensen Eklund Erikson Gunnarson Eklund OK Cancel

Example Dialog Window The dialog box uses a collection of widgets such as, menus, list boxes, buttons, entry fields Often there are dependencies between the widgets in the dialog, for example the marital status buttons get disabled when the entry field is empty, or selecting an entry in the list box changes the content of the entry field, and typing in the entry field changes the highlighted entry in the list box.

Mediator Pattern Different dialog boxes will have different dependencies between widgets, which makes it impossible to simply reuse a standard set of widget classes. Instead widget classes have to be customized to reflect dialog-specific dependencies, which would require a large number of separate subclasses for different types of dialogs.

Coupling between Classes Special Button Special Entry Field Special ListBox list buttonfield button list

Mediator Pattern Encapsulating the collective behavior in a separate Mediator object avoids these problems. A Mediator object is responsible for controlling and coordinating the interactions of a group of objects. The Mediator serves as an intermediary that keeps objects in the group from refering to each other explicitly. The objects only know the Mediator thereby reducing the number of interactions.

Mediator Pattern FormDialog Director Client director Button Entry Field ListBox director buttonfield list

Mediator Pattern The FormDialogDirector is the mediator between the widgets in the dialog box. The FormDialogDirector knows the widgets in a dialog and coordinates their interaction. The FormDialogDirector acts as a hub of communications for widgets.

Mediator Sequence Diagram aListBox Widget Changed() SetText() anEntry Field ShowDialog() aClient aFormDialog Director Get Selection() aButton EnableButton()

Mediator Structure DialogDirector ShowDialog() CreateWidgets() WidgetChanged(w) Widget Changed() director FormDialogDirector CreateWidgets() WidgetChanged(w) ListBox GetSelection() field EntryField SetText() list Director-> WidgetChanged(this)

Mediator Pattern DialogDirector is an abstract class that defines the overall behavior of a dialog. Clients call the ShowDialog operation to display the dialog on the screen. CreateWidgets is an abstract operation for creating the widgets of a dialog. WidgetChanged is another abstract operation, widgets call it to inform their director that they have changed. DialogDirector subclasses override CreateWidgets to create the proper widgets, and they override WidgetChanged to handle the changes.

Mediator Sample Code class DialogDirector { public: virtual void ShowDialog(); virtual void WidgetChanged(Widget *)=0; protected: DialogDirector(); virtual void CreateWidgets() = 0; };

Mediator Sample Code class Widget { public: Widget(DialogDirector*); virtual void Changed(); virtual void HandleMouseEvent(MouseEvent& event); private: DialogDirector* director_; }; void Widget::Changed() { director->WidgetChanged(this); }

Mediator Sample Code class ListBox : public Widget { public: ListBox(DialogDirector*); virtual const string GetSelection(); virtual void HighLight(string selection); virtual void SetList(list newlistItems); virtual void HandleMouseEvent(MouseEvent& event); private: list listItems; };

Mediator Sample Code class EntryField : public Widget { public: EntryField(DialogDirector*); virtual const string GetText(); virtual void SetText(const string newtext); virtual void HandleMouseEvent(MouseEvent& event); private: string text; };

Mediator Sample Code class Button : public Widget { public: Button(DialogDirector*); virtual void SetText(const string newtext); virtual void HandleMouseEvent(MouseEvent& event); virtual void Activate(); virtual void DeActivate(); private: bool active; };

Mediator Sample Code class FormDialogDirector : public DialogDirector { public: FormDialogDirector() virtual void WidgetChanged(Widget *); protected: virtual void CreateWidgets(); private: ListBox* list; EntryField* field; Button* ok_button; Button* cancel_button; };

Mediator Sample Code void FormDialogDirector::CreateWidgets() { list = new ListBox(this); field = new EntryField(this); ok_button = new Button(this); cancel_button = new Button(this); ok_button->DeActivate(); ok_button->SetText(”OK”); cancel_button->Activate(); cancel_button->SetText(”Cancel”); // fill the ListBox with the available names list->SetList(...); }

Mediator Sample Code void FormDialogDirector::WidgetChanged (Widget* ChangedWidget) { if (ChangedWidget==list) field->SetText(list->GetSelection()); if (ChangedWidget==field) { list->Highlight(field->GetText()); if (field->GetText() != ””) ok_button->Activate(); else ok_button->DeActivate(); } } if (ChangedWidget==ok_button)... }

Mediator Applicability Use the Mediator pattern when A set of objects communicate in well-defined complex ways. The resulting interdependencies are unstructured and difficult to understand. Reusing an object is difficult because it refers to and communicates with many other objects. A behavior that is distributed between several classes should be customizable without a lot of subclassing.

Mediator Pattern Structure Mediator Colleague mediator Concrete Mediator Concrete ColleagueA Concrete ColleagueB

Mediator Pattern Participants Mediator defines an interface for communicating with Colleague objects. ConcreteMediator Implements cooperative behavior by coordinating Colleague objects. Colleague classes Each colleague knows its mediator Each colleague communicates with its Mediator whenever it would have otherwise communicated with another colleague

Mediator Pattern Collaborations Colleagues send and receive requests from a Mediator object. The Mediator implements the cooperative behavior by routing requests between the appropriate colleagues

Mediator Pattern Consequences The Mediator pattern limits subclassing. A mediator localizes behavior that otherwise would be distributed among several objects. Changing this behavior requires subclassing Mediator only, Colleague classes can be reused. The Mediator pattern decouples colleagues. A mediator promotes loose coupling between colleagues. You can vary and reuse Colleague and Mediator classes independently.

Mediator Pattern Consequences The Mediator pattern simplifies object protocols. A mediator replaces many-to-many interactions with one-to-many interactions between the mediator and its colleagues. One- to-many relationships are easier to understand, maintain and extend. The Mediator pattern abstracts how objects cooperate. Making mediation an independent concept and encapsulating it in an object lets you focus on how objects interact apart from their individual behavior. That can help clarify how objects interact in a system.

Mediator Pattern Consequences The mediator pattern centralizes control. The Mediator pattern trades complexity of interaction for complexity in the mediator. Because a mediator encapsulates protocols, it can become more complex than an individual colleague. This can make the mediator itself a monolith that is hard to maintain.

Mediator Pattern Implement. Omitting the abstract Mediator class. There is no need to define an abstract Mediator class when colleagues work with only one mediator. The abstract coupling that the Mediator class provides lets colleagues work with different subclasses and vice versa.

Mediator Pattern Implement. Colleague-Mediator communication. Colleagues have to communicate with their mediator when an event of interest occurs. One approach is to implement the Mediator as an Observer. Colleague classes act as Subjects, sending notifications to the mediator whenever they change state. The mediator responds by propagating the effects of the change to the other colleagues.

Mediator Pattern Fiaspel Game Manager Player manager Token Board Dice manager tokenboard dice

Singleton Pattern The Singleton pattern ensures that a class has only one instance and provides a global point of access to it. Examples: There can be many printers in a system but there should only be one printer spooler. There should be only one instance of a WindowManager (GrainWindowingSystem). There should be only one instance of a filesystem.

Singleton Pattern How do we ensure that a class has only one instance and that the instance is easily accessible? A global variable makes an object accessible, but does not keep you from instantiating multiple objects. A better solution is to make the class itself responsible for keeping track of its sole instance. The class ensures that no other instance can be created (by intercepting requests to create new objects) and it provides a way to access the instance.

Singleton Pattern Use the Singleton pattern when There must be exactly one instance of a class, and it must be accessible to clients from a well-known access point. When the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

Singleton Structure Singleton static Instance() SingletonOperation() GetSingletonData() static uniqueinstance singletonData return uniqueinstance

Singleton Particpants Singleton Defines an Instance operation that lets clients access its unique instance. Instance is a class operation (static member function in C++) May be responsible for creating its own unique instance Client Acesses a Singleton instance solely through Singleton’s Instance operation.

Singleton Consequences Controlled access to sole instance Because the Singleton class encapsulates its sole instance, it can have strict control over how and when clients access it. Reduced name space The Singleton pattern is an improvement over global variables. It avoids polluting the name space with global variables that store sole instances.

Singleton Consequences Permits refinement of operations and representations The Singleton class may be subclassed and it is easy to configure an application with an instance of this extended class at run-time. More flexible than class operations An alternative is to use static member functions in C++. However it is difficult to change the design to allow more than one instance of a class and static member functions in C++ are never virtual, so subclasses can not override polymorphically.

Singleton Implementation Ensuring a unique instance The Singleton pattern makes the sole instance a normal instance of a class, but that class is written so that only one instance can ever be created. A common way to do this is to hide the operation that creates the instance behind a static class operation that guarantees that only one instance is created.

Singleton Sample Code class Singleton { public: static Singleton* Instance(); // clients access the Singleton exclusively through // the Instance() member function protected: Singleton(); // the constructor is protected, such that a client // which tries to instantiate a Singleton object gets // a compiler error private: static Singleton* instance_; };

Singleton Sample Code Singleton* Singleton::instance_ = 0; // initialize static member data of class Singleton Singleton* Singleton::Instance() { if (instance_ == 0) // if not created yet instance_ = new Singleton; // create once return instance_; }