Object Oriented. Analysis and Design

Similar documents
OODP OOAD Session 7a

OODP Session 5a. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

OODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

OODP Session 9. Session times PT Thursday 18:00 21:00 room: School of Pharmacy, John Hanbury Lecture Theatre FT Tuesday 13:30 17:00 room: Malet 404

Software Eningeering. Lecture 9 Design Patterns 2

Design Patterns Lecture 2

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

The Design Patterns Matrix From Analysis to Implementation

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

Brief Note on Design Pattern

COSC 3351 Software Design. Design Patterns Behavioral Patterns (I)

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

An Introduction to Patterns

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

SDC Design patterns GoF

Applying Some Gang of Four Design Patterns CSSE 574: Session 5, Part 3

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

Design patterns. Jef De Smedt Beta VZW

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

DESIGN PATTERN - INTERVIEW QUESTIONS

be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

Design Patterns. An introduction

Design Patterns Reid Holmes

Design of Software Systems (Ontwerp van SoftwareSystemen) Design Patterns Reference. Roel Wuyts

Factory Method. Comp435 Object-Oriented Design. Factory Method. Factory Method. Factory Method. Factory Method. Computer Science PSU HBG.

18.1 Definitions and General OO Principles

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

Component ConcreateComponent Decorator ConcreateDecoratorA ConcreteDecoratorB

Mac OS X and ios operating systems. Lecture 5 Design patterns. Tomasz Idzi

Lecture 17: Patterns Potpourri. Copyright W. Howden 1

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

Object-Oriented Design

COSC 3351 Software Design. Design Patterns Behavioral Patterns (II)

GRASP 2. CSC 440: Software Engineering Slide #1

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

From Design Patterns: Elements of Reusable Object Oriented Software. Read the sections corresponding to patterns covered in the following slides.

The Strategy Pattern Design Principle: Design Principle: Design Principle:

Last Lecture. Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/ Spring Semester, 2005

Object-Oriented Design

Object-Oriented Concepts and Design Principles

Design Patterns. Dr. Rania Khairy. Software Engineering and Development Tool

Introduction to Software Engineering: Object Design I Reuse & Patterns

A Reconnaissance on Design Patterns

Software Design COSC 4353/6353 D R. R A J S I N G H

Using Design Patterns in Java Application Development

Review Software Engineering October, 7, Adrian Iftene

Design Patterns. CSE870: Advanced Software Engineering (Design Patterns): Cheng

Object Oriented Design and Programming Revision

GRASP Design Patterns A.A. 2018/2019

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

Design Patterns! Acknowledgements!

Lecture 20: Design Patterns II

Information systems modelling UML and service description languages

CS/CE 2336 Computer Science II

Tuesday, October 4. Announcements

UNIT I Introduction to Design Patterns

Creational. Structural

CPSC 427a: Object-Oriented Programming

Design Patterns: From Analysis to Implementation BJECTIVES

Design Patterns. CSC207 Winter 2017

An Introduction to Patterns

Design Patterns. CSC207 Fall 2017

CS 2720 Practical Software Development University of Lethbridge. Design Patterns

Design Pattern Detection

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

2/23/04 Doc 10 State & Chain of Responsibility slide # 1

Lecture 13: Design Patterns

Design Patterns V Structural Design Patterns, 2

Software Reengineering Refactoring To Patterns. Martin Pinzger Delft University of Technology

» Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request!

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

Java Technologies. Lecture III. Valdas Rapševičius. Vilnius University Faculty of Mathematics and Informatics

(c) Bartosz Walter 1

ADVANCED SOFTWARE DESIGN LECTURE 7 GRASP

Egon Borger (Pisa) Capturing Design Pattern Abstractions by ASMs

Implementing GUI context-sensitive help... ECE450 Software Engineering II. Implementing GUI context-sensitive help... Context-sensitive help

Appendix-A. A.1 Catalogues of Design Patterns. Below is the definition for each design pattern using the FINDER notation, followed

Design Patterns Design patterns advantages:

State Pattern. CSIE Department, NTUT Woei-Kae Chen. State: Intent. Allow an object to alter its behavior when its internal state changes.

1 Software Architecture

Eliminate enterprise software design instability - protect variations! Nickolay Kofanov

17. GRASP: Designing Objects with Responsibilities

Design Pattern Detection

Software Engineering - I An Introduction to Software Construction Techniques for Industrial Strength Software

Design Patterns. Comp2110 Software Design. Department of Computer Science Australian National University. Second Semester

