Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 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

2 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

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

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

5 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

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

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

8 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

9 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

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

11 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

12 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?

13 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

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

15 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

16 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

17 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?

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

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

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

21 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 –…

22 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

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

24 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

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

26 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(){... }

27 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(){... }... }

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

29 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

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

31 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?

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


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

Similar presentations


Ads by Google