Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design...

48
Object-Oriented Design Heuristics Harald Gall, Prof. Dr. http://seal.ifi.unizh.ch

Transcript of Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design...

Page 1: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

Object-Oriented DesignHeuristics

Harald Gall, Prof. Dr.http://seal.ifi.unizh.ch

Page 2: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

2

Design Heuristics

Object-Oriented Design Heuristics byArthur Riel, Addison-Wesley, 1996.

Page 3: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

3

Goal

Insights into oo design improvement.

More than sixty guidelines are language-independent and allow one to rate theintegrity of a software design.

The heuristics are not written as hard andfast rules; they are meant to serve aswarning mechanisms which allow theflexibility of ignoring the heuristic asnecessary.

Page 4: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

Classes and Objects

The Building Blocks of the Object-Oriented Paradigm

Page 5: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

5

Hidding Data

Heuristic #2.1All data should be hidden within its class

Page 6: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

6

No Dependence on Clients

Heuristic #2.2Users of a class must be dependent on itspublic interface, but a class should not bedependent on its users

Page 7: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

7

Support Class = 1 Clear Responsibility

Heuristic #2.3Minimize the number of messages in theprotocol of a class

Page 8: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

8

Supporting Polymorphism andCommunication

Heuristic #2.4Implement a minimal public interface which allclasses understand (e.g. operations such ascopy (deep versus shallow), equality testing,pretty printing, parsing from a ASCII description,etc.).

To send the same message to different objects To be able to substitute them

Example: Object>>printString, Object>>copy…

Page 9: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

9

Clear Public Interface

Heuristic #2.5Do not put implementations details such ascommon-code private functions into the publicinterface of a class

Example: Private/protected in C++ Private method categories in Smalltalk

Do not clutter the public interface of a class withitems that clients are not able to use or are notinterested in using

Page 10: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

10

Minimize Classes Interdependencies

Heuristic #2.7A class should only use operations in thepublic interface of another class or havenothing to do with that class

Page 11: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

11

Support a Class = one Responsibility

Heuristic #2.8A class should capture one and only onekey abstraction

Page 12: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

12

Strengthen Encapsulation

Heuristic #2.9Keep related data and behavior in oneplace

Spin off non related information intoanother class

-> Move Data Close to Behavior

Page 13: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

13

Object: a Cohesive Entity

Most of the methods defined on a classshould be using most of the instancevariables most of the time

Page 14: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

14

Roles vs. Classes

Heuristic #2.11Be sure the abstractions you model areclasses and not the roles objects play

Are mother and father classes or role ofPerson?

No magic answer: Depends on the domain Do they have different behavior? So they

are more distinct classes

Page 15: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

Topologies of Action-Orientedvs. Object-Oriented Applications

Page 16: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

16

Support 1 Class = 1 Responsibility

Heuristic #3.1Distribute system intelligence horizontallyas uniformly as possible, i.e., the top-levelclasses in a design should share the work

Page 17: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

17

Support 1 Class = 1 Responsibility

Heuristic #3.2Do not create god classes/objects (classesthat control all other classes). Be verysuspicious of classes whose namecontains Driver, Manager, System,SubSystem

Page 18: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

18

Model and Interfaces

Heuristic #3.5Model should never be dependent on theinterface that represents it. The interfaceshould be dependent on the model

What is happening if you want twodifferent UIs for the same model?

Page 19: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

19

Basic Checks for God Class Detection

Heuristic #3.3Beware of classes that have many accessormethods defined in their public interface. Mayimply that data and behavior is not being kept atthe same place

Heuristic #3.4Beware of classes having methods that onlyoperate on a proper subset of the instancevariables.

Page 20: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

20

One Class: One Responsibility

One responsibility: coordinating and usingother objects OrderedCollection maintains a list of objects

sorted by arrival order: two indexes and a list

Class should not contain more objectsthan a developer can fit in his short-termmemory. (6 or 7 is the average value)

Page 21: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

21

Classes Evaluation

Model the real world whenever possible

Eliminate irrelevant classes

Eliminate classes that are outside of the system

A method is not a class. Be suspicious of anyclass whose name is a verb or derived from averb, especially those that only one piece ofmeaningful behavior

Page 22: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

The Relationships BetweenClasses and Objects

Page 23: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

23

Minimizing Coupling between Classes

Minimize the number of classes with whichanother class collaborates

Minimize the number of messages sentbetween a class and its collaborators Counter example: Visitor pattern

Minimize the number of differentmessages sent between a class and itscollaborators

Page 24: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

24

About the Use Relationship

When an object use another one it should get areference on it to interact with it

Ways to get references (containment) instance variables of the class Passed has argument Ask to a third party object (mapping…) Create the object and interact with it (coded in class:

kind of DNA)

Page 25: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

25

Containment and Uses

Heuristic #4.5If a class contains object of another class, thenthe containing class should be sendingmessages to the contained objects (thecontainment relationship should always imply auses relationships)

A object may know what it contains but it shouldnot know who contains it.

Page 26: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

26

Coherence in Classes

