1 / 27

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. Advanced Generalisation and Inheritance Modelling. Generalisation (specialisation) and inheritance are not the same

odell
Télécharger la présentation

Criteria for good design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. Convenience Inheritance

  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. Inheritance Coupling - example

  24. Criteria for Good Design - Cohesion

  25. Class Cohesion - example

  26. Specialisation Cohesion - example

  27. Summary

More Related