Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach

web.firat.edu.tr
from web.firat.edu.tr More from this publisher
21.08.2013 Views

12.3 Delegation 153 The concepts of inheritance and subclassing supported by object-oriented languages allow: ■ new classes of objects to be described as extensions or specializations of existing classes, i.e. a new class can be defined as a subclass of an existing class ■ subclasses to inherit the behavior and state of their superclasses. These concepts add extra dimensions to the design process. Taking into account inheritance means that a major design goal is to factor the responsibilities within a hierarchy of classes so that a responsibility attached to a superclass can be shared by all subclasses. In Chapter 13 on refactoring we will see how the design of the cyberspace invaders program can be improved using inheritance. 12.3 ● Delegation This is probably the simplest and most obvious pattern. It describes the situation where one class uses another. Delegation is worth emphasizing as a pattern because there is sometimes a tendency to use inheritance too enthusiastically. Delegation is, in fact, a more general way of extending the functionality of a class. As an example, we will use another game, draughts (UK) or checkers (US). This game takes place on a chess board with a set of black pieces and a set of white pieces. Let us image a program that displays the board and the pieces on the screen as a game is played. A natural (but as we shall see later flawed) structure would be to see that black and white pieces are instances of class Black and White and that these are in turn subclasses of class Piece. The class diagram, showing inheritance, is shown in Figure 12.1. However, there is a problem. When a piece reaches the end of the board, it becomes crowned and thereby gains extra powers. How do we accommodate this in the relationships between classes? The trouble is that once an object is an instance of White, it remains a White. Objects cannot change their class. So inheritance, though appealing, is inappropriate. A better relationship is shown in Figure 12.2. Here classes Black, White, CrownedWhite and CrownedBlack use class Piece in the delegation pattern. Inheritance is absent from this pattern. The class Piece still incorporates all the shared features of the original class Piece – features such as the position on the board. Piece Black White Figure 12.1 Game of draughts showing inheritance

154 Chapter 12 ■ Design patterns But the converse is not true. The moral is: ■ anything that can be accomplished by inheritance can be achieved through delegation ■ everything that can be accomplished by delegation cannot be achieved through inheritance. Delegation is more general mechanism than inheritance. ■ inheritance can be useful for modeling static “is-a” situations ■ inheritance is not appropriate for modeling “is-a-role-played-by” situations ■ delegation is more widely used than inheritance. SELF-TEST QUESTION 12.4 ● Singleton Black White CrownedBlack CrownedWhite Uses Piece Figure 12.2 Game of draughts using delegation 12.1 A soldier in the army is a private, a sergeant or a general. Do we model this as inheritance or delegation? In some systems there only needs to be one instance of a class. Normally, of course, someone writes a class so that any number of objects can be created from it. But occasionally there should only be one. An example is an object to manage the communication between a system and the database. It would be confusing if any number of classes were interacting with the database. Now it would be possible to try to achieve this affect by telling all the programmers on the team that there is one copy of the object, with such-and-such a name, written by Mo. But in a large and complex system, this could be forgotten. We can instead legislate (with the help of compiler checking) using the Singleton pattern. Another example of the Singleton pattern is evident in the cyberspace invaders game (Appendix A), where there should only be one object representing the defender. Uses

154 Chapter 12 ■ Design patterns<br />

But the converse is not true.<br />

The moral is:<br />

■ anything that can be accomplished by inheritance can be achieved through delegation<br />

■ everything that can be accomplished by delegation cannot be achieved through<br />

inheritance. Delegation is more general mechanism than inheritance.<br />

■ inheritance can be useful <strong>for</strong> modeling static “is-a” situations<br />

■ inheritance is not appropriate <strong>for</strong> modeling “is-a-role-played-by” situations<br />

■ delegation is more widely used than inheritance.<br />

SELF-TEST QUESTION<br />

12.4 ● Singleton<br />

Black White CrownedBlack CrownedWhite<br />

Uses<br />

Piece<br />

Figure 12.2 Game of draughts using delegation<br />

12.1 A soldier in the army is a private, a sergeant or a general. Do we model<br />

this as inheritance or delegation?<br />

In some systems there only needs to be one instance of a class. Normally, of course,<br />

someone writes a class so that any number of objects can be created from it. But occasionally<br />

there should only be one. An example is an object to manage the communication<br />

between a system and the database. It would be confusing if any number of<br />

classes were interacting with the database. Now it would be possible to try to achieve<br />

this affect by telling all the programmers on the team that there is one copy of the<br />

object, with such-and-such a name, written by Mo. But in a large and complex system,<br />

this could be <strong>for</strong>gotten. We can instead legislate (with the help of compiler checking)<br />

using the Singleton pattern.<br />

Another example of the Singleton pattern is evident in the cyberspace invaders game<br />

(Appendix A), where there should only be one object representing the defender.<br />

Uses

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!