Navigation: Appendix B: Dolphin Pattern Book > Class Patterns >
You are implementing a New Class and discover that you are adding methods that are identical to those in an already existing sibling class. How is it possible to reuse the implementation of these methods to avoid the unnecessary duplication and the additional maintenance load this incurs.
It should be possible to refactor the local class hierarchy to arrange that the two (or more) classes each become a specialisation of a common superclass. However, in doing so it may be that the new superclass does not contain enough state to ever be instantiable in its own right. That is, this class is one that can never realistically have instances of its own. Such a class is called an Abstract Class.
It is common for an abstract class to define the common representation and/or behaviour of its concrete subclasses. However, sometimes it is not possible to implement the common methods at this level and they must be deferred to the Concrete Subclasses. In these situations it is usual to define the common methods using stub methods. These stub methods in the abstract class demonstrate the method protocol that the subclasses must provide and serve as a placeholder for Method Comments describing the protocol. The stub methods should contain a #subclassResponsibility message send.
Imagine two new classes: CurrentAccount and SavingsAccount with the following attributes:
CurrentAccount (number, balance, cheques)
SavingsAccount (number, balance, interestRate)
The number and balance attributes are common to both classes. We can create an abstract class BankAccount to implement these common attributes (and their corresponding methods), and subclass SavingsAccount and CurrentAccount from it.
BankAccount (number, balance)
The Boolean class is an abstract class with two concrete subclasses: True and False. The Boolean class implements behaviour common to True and False (#asBoolean, #deepCopy etc). It also defines the protocol which is common to True and False but which will be implemented by them (#and:, #ifTrue: etc).