Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.

Slides:



Advertisements
Similar presentations
Winter 2007ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
Advertisements

ANU COMP2110 Software Design in 2003 Lecture 16Slide 1 Lecture 16: Introduction to design patterns 1What are they? 2Where do they come from? 3Why study.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Open-closed principle.
Strategy Pattern1 Design Patterns 1.Strategy Pattern How to design for flexibility?
Introduction to Object Oriented Programming Java.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
Plab – Tirgul 12 Design Patterns
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 More design patterns.
Introduction To System Analysis and Design
UML – Class Diagrams.
. Plab – Tirgul 12 Design Patterns. Design Patterns u The De-Facto Book on Design Patterns:
Object Oriented System Development with VB .NET
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Object-oriented concepts.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Introduction.
Adapters Presented By Zachary Dea. Definition A pattern found in class diagrams in which you are able to reuse an ‘adaptee’ class by providing a class,
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 More on use cases System sequence.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Use cases: deciding what you want.
Spring 2010ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
3/15/05H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Chapter 25 More Design Patterns.
What Is a Factory Pattern?.  Factories are classes that create or construct something.  In the case of object-oriented code languages, factories construct.
Introduction to software design patterns For CSE 3902 By: Matt Boggus.
Programming With Java ICS201 University Of Hail1 Chapter 12 UML and Patterns.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
CS 4240: Bridge and Abstract Factory Readings:  Chap. 10 and 11 Readings:  Chap. 10 and 11.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Introduction To System Analysis and Design
ISP666 MVC & Design Patterns. Outline Review Event Programming Model Model-View-Controller Revisit Simple Calculator Break Design Patterns Exercise.
ANU COMP2110 Software Design in 2004 Lecture 15Slide 1 COMP2110 in 2004 Software Design Lecture 15: Software design patterns (2) 1What are design patterns?
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
DAAD project “Joint Course on OOP using Java” On Object Oriented modeling in Java (Why & How) Ana Madevska Bogdanova Institute of informatics Faculty of.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1 Class 1-2.
What is Object-Oriented?  Organization of software as a collection of discreet objects that incorporate both data structure and behavior.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
SE-2811 Software Component Design Week 1, Day 2 (and 1-3 and 2-1) SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick 1.
Object-Oriented Modeling: Static Models. Object-Oriented Modeling Model the system as interacting objects Model the system as interacting objects Match.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
CS 350 – Software Design Expanding Our Horizons – Chapter 8 The traditional view of objects is that they are data with methods. Sometimes objects could.
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
Design Patterns Introduction
CS 210 Introduction to Design Patterns August 29, 2006.
The Strategy Design Pattern © Allan C. Milne v
Design Patterns in Context ©SoftMoore ConsultingSlide 1.
It started with a simple … A highly successful duck pond simulation game called SimUDuck The game can show a large variety of duck swimming and making.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
SE 461 Software Patterns Welcome to Design Patterns.
Design Patterns: MORE Examples
Unit II-Chapter No. : 5- design Patterns
Strategy Pattern.
Factory Patterns 1.
Behavioral Design Patterns
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
Strategy Design Pattern
SE-2811 Software Component Design
DESIGN PATTERNS : Strategy Pattern
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Software Design Lecture : 27.
HFOOAD Chapter 5 Interlude
Presentation transcript:

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already solved your problems

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 2 Designing Objects with Responsibilities Deciding what methods belong where and how objects should interact carries consequences Knowing UML doesn’t help: it is simply a standard visual modeling language

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 3 Design Patterns If a problem occurs over and over again, a solution to that problem has been used effectively. That solution is described as a pattern. The design patterns are language- independent strategies for solving common object-oriented design problems.

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 4 Design Patterns A design pattern is a general reusable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved.

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 5 Design Patterns Can algorithms be considered design patterns? Algorithms are not thought of as design patterns, since they solve computational problems rather than design problems

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 6 Design Patterns In order to achieve flexibility, design patterns usually introduce additional levels of indirection, which might complicate the resulting designs and hurt application performance.

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 7 Classification of design patterns Use creational patterns when you need to create objects Use structural patterns when relating objects with other objects Use behavioural patterns to give behaviour to objects.

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 8 Using patterns Best way to use patterns: 1.Load your brain with them 2.Recognize places where you can use them

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 9 How many design patterns are there? 23 well konwn from the famous book (Gang of Four – GOF) Some say at least 250

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 10 Ducko’rama As almost everyone else, you need money. So you are employed by a game company and a new game with ducks is in development. The game should show a large variety of ducks that walk, swim and quack.

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 11 Ducko’rama … more ducks Superclass for all ducks Each duck subtype has its own display Abstract method

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 12 Adding functionalities Due to the comercial success of the game, new functionalities will be included for the new release: the ducks in the game can now fly and dive. How would you do it?

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 13 Easy solution! Add the methods for diving and flying in the superclass All subclasses inherit the methods … more ducks

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 14 While the new version was being presented to the shareholds, something weird happend: some rubber ducks started walking and flying around! What happened?

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 15 Inheritance and reuse The use of inheritance allowed reuse of code, but can turn out bad in the maintenance. Possible solution: override the methods

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 16 Maintenance Predicting the future: new types of ducks can be added: woden, robotic, talking, etc… and you will need to override their methods

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 17 Another solution The problem: we need a better way to have only some ducks fly (or dive or walk or quack or swim or …), in other words, not all ducks are equal! How about using interfaces?

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 18 New notation: interfaces and realization supplier Client: must implement the behaviour

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 19 Solution with interfaces Is this a good solution? … more ducks … more interfaces

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 20 Where is the reuse? This solution duplicates code in the subclasses, as they should provide an implementation for the behaviour in the interfaces. If you need to do a small change in a method, you need to change all subclasses that implement it!

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 21 Software is not constant We are lucky that software is always changing: plenty of jobs for us! Some causes: –Your customer is amazed by your demo and wants to add more functionality –Your company was bought and they have their own database –Adaptation to a new hardware –…

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 22 Encapsulation Design principle: separate the aspects that vary from what stays the same Take what varies and encapsulate them This is the main idea for design patterns: they provide a way to let some parts of the system to vary independently of the other parts

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 23 Encapsulation New interfaces for the commom behaviours

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 24 Delegating responsibilities The Duck class will delegate behaviour to other classes Behaviour variables are interface types

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 25 The Duck class in Java public class Duck { QuackBehaviour quackBehaviour;... public void performQuack(){ quackBehaviour.quack() }... }

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 26 Java code for interfaces public interface QuackBehaviour { public void quack(); } public class Quack implements QuackBehaviour{ public void quack(){... }

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 27 Class for new ducks public class CityDuck extends Duck { public CityDuck(){ quackBehaviour = new Quack();... } public void display(){... }... }

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 28 Class diagram … more ducks … more quacks … more flies What relationship are these?

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 29 What have we done? We changed inheritance and used composition instead. Design patterns favour composition over inheritance We encapsulated similar behaviours or algorithms

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 30 Strategy pattern This pattern defines a family of algorithms and encapsulates each one. This makes them interchangeable and independent from the clients.

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 31 Adding flexibility Future versions of the Duck game is becoming more active: a duck can brake its wing. How can you change the flying behaviour at runtime?

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 32 Adding new behaviours The Daffy Duck is going to be included in the game. Modify the classes to include singing and talking ducks.