1 Advanced Object-oriented Design – Principles and Patterns OO Design Principles.

Slides:



Advertisements
Similar presentations
•7/12 /07 F-1 © 2010 T. Horton CS 4240 Principles of SW Design Packages in Java and UML.
Advertisements

Stability and Volatility is Software Design H. Rahnama Tutorial in software engineering.
General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
Lecture 9 Improving Software Design CSC301-Winter 2011 – University of Toronto – Department of Computer Science Hesam C. Esfahani
SOLID Object Oriented Design Craig Berntson
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 6/e and are provided with permission by.
Developed by Reneta Barneva, SUNY Fredonia Component Level Design.
Software Issues Derived from Dr. Fawcett’s Slides Phil Pratt-Szeliga Fall 2009.
TEST-1 7. Object-Oriented Design Principles. TEST-2 The Pillars of the Paradigm Abstraction Encapsulation Hierarchy –Association, Aggregation –Inheritance.
1 OO Design Principles Project Group eXtreme Programming Md. Abul Bashar 07/09/2004.
CLASS DESIGN PRINCIPLES Lecture 2. The quality of the architecture What is a good design? It is the design that at least does not have signs of “bad”.
1 OO Design Novosoft, 2001 by V. Mukhortov. 2 OO Design Goals  Flexibility Changes must be localized  Maintainability Modules requiring changes can.
Ch:10 Component Level Design Unit 4. What is Component? A component is a modular building block for computer software Because components reside within.
Company Confidential – Do Not Duplicate 2 Copyright 2008 McLane Advanced Technologies, LLC S.O.L.I.D. Software Development Achieving Object Oriented Principles,
CSC 395 – Software Engineering Lecture 12: Reusability –or– Programming was Bjarne Again.
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
CSE 301 Exam Revision Lecture
S.O.L.I.D. Software Development 12 January 2010 (Martin Verboon, Patrick Kalkman, Stan Verdiesen)
SWE © Solomon Seifu ELABORATION. SWE © Solomon Seifu Lesson 12-5 Software Engineering Design Goals.
4/1/05F-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Packages and Components in Java and UML.
Design Principles iwongu at gmail dot com.
1 OO Package Design PrinciplesStefan Kluth 4OO Package Design Principles 4.1Packages Introduction 4.2Packages in UML 4.3Three Package Design Principles.
Software Design Principles
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency.
OO Design Principles Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
1 Software Engineering: A Practitioner’s Approach, 6/e Chapter 11a: Component-Level Design Software Engineering: A Practitioner’s Approach, 6/e Chapter.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
Five design principles
CHAPTER 3 MODELING COMPONENT-LEVEL DESIGN.
Principles of Object Oriented Design
SOLID Design Principles
These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 6/e and are provided with permission by.
1 Good Object-Oriented Design Radu Marinescu Lecture 3 Principles of Object-Oriented Design Part II 19 th November 2002.
1 OO Class Design PrinciplesStefan Kluth 3OO Class Design Principles 3.1Dependency Management 3.2The Copy Program 3.3Class Design Principles.
Principles of Package Design COMPOSE AT A HIGHER LEVEL OF ABSTRACTION.
1 Advanced Object- oriented Design – Principles CS320 Fall 2005.
Mantas Radzevičius ifm-2/2
Design Patterns: MORE Examples
Software Design Principles
Chapter 12 Object Oriented Design Principles
Software Architecture & Difference from Design
MPCS – Advanced java Programming
Low Budget Productions, LLC
Chapter 12 Object Oriented Design Principles
Pertemuan 08 Design Patterns & Anti-Patterns
CSE687 - Object Oriented Design class notes Survey of the C++ Programming Language Jim Fawcett Spring 2004.
Copyright © by Curt Hill
Chapter 9 - Programming to Interfaces
Software Engineering: A Practitioner’s Approach, 6/e Chapter 11 Component-Level Design copyright © 1996, 2001, 2005 R.S. Pressman & Associates, Inc.
Principles of Package Architecture
Object Oriented Practices
Software Design Principles
lecture 08, OO Design Principle
Object-Oriented Design
Design Tips.
SAMANVITHA RAMAYANAM 18TH FEBRUARY 2010 CPE 691
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
The SOLID Principles.
Applying Use Cases (Chapters 25,26)
Principles of Object-Oriented Design Part II
A (partial) blueprint for dealing with change
Principles of Object-Oriented Design
Object-Oriented PHP (1)
Principles of High-Level Design
Dependency Inversion principle
Some principles for object oriented design
Chapter 10 – Component-Level Design
Presentation transcript:

