Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion."— Presentation transcript:

1 Criteria for good design

2 aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 Extension Inheritance

11 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

12 Restriction Inheritence

13 Convenience Inheritance

14

15 The Evils of Implementation Inheritance

16 Aggregation as an Alternative to Generalisation

17 Delegation

18 Generalisation V Aggregation

19 Coupling and Cohesion

20 Example of Poor Coupling

21 Example of Poor Cohesion

22 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

23 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.

24 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 levelNo direct coupling bad levelcontent coupling

25 Inheritance Coupling - example Low inheritance

26 Criteria for Good Design - Cohesion

27 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)

28 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) GoodBad FunctionalCoincidental

29 Class Cohesion - example

30 Specialisation Cohesion - example

31 Summary


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

Similar presentations


Ads by Google