Difference between revisions of "OOP Design Principles"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(favour composition over inheritance)
(favour composition over inheritance)
Line 12: Line 12:
 
Prefer composition over inheritance as it is more malleable / easy to modify later, but do not use a compose-always approach. With composition, it's easy to change behavior on the fly with Dependency Injection / Setters. Inheritance is more rigid as most languages do not allow you to derive from more than one type. So the goose is more or less cooked once you derive from TypeA.
 
Prefer composition over inheritance as it is more malleable / easy to modify later, but do not use a compose-always approach. With composition, it's easy to change behavior on the fly with Dependency Injection / Setters. Inheritance is more rigid as most languages do not allow you to derive from more than one type. So the goose is more or less cooked once you derive from TypeA.
  
My acid test for the above is:
+
==Test 1==
  
 
  Does TypeB want to expose the complete interface (all public methods no less) of TypeA such that TypeB can be used where TypeA is expected?
 
  Does TypeB want to expose the complete interface (all public methods no less) of TypeA such that TypeB can be used where TypeA is expected?
Indicates need for Inheritance.  
+
 
 +
This indicates the need for Inheritance.  
  
 
e.g. A Cessna biplane will expose the complete interface of an airplane, if not more. So that makes it fit to derive from Airplane.  
 
e.g. A Cessna biplane will expose the complete interface of an airplane, if not more. So that makes it fit to derive from Airplane.  
  
  Does TypeB want only some/part of the behavior exposed by TypeA? Indicates need for Composition.  
+
==Test 2==
 +
  Does TypeB want only some/part of the behavior exposed by TypeA?  
 +
 
 +
this indicates the need for Composition.  
  
 
e.g. A Bird may need only the fly behavior of an Airplane. In this case, it makes sense to extract it out as an interface / class / both and make it a member of both classes.
 
e.g. A Bird may need only the fly behavior of an Airplane. In this case, it makes sense to extract it out as an interface / class / both and make it a member of both classes.

Revision as of 07:22, 6 June 2018

OOP has added many features and elements to basic Procedure-oriented programming. Writing good code, or designing OOP programs well, requires more care and planning. To help with writing good programs, OOP comes with some Design Principles to help us design and plan our programs.

encapsulate what varies

"A test for good software design is how well it can deal with future change. As the cliche truthfully claims, the only constant is change. Inevitably any piece of software that is in use will be asked to change. Business needs will evolve or the problem space will be better understood, etc. Whatever the reason, the software will need to change. A good design will allow for that change without too much work. A bad design will be very hard to modify. " ... "When designing software, look for the portions most likely to change and prepare them for future expansion by shielding the rest of the program from that change. Hide the potential variation behind an interface. Then, when the implementation changes, software written to the interface doesn’t need to change. This is called encapsulating variation." https://blogs.msdn.microsoft.com/steverowe/2007/12/26/encapsulate-what-varies/

favour composition over inheritance

Prefer composition over inheritance as it is more malleable / easy to modify later, but do not use a compose-always approach. With composition, it's easy to change behavior on the fly with Dependency Injection / Setters. Inheritance is more rigid as most languages do not allow you to derive from more than one type. So the goose is more or less cooked once you derive from TypeA.

Test 1

Does TypeB want to expose the complete interface (all public methods no less) of TypeA such that TypeB can be used where TypeA is expected?

This indicates the need for Inheritance.

e.g. A Cessna biplane will expose the complete interface of an airplane, if not more. So that makes it fit to derive from Airplane.

Test 2

Does TypeB want only some/part of the behavior exposed by TypeA? 

this indicates the need for Composition.

e.g. A Bird may need only the fly behavior of an Airplane. In this case, it makes sense to extract it out as an interface / class / both and make it a member of both classes.

program to interfaces, not implementation

Worked example w.r.t. "finding commonality in otherwise unrelated items"

http://stackoverflow.com/a/384067