1 Advanced Object-oriented Design – Principles and Patterns OO Design Principles

2 Elements of Bad Design

3 Software Rigidity n Rigidity is the tendency for software to be difficult to change, even in simple ways. n Symptom: Every change causes a cascade of subsequent changes in dependent modules. n Effect: When software behaves this way, managers fear to allow developers to fix non- critical problems. This reluctance derives from the fact that they don’t know, with any reliability, when the developers will be finished.

4 Software Fragility n Fragility is the tendency of the software to break in many places every time it is changed. Often the breakage occurs in areas that have no conceptual relationship with the area that was changed. n Symptom: Every fix makes it worse, introducing more problems than are solved. n Effect: Every time mangers/ team leaders authorize a fix, they fear that the software will break in some unexpected way.

5 Software Immobility n Immobility is the inability to reuse software from other projects or from parts of the same project. n Symptom: A developer discovers that he needs a module that is similar to one that another developer wrote. But the module in question has too much baggage that it depends upon. After much work, the developer discovers that the work and risk required to separate the desirable parts of the software from the undesirable parts are too great to tolerate. n Effect: And so the software is simply rewritten instead of reused.

6 Software Viscosity n Viscosity is the tendency of the software/ development environment to encourage software changes that are hacks rather than software changes that preserve original design intent. n Symptom: It is easy to do the wrong thing, but hard to do the right thing. n Effect: The software maintainability degenerates due to hacks, workarounds, shortcuts, temporary fixes etc.

7 Why bad design results? n Obvious reasons: lack of design skills/ design practices, changing technologies, time/ resource constraints, domain complexity etc. n Not so obvious: –Software rotting is a slow process.. Even originally clean and elegant design may degenerate over the months/ years.. –Unplanned and improper module dependencies creep in; Dependencies go unmanaged. –Requirements often change in the way the original design or designer did not anticipate..

8 OO Design Principles  The Open/Closed Principle (OCP)  The Liskov Substitution Principle (LSP)  The Dependency Inversion Principle (DIP)  The Interface Segregation Principle (ISP)  The Reuse/Release Equivalency Principle (REP)  The Common Closure Principle (CCP)  The Common Reuse Principle (CRP)  The Acyclic Dependencies Principle (ADP)  The Stable Abstractions Principle (SAP)

9 Topic 1: The Open/Closed Principle (OCP) A module should be open for extension but closed for modification.

10 The open/ closed principle (OCP) n We should write our modules so that they can be extended, without requiring them to be modified. In other words, we want to be able to change what the modules do, without changing the source code of the modules. n How?: Abstraction and Polymorphism

11 The open/ closed principle (OCP) Example

12 The open/ closed principle (OCP) Example

13 The open/ closed principle (OCP) Discussion If I need to create a new shape, such as a Triangle, I must modify the ‘drawShape()' function. If I need to create a new shape, such as a Triangle, I must modify the ‘drawShape()' function. In a complex application the switch/case statement above is repeated over and over again for every kind of operation that can be performed on a shape. In a complex application the switch/case statement above is repeated over and over again for every kind of operation that can be performed on a shape. Worse, every module that contains such a switch/case statement retains a dependency upon every possible shape that can be drawn, thus, whenever one of the shapes is modified in any way, the modules all need recompilation, and possibly modification Worse, every module that contains such a switch/case statement retains a dependency upon every possible shape that can be drawn, thus, whenever one of the shapes is modified in any way, the modules all need recompilation, and possibly modification However, when the majority of modules in an application conform to the open/closed principle, then new features can be added to the application by adding new code rather than by changing working code. Thus, the working code is not exposed to breakage. However, when the majority of modules in an application conform to the open/closed principle, then new features can be added to the application by adding new code rather than by changing working code. Thus, the working code is not exposed to breakage.

