Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Open-closed principle.

Slides:



Advertisements
Similar presentations
CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
Advertisements

Chapter 4: The Factory Pattern. Consider the Following Code Fragment Duck duck; if (picnic) { duck = new MallardDuck(); } else if (hunting) { duck = new.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
SWE 4743 Strategy Patterns Richard Gesick. CSE Strategy Pattern the strategy pattern (also known as the policy pattern) is a software design.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 More design patterns.
UML – Class Diagrams.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
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.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
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.
+ Informatics 122 Software Design II Lecture 8 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
3/15/05H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Inheritance and Subclasses in Java CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Inheritance using Java
Programming Languages and Paradigms Object-Oriented Programming.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Design Patterns.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Chapter 1: Introduction to Design Patterns. SimUDuck Example.
Module 7: Object-Oriented Programming in Visual Basic .NET
Case Studies on Design Patterns Design Refinements Examples.
1. 2 Object-Oriented Concept Class & Object Object-Oriented Characteristics How does it work? Relationships Between Classes Development Tools Advantage.
Chapter 26 GoF Design Patterns. The Adapter Design Pattern.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
1 University of Sunderland Advanced OO Development “Building a small bank system” © James Malone, 2003.
Inheritance in Java. RHS – SOC 2 What is inheritance (in Java)? Inheritance is a mechanism for enhancing existing classes What does that mean…? Defining.
Design Patterns Façade, Singleton, and Factory Methods Team Good Vibrations (1)
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
Week 2, Day 2: The Factory Method Pattern Other good design principles Cohesion vs. Coupling Implementing the Strategy Pattern Changing strategies (behaviors)
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1 Class 1-2.
Frameworks & Patterns Use of Organized Classes. Frameworks vs Toolkits Framework Framework  Start with classes and interfaces that define a rudimentary.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Object-Oriented Programming Chapter Chapter
The Strategy Pattern SE-2811 Dr. Mark L. Hornick 1.
ISBN Object-Oriented Programming Chapter Chapter
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Pattern Bridge. Definition Bridge is the structural pattern that separates abstraction from the implementation so that both of them can be changed independently.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CET203 SOFTWARE DEVELOPMENT Session 2A Inheritance (programming in C#)
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
COP 4331 – OOD&P Lecture 7 Object Concepts. What is an Object Programming language definition: An instance of a class Design perspective is different.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
SE 461 Software Patterns Welcome to Design Patterns.
Design Patterns: MORE Examples
Strategy: A Behavioral Design Pattern
Sections Inheritance and Abstract Classes
Low Budget Productions, LLC
Week 2, Day 1: The Factory Method Pattern
Behavioral Design Patterns
Week 4 Object-Oriented Programming (1): Inheritance
Inheritance B.Ramamurthy 11/7/2018 B.Ramamurthy.
CSC 205 Java Programming II
Software Design Lecture : 12.
Software Design Lecture : 14.
SE-2811 Software Component Design
Design pattern Lecture 9.
Object-Oriented PHP (1)
Object Oriented Design & Analysis
Presentation transcript:

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Open-closed principle

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 2 Object connectivity Client classes use services from supplier classes: A module (class) is open when it’s available for extension (add, enlarge). A module is closed if it’s not subject to further change (it has a fixed number of functions). Client Supplier >

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 3 Advantages Closed module: can be used as a stable component in the system, avoiding changes that potentially affect the whole design Open module: make it possible to extend and modify the system in the awake of new capabilities and functionalities, keeping maintenace costs down

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 4 Open-Closed Principle We should try to produce modules that are simultaneously open and closed. This looks like a contradiction!

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 5 General solution Distinguish between interface and implementation. –Implementation can be modified without affecting clients. –How can we do this? What’s the interface?

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 6 General solution The interface is the public definitions public class Supplier { private int attribute; public void operation() { // implementation of operation } } Operation visible to the client

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 7 General solution - advantages We can change the implementation of the operation in the supplier without the need to change the client We can add new operations and attributes also without the need to change any the client Is this a good solution?

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 8 General solution - limitations In C++, modifications in the header (as if you want to add a private attribute in the Supplier) requires clients modules to be recompilated, even if the changes are invisible to them. –The open-closed principle should be language- independent. Different clients of a module can use different subsets of the visible interface. –It is difficult to know exactly what changes to a module will affect a given client. Can we have a better solution?

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 9 Let’s use abstract classes New subclasses can be added, extending (or specializing) the AbstractSupplier or providing alternative implementation of the interface.

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 10 Java’s code public abstract class AbstractSupplier { public abstract void operation(); } public class ConcreteSupplier extends AbstractSupplier { private int attribute; public void operation() { // implementation of operation } }

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 11 Abstract classes Dependencies: The use of abstract classes is fundamental for working with design patterns.

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 12 Abstract classes What happens If we need to modify the interface of the AbstractSupplier due to changes on the needs of the client (like add a new operation)? We need more principles…

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 13 Principle: No Concrete SuperClass All Superclasses should be abstract. Only leaf classes should be concrete. Why? Let’s see an example… Changes ahead: the bank wants to add a savings account. How would you do that? A class to provide accounts in a bank.

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 14 Easy solution: The subclass inherits the shared functionalities Concrete superclass

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 15 Doing changes Now the bank decides to allow accounts become overdraw, but not on SavingsAccount’s. How to do that?

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 16 One possibility public class CurrentAccount {... void withdraw (double amt) { if (this.instanteof SavingsAccount) { // check if becoming overdraw } balance -= amt; }... } What are possible problems with this solution? Chech the type of the account

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 17 Problems with extension Public class CurrentAccount { void withdraw (double amt) { if (this.instanteof SavingsAccount) { // check if becoming overdraw } balance -= amt; } } If there is a new subclass, changes on this code will be necessary: the module is not closed. The superclass is aware of the existence of subclasses!

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 18 Problems with extension void cashCheque (currentAccount a) { if (this.instanceof SavingsAccount) { return; } // cash cheque } Adding new functionalities. For example: add cheques capabilities, but not on SavingsAccounts. Could be like: What is causing the problems?

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 19 The cause of the problem void cashCheque (currentAccount a) { if (this.instanceof SavingsAccount) { return; } // cash cheque } Role 1: as a superclass, this interface is implemented by all accounts objects and subclasses Role 2: it is also providing a default implementation This superclass has two roles

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 20 General solution All superclasses should be abstract. Overrides with the code that check if becoming overdraw

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 21 Coupled design Suppose you need to add a function to the interface due to new requirements. How can you do it without modifying the AbstractSupplier?

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 22 Coupled design you have to modify the abstract supplier maybe clients should be recompiled this new function will have to be implemented in all existing concrete supplier classes or a default implementation can be provided by the abstract supplier Suppose you need to add a function to the interface due to new requirements…

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 23 Decoupling the Interface Instead of making abstract supplier as a class, make it as an interface. So, concrete providers are no longer subclasses of abstract supplier, but instead provide realizations of the interfaces. How to do this?

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 24 Making an interface

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 25 Extending the interface Now we can extend the interface to provide a new operation:

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 26 More Design Pattern Factory Method Design Pattern

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 27 Back to Duck’orama … more ducks … more quacks … more flying types We used the strategy pattern in the example

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 28 Creating a new object How do you have the concret duck objects?... Duck duck; if (picnic) { duck = new MallarDuck(); } else if (hunting) ) { duck = new WildDuck(); } else if (inBathTube) { duck = new RubberDuck(); }... The objects are created according to a context

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 29 Problems You might have several places in the system where this kind of code for creating objects is repeated If you need to do a change…... Duck duck; if (picnic) { duck = new MallarDuck(); } else if (hunting) ) { duck = new WildDuck(); } else if (inBathTube) { duck = new RubberDuck(); } duck.showBehaviour();... Does this code follow the open-closed principle?

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 30 Identifying what varies We want to encapsulate the code the object creation:... Duck duck; ????? duck.showBehaviour();... if (picnic) { duck = new MallarDuck(); } else if (hunting) ) { duck = new WildDuck(); } else if (inBathTube) { duck = new RubberDuck(); } How can we do this?

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 31 Simple solution Move the code to a new class: public class DuckFactory { public Duck createDuck(DuckType t){ Duck duck = null; if (t == picnic) { duck = new MallarDuck(); } else if (t == hunting) ) { duck = new WildDuck(); } else if (t == inBathTube) { duck = new RubberDuck(); } return duck; }

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 32 Using the factory Let’s have store of ducks: public class DuckStore { DuckFactory df; public DuckStore(DuckFactory df){ this.df = df; } public Duck getDuck(DuckType t){ Duck duck; duck = df.createDuck(t); duck.showBehaviour();... return duck; }... } The store has the factory to build the ducks. Every time you need a new duck, you just get it from the store.

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 33 Example Write the main program using the factory and the store of ducks. public class DuckFactory { public Duck createDuck(DuckType t){ Duck duck = null; if (t == picnic) { duck = new MalarDuck(); } else if (t == hunting) ) { duck = new WildDuck(); } else if (t == inBathTube) { duck = new RubberDuck(); } return duck; } public class DuckStore { DuckFactory df; public DuckStore(DuckFactory df){ this.df = df; } public Duck getDuck(DuckType t){ Duck duck; duck = df.createDuck(t); duck.showBehaviour();... return duck; }... }

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 34 Adding new duck types Now it is much easier to inclue a new type of duck, and you only need to change one place: the factory. Draw the class diagram StrangeDuck CoiledDuck WersternDuck USBDuck

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 35 More changes Due to the huge success of the game, the company decided to develop a new game, the Duck Raider, with several types of strange ducks. How would you do? Can you reuse the code?

Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 36 Deriving the pattern in the whiteboard