Presentation is loading. Please wait.

Presentation is loading. Please wait.

Stéphane Ducasse 1 Elements of Design - Inheritance/Compositio n.

Similar presentations


Presentation on theme: "Stéphane Ducasse 1 Elements of Design - Inheritance/Compositio n."— Presentation transcript:

1 Stéphane Ducasse stephane.ducasse@inria.fr http://stephane.ducasse.free.fr/ 1 Elements of Design - Inheritance/Compositio n

2 S.Ducasse 2 A Formating Text Editor With several possible algorithms formatWithTex formatFastColoring formatSlowButPreciseColoring

3 S.Ducasse 3 Code Smells Composition>>repair formatting == #Simple ifTrue: [ self formatWithSimpleAlgo] ifFalse: [ formatting == #Tex ifTrue: [self formatWithTex]....]

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 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 S.Ducasse 6 Delegating to other Objects myEditor setColorizer: FastColorizer new. myEditor setColorizer: AdvancedColorizer new. Strategy design pattern

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

8 S.Ducasse 8 Another example

9 S.Ducasse 9 Delegation of Responsibilities New requirement: A document can be printed on different printers for example lw100s or lw200s depending on which printer is first encountered.

10 S.Ducasse 10 Ad-hoc Solution LanPrinter>>accept: aPacket (thePacket addressee = #*lw*) ifTrue: [ self print: thePacket] ifFalse: [ (thePacket isAddressedTo: self) ifTrue: [self print: thePacket] ifFalse: [super accept: thePacket]] Limits: not general brittle because based on a convention adding a new kind of address behavior requires editing the class Printer

11 S.Ducasse 11 Create Object and Delegate An alternative solution: isAddressedTo: could be sent directly to the address With the current solution, the packet can still control the process if needed

12 S.Ducasse 12 NodeAddress NodeAddress is responsible for identifying the packet receivers Packet>>isAddressedTo: aNode ^ self address isAddressedTo: aNode address “was name” Object subclass: #NodeAddress instanceVariableNames: ‘id‘ NodeAddress>>isAddressedTo: aNodeAddress ^ self id = aNodeAddress id Refactoring Remark: name was not a good name anyway, and now it has become an address -> we should rename it.

13 S.Ducasse 13 Matching Address For packets with matchable addresses Packet send: ‘lulu’ to: (MatchingAddress with: #*lw*) Address subclass: #MatchingAddress instanceVariableNames: ‘’ MatchingAddress>>isAddressedTo: aNodeAddress ^ self id match: aNodeAddress id

14 S.Ducasse 14 Addresses Object subclass: #Address instanceVariableNames: ‘id‘ Address>>isAddressedTo: anAddress ^self subclassResponsibility Address subclass: #NodeAddress instanceVariableNames: ‘‘ Address subclass: #MatchingAddress instanceVariableNames: ‘‘

15 S.Ducasse 15 Trade-Off Delegation Pros No blob class: one class one responsibility Variation possibility Pluggable behavior without inheritance extension Runtime pluggability Delegation Cons Difficult to follow responsibilities and message flow Adding new classes = adding complexities (more names) New object


Download ppt "Stéphane Ducasse 1 Elements of Design - Inheritance/Compositio n."

Similar presentations


Ads by Google