14 Topic 2: The Liskov Substitution Principle (LSP) Subclasses should be substitutable for their base classes.

15 The Liskov Substitution Principle (LCP) n A client of a base class should continue to function properly if a derivative of that base class is passed to it. In other words, if some function takes an argument ot type Policy, then it should be legal to pass in an instance of Personal Auto Policy to that provided Personal Auto Policy is directly/ indirectly derived from Policy. In other words, if some function takes an argument ot type Policy, then it should be legal to pass in an instance of Personal Auto Policy to that provided Personal Auto Policy is directly/ indirectly derived from Policy.

16 The Liskov Substitution Principle (LCP) Example

17 The Liskov Substitution Principle (LCP) Discussion Is Square a Rectangle ? Mathematically yes, Behaviorally, a Square is not a Rectangl e and it is behavior that software is really all about. Is Square a Rectangle ? Mathematically yes, Behaviorally, a Square is not a Rectangl e and it is behavior that software is really all about. n It is only when derived types are completely substitutable for their base types that functions which use those base types canbe reused with impunity, and the derived types can be changed with impunity. n Violations of LSP are latent violations of OCP.

18 Topic 3: The Dependency Inversion Principle (DIP) Depend upon Abstractions. Do not depend upon concretions.

19 The Dependency Inversion Principle (DIP) n Dependency Inversion is the strategy of depending upon interfaces or abstract functions and classes, rather than upon concrete functions and classes. n Every dependency in the design should target an interface, or an abstract class. No dependency should target a concrete class.

20 The Dependency Inversion Principle (DIP) Example Dependency Structure of a Procedural Architecture

21 The Dependency Inversion Principle (DIP) Example Dependency Structure of an Object Oriented Architecture

22 The Dependency Inversion Principle (DIP) Discussion n One motivation behind the DIP is to prevent you from depending upon volatile modules. n Typically, Concrete things change a lot, abstract things change much less frequently. n Abstractions are “hinge points”, they represent the places where the design can bend or be extended, without themselves being modified (OCP) n One of the most common places that designs depend upon concrete classes is when those designs create instances. By definition, you cannot create instances of abstract classes. There is an elegant solution to this problem named Abstract Factory

23 Topic 4: The Interface Segregation Principle (ISP) Many client specific interfaces are better than one general purpose interface

24 The Interface Segregation Principle (ISP) n If you have a class that has several clients, rather than loading the class with all the methods that the clients need, create specific interfaces for each type of client and multiply inherit them into the class.

25 The Interface Segregation Principle (ISP) Example Fat Service with Integrated Interfaces

26 The Interface Segregation Principle (ISP) Example Segregated Interfaces

27 The Interface Segregation Principle (ISP) Discussion Without segregation whenever a change is made to one of the methods that ClientA calls, ClientB and ClientC may be affected. It may be necessary to recompile and redeploy them. With segregation if the interface for ClientA needs to change, ClientB and ClientC will remain unaffected. Without segregation whenever a change is made to one of the methods that ClientA calls, ClientB and ClientC may be affected. It may be necessary to recompile and redeploy them. With segregation if the interface for ClientA needs to change, ClientB and ClientC will remain unaffected. n The ISP does not recommend that every class that uses a service have its own special interface class that the service must inherit from. Rather, clients should be categorized by their type, and interfaces for each type of client should be created. If two or more different client types need the same method, the method should be added to both of their interfaces.

28 Topic 5: The Release Reuse Equivalency Principle (REP) The granule of reuse is the granule of release.