The Chain of Responsibility Pattern

The Chain of Responsibility Pattern. Design Patterns In Java Bob Tarr

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

Application Architectures, Design Patterns

Patterns and Testing

UNIT I Introduction to Design Patterns

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

Design Patterns IV Structural Design Patterns, 1

Transcription:

OODP- OOAD Session 5 Object Oriented Analysis and Design Oded Lachish Department of Computer Science and Information Systems Birkbeck College, University of London Email: oded@dcs.bbk.ac.uk Web Page: http://www.dcs.bbk.ac.uk/~oded

Previously on OOAD GRASP design patterns Information Expert Creator Low Coupling High Cohesion Controller (don t think in patterns, think patterns)

Classification (GoF Gang of Four) Creational Patterns Abstract object instantiation Structural Patterns Compose and organise objects into larger structures Behavioural Patterns Algorithms, interactions and control flow between objects.

Previously on OOAD List of Advanced patterns we already discussed Strategy (behavioural) Builder (creational) Decorator (structural) Factory Method Abstract Factory Singleton

More Advanced Patterns Bridge

http://www.guardian.co.uk/technology/2011/mar/17/us-spy-operation-social-networks Revealed: US spy operation that manipulates social media Military's 'sock puppet' software creates fake online identities Jeff Jarvis

Bridge Pattern Problem: We want to decouple people from their Facebook identities Person swim () drive() travel() Why can t we settle for inheritance? SmartPerson swim () drive() travel() BravePerson swim () drive() travel()

Bridge Pattern Solution: delegate implementation Person swim () drive() travel() ImpAI ImpPerson swim () drive() Problem: there maybe different AI s, with added responsibilities void Person::travel() { } ImpAI->drive(); ImpAI->swim(); ImpSmartPerson swim() drive() ImpBravePerson swim() drive()

Bridge Pattern Solution: Inheritance Abstraction Implementor Implementor operationalpha () operationbeta () impoperation1 () impoperation2 () RefinedAbst1 RefinedAbst2 ConcreteImplementorA ConcreteImplementorB operationalpha () operationbeta () operationgamma () operationalpha () operationbeta () operationdelta () impoperation1 () impoperation2 () impoperation1 () impoperation2 ()

Discussion Why use? Avoiding permanent binding Abstraction and implementations are extensible Implementation changes have no impact on clients Implementation hidden Implementations can be shared (what is the difference from strategy)

State

DriverAI We want to simulate an AI that among other things drives To make it feel human it should have moods The driving and only the driving should depend on the AI s mood One solution is to, give it a mood attribute. What is the problem with this solution? AI mood Swim () drive()

State Pattern Solution: delegate mood dependent behaviors AI mood mood drive() Angry drive() Happy drive() Sad drive()

State Pattern, Consequences Easy to add more states More classes Less code State transition appears explicitly in the DCD Context request() State handle() ConcreteStateA handle() ConcreteStateB handle() ConcreteStateC handle()

State Pattern, What s Missing? Who is responsible for the state transitions? Context ConcreteState classes What are the pros and cons of each option? Context request() State handle() ConcreteStateA handle() ConcreteStateB handle() ConcreteStateC handle()

Discussion Why Use? Issues Objects behaviour depends on its state + messy code to implement this When are states created How to add more states Context request() State handle() ConcreteStateA handle() ConcreteStateB handle() ConcreteStateC handle()

Back to GRASP

More GRASP patterns Information Expert, Creator, Low Coupling, High Cohesion, Controller Polymorphism Indirection Pure Fabrication Protected Variations

Polymorphism

Pattern Name: Polymorphism Problem It Solves: How to handle alternatives based on type? How to create pluggable software components? Solution: When related alternatives or behaviour vary by type, assign responsibilities for the behaviour using polymorphism Alternatives based on type replacing code by classes and types Pluggable software components viewing components in client server relationships, how can you replace one server component with another without affecting the client

Where have we seen Polymorphism GoF Design Patterns: Strategy State Bridge Abstract Factory

Indirection

Pattern Name: Indirection Problem It Solves: Where to assign a responsibility, to avoid direct coupling between two or more things Solution: Assign the responsibility to an intermediate object

Where have we seen Indirection GoF Design Patterns: Strategy State

Pure Fabrication

