270 likes | 490 Views
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
E N D
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
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
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
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
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
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
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
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
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