29 The Release Reuse Equivalency Principle (REP) n A reusable element, be it a component, a class, or a cluster of classes, cannot be reused unless it is managed by a release system of some kind. n Clients will/ should refuse to reuse an element unless the author promises to keep track of version numbers, and maintain old versions for awhile.Therefore, one criterion for grouping classes into packages is reuse. n Since packages are the unit of release in JAVA, they are also the unit of reuse. Therefore architects would do well to group reusable classes together into packages.

30 Topic 6: The Common Closure Principle (CCP) Classes that change together, belong together.

31 The Common Closure Principle (CCP) n The work to manage, test, and release a package is non-trivial in a large system. The more packages that change in any given release, the greater the work to rebuild, test, and deploy the release. Therefore we would like to minimize the number of packages that are changed in any given release cycle of the product. n To achieve this, we group together classes that we think will change together.

32 Topic 7: The Common Reuse Principle (CRP) Classes that aren’t reused together should not be grouped together.

33 The Common Reuse Principle (CRP) n A dependency upon a package is a dependency upon everything within the package. When a package changes, and its release number is bumped, all clients of that package must verify that they work with the new package - even if nothing they used within the package actually changed. n Hence, Classes that aren’t reused together should not be grouped together in a package.

34 The Package Cohesion Principles (REP/CCP/CRP) Discussion n These three cannot simultaneously be satisfied. n The REP and CRP makes life easy for re-users, whereas the CCP makes life easier for maintainers. n The CCP strives to make packages as large as possible (after all, if all the classes live in just one package, then only one package will ever change). The CRP, however, tries to make packages very small. n Early in a project, architects may set up the package structure such that CCP dominates for ease of development and maintenance. Later, as the architecture stabilizes, the architects may re-factor the package structure to maximize REP and CRP for the external re-users.

35 Topic 8: The Acyclic Dependencies Principle (ADP) The dependencies betwen packages must not form cycles.

36 The Acyclic Dependencies Principle (ADP) n Once changes to a package are made, developers can release the packages to the rest of the project. Before they can do this release, however, they must test that the package works. To do that, they must compile and build it with all the packages it depends upon. n A single cyclic dependency that gets out of control can make the dependency list very long. n Hence, someone needs to be watching the package dependency structure with regularity, and breaking cycles wherever they appear.

37 The Acyclic Dependencies Principle (ADP) Example Acyclic Package Network

38 The Acyclic Dependencies Principle (ADP) Example Cyclic Package Network

39 The Acyclic Dependencies Principle (ADP) Discussion In the acyclic scenario to release the p rotocol package, the engineers would have to build it with the latest release of the c omm_error package, and run their tests. In the acyclic scenario to release the p rotocol package, the engineers would have to build it with the latest release of the c omm_error package, and run their tests. In the cyclic scenario to release protocol, the engineers would have to build it with the latest release of the c omm_error, gui, comm, process, modem, file and run their tests. In the cyclic scenario to release protocol, the engineers would have to build it with the latest release of the c omm_error, gui, comm, process, modem, file and run their tests. n Breaking the cycle: –Add new package in between –Add a new Interface

40 The Acyclic Dependencies Principle (ADP) Discussion Breaking Cycle by introducing an Interface

41 Topic 9: The Stable Abstractions Principle (SAP) Stable packages should be abstract packages.

42 The Stable Abstractions Principle (SAP) n Stability is related to the amount of work required to make a change. A package with lots of incoming dependencies is very stable because it requires a great deal of work to reconcile any changes with all the dependent packages.

43 The Stable Abstractions Principle (SAP) Example

44 The Stable Abstractions Principle (SAP) Discussion n The packages at the top are instable and flexible. But those at the bottom are very difficult to change. n The highly stable packages at the bottom of the dependency network may be very difficult to change, but according to the OCP they do not have to be difficult to extend. If the stable packages at the bottom are also highly abstract, then they can be easily extended. n It is possible to compose our application from instable packages that are easy to change, and stable packages that are easy to extend. n The SAP is just a restatement of the DIP.

45 Resources: Principles and Patterns n Designing Object-Oriented C++ Applications using the Booch Method By Robert C. Martin (1994) n Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Grady Booch n n n n n