Pattern Name: Pure Fabrication Problem It Solves: What object should have the responsibilities, when you don t want to violate high cohesion and low coupling, or other goals, but solutions offered by Expert (for example) are not appropriate. Solution: Assign a highly cohesive set of responsibilities to an artificial class that does not represent a problem domain concept (such a class is a fabrication of the imagination).

Where have we seen Pure Fabrication GoF Design Patterns: Strategy Abstract Factory

Protected Variation

Pattern Name: Protected Variation Problem It Solves: How to design objects, subsystems, and systems so that the variations or instability in these elements does not have an undesirable impact on other elements? Solution: Identify points of predicted variation or instability; assign responsibilities to create a stable interface around them.

Where have we seen Protected Variation GoF Design Patterns: Strategy Decorator Abstract Factory...

Advanced Patterns Again MVC

Example: Model-View-Controller (MVC) Model-View-Controller group of classes used to build interfaces (originally in Smalltalk, Java Swing, EPOC uses MVC). Model - application object, View - presentation on screen, Controller - how user input controls the interface

Two views of the data

MVC Model must notify views of change. Views must keep themselves up to date. Several controllers e.g. command keys, popup menus can be used. Usually controllers organised in a class hierarchy -> sub-classing

MVC Advantages MVC decouples the model management (data) from the representation (views) and the reaction to user input (controllers). Increased flexibility. Allows to have multiple (synchronized views) on the same data. Allows to associate different controllers with each of the views if needed. Change the way in which the interface reacts without changing the interface. Views can be easily composed.

Observer

PassengerAI We want to simulate an AI that is the passenger in a car when the AI, from the state pattern is the driver. To make the new AI feel human it should change its behavior according to how the driver AI drives What is the problem with this solution? DriverAI mood notify () PassengerAI Maybe we don t even know how many passengers are in the care?

PassengerAI Solution: teach them the subject observer relation Subject Observers Observer attach(observer) detach(observer) notify() update() Call every observer on the list DriverAI PassengerAI s PassengerAI mood mood getmood() Get mood (possibly also other stuff) update()

Discussion Why Use? A change to one object requires change to another object One object can notify others without any dependency on their class Issues Need some mechanism to say what changed Without maintenance, may get an unwanted cascade of updates

Adapter

Adapter Problem: How to resolve incompatible interfaces, or provide a stable interface to similar components with different interfaces? Solution: Convert the original interface of a component into another interface through an intermediate adapter object

Class Adapter How: Inheritance client Target Adaptee request() specificrequest() (implementation) Adapter request() All the Gluing code

Object Adapter How: Inheritance client Target Adaptee request() specificrequest() Adapter adaptee request() All the Gluing code

Discussion Why Use? Want to use other people code Class Adapter advantages: Simpler Class Adapter Issues Works only for one specific class and not its subclasses Object Adapter Issues Harder to override adaptee behaviour

Memento

Memento Problem: How to store the internal state of an object without violating encapsulation? Motivation: Support undo

Memento Originator Memento Caretaker state setmemento(memento m) CreateMemento() state getstate() setstate() This doesn t end here! Only the originator should be able to access and store information in the memeto How?

Memento Code(C++) class State; Class Originator { public: Memento* creatememento* CreateMemento(); void setmemento(const Memento*); //.. private: State* _state; // internal data structure }; class Memento { public: virtual Memento(); private: friend class Originator; // Private member accessible only to Originator Memento(); void setstate(state*); State* getstate(); //.. State* _state; //.. };

Discussion Why Use? Want to store internal state without Not just use other peoples code but override some of it Issues More classes Works in C++ what about other languages Cohesion

Chain of Responsibility

Chain of Responsibility Problem: How to handle a request when it is not known which object should deal with it? Motivation: Recall the monopoly game In the example (with the GRASP pattern) we had a controller. What if in order to avoid cohesion we need many controllers, one for each complex operation On top of that we want to be able to add more operations

Chain of Responsibility successor client Handler handlerequest() ConcreteHandler1 handlerequest() ConcreteHandler2 handlerequest()

Chain of responsibility code (C++) Class HelpHandler { public: HelpHandler(HelpHandler* s) : _successor(s) { } virtual void HandleHelp(int code); //.. private: HelpHandler* _successor; }; Void HelpHandler::HandleHelp(int code) { if(_succesor){ _successor-> HandleHelp(int code); } } Class controller1: public HelpHandler { Public: virtual void HandleHelp(int code); // overrides original function }

Discussion Why Use? Easy to add new request handlers No need to specify who handles the request Issues Representing requests Not guaranteed that request will be answered