stoop ed-subtyping subclassing

17
Stéphane Ducasse 1 Stéphane Ducasse [email protected] http://stephane.ducasse.fr ee.fr/ Design Points - Subclassing vs Subtyping Stéphane Ducasse --- 2010

Upload: the-world-of-smalltalk

Post on 02-Dec-2014

635 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Stoop ed-subtyping subclassing

Stéphane Ducasse 1

Stéphane [email protected]://stephane.ducasse.free.fr/

Design Points - Subclassing vs Subtyping

Stéphane Ducasse --- 2010

Page 2: Stoop ed-subtyping subclassing

S.Ducasse 2

A little problem

How to implement a Stack?

Page 3: Stoop ed-subtyping subclassing

S.Ducasse 3

Stack subclass of OrderedCollection

Stack>>pop ^ self removeFirstStack>>push: anObject self addFirst: anObjectStack>>top ^ self first

Stack>>size, Stack>>includes: are free, inherited from

Page 4: Stoop ed-subtyping subclassing

S.Ducasse 4

Wait! but • What do we do with all the rest of the interface of

OrderedCollection?

• a Stack IS NOT an OrderedCollection!

• We cannot substitute an OrderedCollection by a Stack

• Some messages do not make sense on Stack• Stack new addLast: anObject• Stack new last

• So we have to block a lot of methods...

Page 5: Stoop ed-subtyping subclassing

S.Ducasse 5

Consequences...

Stack>>removeFirst self shouldNotImplement

Stack>>pop ^ super removeFirst

Page 6: Stoop ed-subtyping subclassing

S.Ducasse 6

The Problem• There is not a simple relationship between

Stack and OrderedCollection

• Stack interface is not an extension or subset of OrderedCollection interface

• Compare with CountingStack a subclass of Stack

• CountingStack interface is an extension of Stack interface

Page 7: Stoop ed-subtyping subclassing

S.Ducasse 7

Compare the two uses

Page 8: Stoop ed-subtyping subclassing

S.Ducasse 8

Compare the two replacements

Page 9: Stoop ed-subtyping subclassing

S.Ducasse 9

A Better Approach

By defining the class Stack that uses OrderedCollection

Object subclass: Stack instVarNames: ‘elements’

Stack>>push: anElement elements addFirst: anElementStack>>pop element isEmpty ifFalse: [^ element removeFirst]

Page 10: Stoop ed-subtyping subclassing

S.Ducasse 10

Inheritance and Polymorphism• Polymorphism works best with

conforming/substituable interfaces

• Inheritance creates families of classes with similar interfaces

• An abstract class describes an interface fulfilled by its subclasses

• Inheritance helps software reuse by making polymorphism easier

Page 11: Stoop ed-subtyping subclassing

S.Ducasse 11

About subtyping and subclassing• You have only extends or subclass: in

programming language.

• But they can be used to define a subclassing or subtyping relationship.

Page 12: Stoop ed-subtyping subclassing

S.Ducasse 12

Subtyping Inheritance • Reuse of specifications• A subclass refines superclass specifications

• A program that works with Numbers will work with Fractions.

• A program that works with Collections will work with Arrays.

Page 13: Stoop ed-subtyping subclassing

S.Ducasse 13

Subclassing• Inheritance for code reuse• Dictionary is a subclass of Set• Semaphore is a subclass of LinkedList• No relationship between the interfaces of the

classes

• Subclass reuses code from superclass, but has a different specification. It cannot be used everywhere its superclass is used. Usually overrides a lot of code.

• ShouldNotImplement use is a bad smell…

Page 14: Stoop ed-subtyping subclassing

S.Ducasse 14

Inheritance for Code Reuse • Inheritance for code reuse is good for• rapid prototyping

• getting application done quickly.

• Bad for:• easy to understand systems• reusable software• application with long life-time.

Page 15: Stoop ed-subtyping subclassing

S.Ducasse 15

Subtyping Essence• You reuse specification

• You should be able to substitute an instance by one of its subclasses (more or less)

• There is a relationship between the interfaces of the class and its superclass

Page 16: Stoop ed-subtyping subclassing

S.Ducasse 16

How to Choose?• Favor subtyping

• When you are in a hurry, do what seems easiest.

• Clean up later, make sure classes use “is-a-subtype” relationship, not just “is-implemented-like”.

• Is-a-subtype is a design decision, the compiler only enforces is-implemented-like!!!

Page 17: Stoop ed-subtyping subclassing

S.Ducasse 17

Quizz– Circle subclass of Point? – Poem subclass of OrderedCollection?