31
Criteria for good design

Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

  • View
    216

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Criteria for good design

Page 2: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

aim

to appreciate the proper and improper uses

of inheritance and appreciate the concepts

of coupling and cohesion

Page 3: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Advanced Generalisation and Inheritance Modelling

• Generalisation (specialisation) and inheritance are not the same– generalisation is a semantic relationship between classes– the interface of the subclass must include all properties

of the superclass• Inheritance

– the mechanism by which more specific elements incorporate structure and behaviour defined by more general elements• Generalisation is a useful and powerful concept but can

also create many problems due to the intricate mechanisms of inheritance

Page 4: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Generalisation and Substitutability

• Generalisation introduces additional classes and categorises them into generic and more specific classes

– establishes superclass-subclass relationships into the model• The benefits of generalisation arise from the substitutability principle

– a subclass object can be used in place of a superclass object in any part of the code where the superclass object is accessed– this means it can reduce the overall number of

association and aggregation relationships in the model– unfortunately, inheritance may be used in a way that defeats the benefits of the substitutability principle

Page 5: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

The Substitution Principle

• Defined by Barbara Liskov, so known as the Liskov Substitution Principle (LSP)

– it should be possible to treat a derived object as if

it were a base object

Page 6: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Inheritance v. Encapsulation• Encapsulation

– an object's state should only be accessible via the operations

in the object's interface• Inheritance may compromise encapsulation

– subclasses may be allowed to access protected attributes directly– objects belonging to different classes may be friends of

each other or have elements with package visibility

• Applications should be designed so that they balance the desired level of encapsulation against inheritance

Page 7: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Interface Inheritance v. Implementation Inheritance

• Interface inheritance– when generalisation is used with the aim of substitutability, it can

be realised using interface inheritance (subtyping, or type inheritance)

this is both a “harmless” as well as a very desirable form of inheritance

a subclass inherits attribute types and operation signatures the implementation of inherited operations may be deferred– there is a difference between an interface class and an abstract

class interface classes defer the definition of operations – these are defined or implemented by another class abstract classes can provide a partial implementation of some operations

Page 8: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Interface Inheritance v. Implementation Inheritance

• Implementation inheritance– when generalisation is used to imply code reuse it is realised using implementation inheritance– implementation inheritance combines the superclass properties in the subclasses and allows them to be overridden with new implementations when necessary

-- overriding can mean the inclusion (or call) of a superclass method in the subclass method-- overriding can also mean a complete replacement of the superclass method by the subclass method

• Implementation inheritance can be harmful if it is not carefully controlled

Page 9: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Extension Inheritance

• The only proper use of inheritance is as an incremental definition of a class

– a subclass has more properties than its superclass

– a subclass is a kind of superclass• This type of inheritance is known as extension inheritance• In extension inheritance, the overriding of properties

should be used with care– it should be used to make properties more

specific, and not to change the meaning of the property

Page 10: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Extension Inheritance

Page 11: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Restriction Inheritance

• It is possible to use inheritance as a restriction mechanism whereby some of the inherited properties are suppressed or overridden in the subclass – this type of inheritance is known as restriction inheritance

• Restriction inheritance is problematic– a subclass does not include all the properties of the superclass but a superclass object can still be substituted by a subclass object -- whoever is using the object should be aware of the overridden properties

• Restriction inheritance may give rise to maintenance problems

Page 12: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Restriction Inheritence

Page 13: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Convenience Inheritance

Page 14: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Convenience Inheritance

Page 15: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

The Evils of Implementation Inheritance

Page 16: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Aggregation as an Alternative to Generalisation

Page 17: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Delegation

Page 18: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Generalisation V Aggregation

Page 19: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Coupling and Cohesion

Page 20: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Example of Poor Coupling

Page 21: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Example of Poor Cohesion

Page 22: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Criteria for Good Design - Coupling

• Coupling – a good design will minimise coupling– describes the degree of interconnectedness between design components– is reflected by the number of links an object has and by the degree of interaction the object has with other objects

• Interaction coupling– a measure of the number of message types an objects sends and the number of parameters passed with these message types

• Inheritance coupling– the degree to which a subclass needs the features it inherits from its base class

Page 23: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Types of Interaction Coupling(1)

• No Direct Coupling– The methods do not relate to one another, that is, they do not call one another

• Data– The calling method passes a variable to the called method. If the variable is a

composite, (i.e., an object), the entire object is used by the called method to perform its function.

• Stamp– The calling method passes a composite variable (i.e., an object) to the called

method, but the called method only used a portion of the object to perform its function.

• Control– The calling method passes a control variable whose value will control the

execution of the called method.

Page 24: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Types of Interaction Coupling(2)

• Common or Global– The methods refer to a “global data area” that is outside the individual objects.

• Content or Pathological– A method of one object refers to the inside (hidden part) of another object. This

violates the principles of encapsulation and information hiding.

good level No direct coupling

bad levelcontent coupling

Page 25: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Inheritance Coupling - example

Low inheritance

Page 26: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Criteria for Good Design - Cohesion

Page 27: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Types of Operation Cohesion (1)

• Functional– A method perform a single problem-related task (e.g., Calculate current GPA)

• Sequential– The method combines two functions in which the output from the first one is used

as the input to the second one (e.g., format and validate current GPA)

• Communicational– The method combines two functions that use the same attributes to execute

(e.g., calculate current and cumulative GPA)

• Procedural– The method supports multiple weakly related functions. (e.g., calculate student

GPA, print student record, calculate cumulative GPA, and print cumulative GPA)

Page 28: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Types of Operation Cohesion (2)

• Temporal– A method support multiple related functions in time (e.g., initialize all attributes)

• Logical– The method support multiple related functions, but the choice of the specific

function is chosen based on a control variable that is passed into the method (e.g., called method could open a checking account, open a saving account, or calculate a loan, depending on the message that is send by its calling method)

• Coincidental– The purpose of the method cannot be defined or it performs multiple functions

that are unrelated to one another. (e.g., update customer records, calculate loan payments, print exception reports, and analyze competitor pricing structure)

Good Bad

Functional Coincidental

Page 29: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Class Cohesion - example

Page 30: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Specialisation Cohesion - example

Page 31: Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

Summary