Software Engineering for Students A Programming Approach
Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach
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
- Page 126 and 127: > 8.2 Case study 103 A statement th
- Page 128 and 129: start button event create defender
- Page 130 and 131: 8.3 ● Discussion Abstraction One
- Page 132 and 133: Exercises 109 skill. On the other h
- Page 134 and 135: CHAPTER 9 This chapter explains: 9.
- Page 136 and 137: 9.2 Identifying data flows 113 Noti
- Page 138 and 139: 9.3 Creation of a structure chart 1
- Page 140 and 141: SELF-TEST QUESTION 9.4 Discussion 1
- Page 142 and 143: Exercises 119 During the second sta
- Page 144 and 145: CHAPTER 10 This chapter explains:
- Page 146 and 147: In English, this reads: 10.2 A simp
- Page 148 and 149: 10.2 A simple example 125 Now comes
- Page 150 and 151: 10.4 Multiple input and output stre
- Page 152 and 153: Process header Process issue 10.4 M
- Page 154 and 155: 10.5 Structure clashes 131 As seen
- Page 156 and 157: 10.5 Structure clashes 133 Let us r
- Page 158 and 159: 10.6 Discussion 135 ■ teachable -
- Page 160 and 161: Exercises 137 2. a control block, s
- Page 162 and 163: CHAPTER 11 Object-oriented design T
- Page 164 and 165: Figure 11.1 The cyberspace invaders
- Page 166 and 167: SELF-TEST QUESTION 11.1 Derive info
- Page 168 and 169: 11.5 Class-responsibility-collabora
- Page 170 and 171: 11.7 ● Discussion Summary 147 OOD
- Page 172 and 173: 11.11 Compare and contrast the prin
- Page 174 and 175: CHAPTER 12 This chapter explains: 1
- Page 178 and 179: 12.5 Factory method 155 The followi
- Page 180 and 181: 12.8 Model, view controller (observ
- Page 182 and 183: Figure 12.4 Pipe and Filter pattern
- Page 184 and 185: Figure 12.6 Layers in a distributed
- Page 186 and 187: Answers to self-test questions 163
- Page 188 and 189: CHAPTER 13 Refactoring This chapter
- Page 190 and 191: 13.3 ● Move Method 13.6 Inline Cl
- Page 192 and 193: class Sprite Instance variables x y
- Page 194 and 195: Summary Summary 171 it is making po
- Page 196: PART C PROGRAMMING LANGUAGES
- Page 199 and 200: 176 Chapter 14 ■ The basics and a
- Page 201 and 202: 178 Chapter 14 ■ The basics > > >
- Page 203 and 204: 180 Chapter 14 ■ The basics > Ear
- Page 205 and 206: 182 Chapter 14 ■ The basics > Cas
- Page 207 and 208: 184 Chapter 14 ■ The basics > > >
- Page 209 and 210: 186 Chapter 14 ■ The basics > } }
- Page 211 and 212: 188 Chapter 14 ■ The basics Unfor
- Page 213 and 214: 190 Chapter 14 ■ The basics Ada d
- Page 215 and 216: 192 Chapter 14 ■ The basics The w
- Page 217 and 218: 194 Chapter 14 ■ The basics In a
- Page 219 and 220: 196 Chapter 14 ■ The basics > str
- Page 221 and 222: 198 Chapter 14 ■ The basics Answe
- Page 223 and 224: CHAPTER 15 Object-oriented programm
- Page 225 and 226: 202 Chapter 15 ■ Object-oriented
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