Heuristic #4.6Most of the methods defined on a class shouldbe using most of the data members most of thetime.

Heuristic #4.7Classes should not contain more objects than adeveloper can fit in his or her short termmemory. A favorite value for this number is six.

Heuristic #4.8Distribute system intelligence vertically downnarrow and deep containment hierarchies.

Page 27: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

27

Representing Semantics Constraints

How do we represent possibilities or constraintsbetween classes? Appetizer, entrée, main dish… No peas and corn together…

It is best to implement them in terms of classdefinition but this may lead to class proliferation

=> implemented in the creation method

Page 28: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

28

Objects define their logic

Heuristic #4.9When implementing semantic constraintsin the constructor of a class, place theconstraint definition as far down acontainment hierarchy as the domainallows

=> Objects should contain the semanticconstraints about themselves

Page 29: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

29

Third party constraint holder

Heuristic #4.11The semantic information on which a constraintis based is best placed in a central third-partyobject when that information is volatile.

Heuristic #4.12The semantic information on which a constraintis based is best decentralized among theclasses involved in the constraint when thatinformation is stable.

Page 30: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

The Inheritance Relationship

Page 31: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

31

Classes - Subclasses

Superclass should not know its subclasses

Subclasses should not use directly data ofsuperclasses

If two or more classes have common data andbehavior, they should inherit from a commonclass that captures those data and behavior

Page 32: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

32

Inheritancs for Specialization

Heuristic #5.1Inheritance should only be used to model aspecialization hierarchy.

Vererbung ist ein White-box-Entwurf Aggregation oder Komposition definieren einen

Black-box-Entwurf Richtiger Einsatz von Vererbung erleichtert das

Programmverständnis enorm!

Page 33: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

33

Base Class Knowledge

Heuristic #5.2Derived classes must have knowledge oftheir base class by definition, but baseclasses should not know anything abouttheir derived classes.

Page 34: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

34

Base Class Data is Private

Heuristic #5.3 All data in a base class should be private,

i.e. do not use protected data.

Page 35: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

35

Inheritance Depth

Heuristic #5.4Theoretically, inheritance hierarchies should bedeep, i.e. the deeper the better.

Heuristic #5.5Pragmatically, inheritance hierarchies should beno deeper than an average person can keep intheir short term memory. A popular value forthis depth is six.

Page 36: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

36

Controversial

All abstract classes must be base classes

All base classes should be abstractclasses > Not true they can have default value method

Page 37: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

37

Interfaces

Heuristic #5.8Factor the commonality of data, behavior,and/or interface as high as possible in theinheritance hierarchy.

Heuristic #5.10If two or more classes have common data andbehavior (i.e. methods) then those classesshould each inherit from a common base classwhich captures those data and methods.

Page 38: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

38

Inheritance (2)

Heuristic #5.9If two or more classes only share common data(no common behavior) then that common datashould be placed in a class which will becontained by each sharing class.

Heuristic #5.11If two or more classes only share commoninterface (i.e. messages, not methods) thenthey should inherit from a common base classonly if they will be used polymorphically.

Page 39: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

39

Avoid Type Checks

Explicit case analysis on the type of an objects isusually an error.

An object is responsible of deciding how toanswer to a message

A client should send message and notdiscriminate messages sent based on receivertype

Page 40: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

40

Dynamic semantics

Heuristic #5.14Do not model the dynamic semantics of a classthrough the use of the inheritance relationship.An attempt to model dynamic semantics with astatic semantic relationship will lead to a togglingof types at runtime.

Heuristic #5.15Do not turn objects of a class into derivedclasses of the class. Be very suspicious of anyderived class for which there is only oneinstance.

Page 41: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

Multiple Inheritance

Page 42: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

42

Prove Multiple Inheritance

Heuristic #6.1If you have an example of multipleinheritance in your design, assume youhave made a mistake and proveotherwise.

Page 43: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

43

Question it

Heuristic #6.2Whenever there is inheritance in an object-oriented design ask yourself two questions: 1) Am I a special type of the thing I'm inheriting from? 2) Is the thing I'm inheriting from part of me?

Heuristic #6.3Whenever you have found a multiple inheritancerelationship in a object-oriented design be surethat no base class is actually a derived class ofanother base class, i.e. accidental multipleinheritance.

Page 44: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

The Association Relationship

Page 45: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

45

Containment

Heuristic #7.1When given a choice in an object-orienteddesign between a containmentrelationship and an associationrelationship,choose the containment relationship.

Page 46: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

Class Specific Data andBehavior

Page 47: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

47

Use of Class Variables & Methods

Heuristic #8.1Do not use global data or functions toperform bookkeeping information on theobjects of a class, class variables ormethods should be used instead.

Page 48: Object-Oriented Design Heuristics - UZHffffffff-fd5f-cdf8-ffff... · 2016-06-23 · 2 Design Heuristics Object-Oriented Design Heuristics by Arthur Riel, Addison-Wesley, 1996.

48

Summary

Use the guidelines for insightful analysis critical reviews as guide for better oo design to build reusable components and

frameworks