Stoop ed-inheritance composition

450 views

Published on

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

Stoop ed-inheritance composition

  1. 1. Stéphane Ducasse Stéphane Ducasse stephane.ducasse@inria.fr http://stephane.ducasse.free.fr/ 1 Elements of Design - Inheritance/Compositio n
  2. 2. S.Ducasse 2 A Formating Text Editor With several possible algorithms formatWithTex formatFastColoring formatSlowButPreciseColoring
  3. 3. S.Ducasse 3 Code Smells Composition>>repair formatting == #Simple ifTrue: [ self formatWithSimpleAlgo] ifFalse: [ formatting == #Tex ifTrue: [self formatWithTex] ....]
  4. 4. S.Ducasse 4 Inheritance? May not be the solution since: - you have to create objects of the right class - it is difficult to change the policy at run-time - you can get an explosion of classes bloated with the use of a functionality and the functionalities. - no clear identification of responsibility
  5. 5. S.Ducasse 5 Inheritance vs. Composition Inheritance is not a panacea Require class definition Require method definition Extension should be prepared in advance No run-time changes Ex: editor with spell-checkerS, colorizerS, mail- readerS…. No clear responsibility Code bloated Cannot load a new colorizers
  6. 6. S.Ducasse 6 Delegating to other Objects myEditor setColorizer: FastColorizer new. myEditor setColorizer: AdvancedColorizer new. Strategy design pattern
  7. 7. S.Ducasse 7 Composition Analysis Pros Possibility to change at run-time Clear responsibility No blob Clear interaction protocol Cons New class Delegation New classes

×