























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
























Abstraction Encapsulation Hierarchy Association, Aggregation Inheritance Polymorphism
OOP-
Is it using Objects? Is it using C++, Java, C#, Smalltalk? No, its got to be using UML?! :) What makes a program OO? How do you measure good design? 3
Designing Classes & Objects An incremental, iterative process Difficult to design right the first time
OOP-
“Procedural code gets information and then makes decisions. OO code tells objects to do things,” Alec Sharp in Smalltalk by Example. Objects should take limited responsibility and rely on others to provide appropriate service Command-Query Separation: categorize methods as command or query 7
David Bocks’ The Paper Boy, The Wallet, and The Law Of Demeter
=> drives away shiny new Jaguar! => Customer controls amount, where it’s kept (wallet, hidden in cookie jar,...)
OOP-
You may find code that continues to call on objects returned by methods in sequence customer.getAddress().getCity().getCounty()... This indicates that you are interested in working with objects that are farther away than those that are your close friends Hard to understand Hard to debug Lacks Cohesion Good candidate for refactoring 9
Perils of a bad design Rigidity–Hard to change, results in cascade of changes Fragility–Breaks easily and often Immobility–Hard to reuse (due to coupling) Viscosity–Easy to do wrong things, hard to do right things Needless Complexity–Complicated class design, overly generalized Needless Repetition–Copy and Paste away Opacity –Hard to understand
OOP-
Don’t Repeat Yourself “Every Piece of Knowledge must have a single, unambiguous, authoritative representation within a system” One of the most difficult, but most seen How many times have you see this happen
Font end Application where UI did business validation. Start out due to flaw in Application Engine. Once flaw was rectified, took us weeks to fix the UI due to duplication of validation logic. 13
Some times hard to realize this It is much easier to copy, paste and modify code to get it working the way you want it, isn’t it Duplicating code results in Poor maintainability Expensive to fix bugs/errors Hard to keep up with change
OOP-
Single-Responsibility Principle What metric comes to mind? “A class should have only one reason to change” Some C++ books promoted bad design Overloading input/output operators! What if you do not want to display on a terminal any more? GUI based, or web based? 15
Alarm +alert()
Control System AlarmUI +display(Panel) Alarm +alert() +display(Panel)
Control System Faces more frequent change Has greater dependency (to UI related stuff) Related topics: MVC Analysis model stereotypes : Control Entity Boundary
OOP-
“Software Systems change during their life time” Both better designs and poor designs have to face the changes; good designs are stable 19
Bertrand Meyer: “Software Entities (Classes, Modules, Functions, etc.) should be open for extension, but closed for modification”
OOP- Characteristics of a poor design: Single change results in cascade of changes Program is fragile, rigid and unpredictable Characteristics of good design: Modules never change Extend Module’s behavior by adding new code, not changing existing code
21 Software Modules must be open for extension module’s behavior can be extended closed for modification source code for the module must not be changed
OOP- Strategic Closure: No program can be 100% closed There will always be changes against which the module is not closed Closure is not complete - it is strategic Designer must decide what kinds of changes to close the design for. This is where the experience and problem domain knowledge of the designer comes in
25 Heuristics and Conventions that arise from OCP Make all member variables private encapsulation: All classes/code that depend on my class are closed from change to the variable names or their implementation within my class. Member functions of my class are never closed from these changes Further, if this were public, no class will be closed against improper changes made by any other class No global variables
(^26)
OOP- Heuristics and Conventions that arise from OCP... RTTI is ugly and dangerous If a module tries to dynamically cast a base class pointer to several derived classes, any time you extend the inheritance hierarchy, you need to change the module Not all these situations violate OCP all the time
27 Inheritance is used to realize Abstraction ! and Polymorphism which are key to OCP How do we measure the quality of inheritance? LSP: ! “Functions that use pointers or references to base classes must be ! able to use objects of derived classes without knowing it”
OOP- Design by Contract Advertised Behavior of the Derived class is Substitutable for that of the Base class Substitutability: Derived class Services Require no more and promise no less than the specifications of the corresponding services in the base class
31 “Any Derived class object must be substitutable where ever a Base class object is used, without the need for the user to know the difference”
OOP-
LSP is being used in Java at least in two places Overriding methods can not throw new unrelated exceptions Overriding method’s access can’t be more restrictive than the overridden method for instance you can’t override a public method as protected or private in derived class 33 Bad Design is one that is Rigid - hard to change since changes affect too many parts Fragile - unexpected parts break upon change Immobile - hard to separate from current application for reuse in another
OOP- Inheritance is one of the most abused concepts in OO programming Often, delegation may be a better choice than inheritance When should you use inheritance vs. delegation?
37 If an object of B may be used in place of an object of A, use inheritance If an object of B may use an object of A, then use delegation
OOP- Dependency Inversion Principle “High level modules should not depend upon low level modules. Both should depend upon abstractions.” “Abstractions should not depend upon details. ! Details should depend upon abstractions.” DIP 39
The three principles are closely related Violating either LSP or DIP invariably results in violating OCP It is important to keep in mind these principles to get the most out of OO development