Docsity

Measuring Good Object-Oriented Design: Principles and Best Practices, Study Guides, Projects, Research of Design

Partial preview of the text

Download Measuring Good Object-Oriented Design: Principles and Best Practices and more Study Guides, Projects, Research Design in PDF only on Docsity!

Object-Oriented

Design Principles

The Pillars of the

Paradigm

Abstraction Encapsulation Hierarchy Association, Aggregation Inheritance Polymorphism

OOP-

What’s OO?

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

Measuring Quality

of an Abstraction

Designing Classes & Objects An incremental, iterative process Difficult to design right the first time

OOP-

Tell Don’t Ask

“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 Bock’s Example

on LoD/TDA

David Bocks’ The Paper Boy, The Wallet, and The Law Of Demeter

Failing LoD

PaperBoy’s method does:

customer.waller.totalMoney;

Honoring LoD

PaperBoy’s method does:

customer.getPayment(...);

=> drives away shiny new Jaguar! => Customer controls amount, where it’s kept (wallet, hidden in cookie jar,...)

OOP-

Train Wreck Coding

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

Bad design

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-

DRY

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

Execution

Engine

(chokes on

certain

names of

objects)

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

DRY

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-

SRP

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

SRP…

Alarm +alert()

UI

Control System AlarmUI +display(Panel) Alarm +alert() +display(Panel)

UI

Control System Faces more frequent change Has greater dependency (to UI related stuff) Related topics: MVC Analysis model stereotypes : Control Entity Boundary

OOP-

Nature of code

“Software Systems change during their life time” Both better designs and poor designs have to face the changes; good designs are stable 19

OCP…

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

OCP…

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

OCP…

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

OCP…

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

OCP…

(^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

OCP…

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”

Liskov Substitution

Principle

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

Design by Contract

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”

LSP

OOP-

LSP in Java?

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

Nature of Bad Design

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?

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

Inheritance Vs.

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 Founding

Principles

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