Software Engineering for Students A Programming Approach
Software Engineering for Students A Programming Approach Software Engineering for Students A Programming Approach
Summary Summary 171 it is making poor use of polymorphism. The purpose of the if statements may be to distinguish the different classes and thereby take appropriate action. But it may be simpler to refactor the class, eliminate the if statements and exploit polymorphism. In the game program, we identified the commonalities in a number of classes – Alien, Defender, Bomb and Laser. We placed the common factors in a superclass called Sprite. Now we can treat all the objects uniformly. We place the game objects in an array list named game and write the following code to display them: for (int s = 0; s < game.size(); s++) { Object item = game.get(s); Sprite sprite = (Sprite) item; sprite.display(paper); } which is much neater than a whole series of if statements. 13.9 ● Discussion The idea of taking a design and changing it can be a surprise. It may seem akin to creating an ad hoc design and then experimenting with it. It has the flavor of hacking. Some people argue that a good design method should produce a good design – that it should not need improvement. Equally, many developers are reluctant to tinker with an architectural design that has been created according to sound principles. However, refactoring has a respectable pedigree. It recognizes that a perfect initial design is unlikely and it offers a number of possible strategies for improving a structure. A refactoring such as Extract Method gives the developer the green light to modify an initial design. Note that refactoring implies that iteration is commonly used during OOD. Refactoring means improving the architectural structure of a piece of software. This can be done at the end of design or during design. A number of useful refactorings have been identified, given names and cataloged. The refactorings described in this chapter are: ■ Encapsulate Data ■ Move Method ■ Extract Class ■ Inline Class ■ identify composition or inheritance ■ use polymorphism.
172 Chapter 13 ■ Refactoring Exercises •13.1 In the cyberspace invaders game, we have already carried out a refactoring, identifying a superclass Sprite and applying inheritance. Some of the graphical objects in the game move vertically (bombs, lasers) while some move horizontally (alien, defender). Consider new superclasses MovesVertically and MovesHorizontally and draw the class diagrams for this new inheritance structure. Assess whether this refactoring is useful. 13.2 At what stage do you stop the process of refactoring? 13.3 Examine your architectural designs for the software case studies (Appendix A) and see if refactoring is achievable and desirable. Answers to self-test questions 13.1 Inline Class is the opposite of the Extract Class. 13.2 1. a house has-a roof and a door. A house is-a dwelling 2. a man (and a woman) is-a person 3. an engine has-a piston and a gearbox and an engine 4. a car and a bus is-a vehicle.
- 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 176 and 177: 12.3 Delegation 153 The concepts of
- 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 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
- Page 227 and 228: 204 Chapter 15 ■ Object-oriented
- Page 229 and 230: 206 Chapter 15 ■ Object-oriented
- Page 231 and 232: 208 Chapter 15 ■ Object-oriented
- Page 233 and 234: 210 Chapter 15 ■ Object-oriented
- Page 235 and 236: 212 Chapter 15 ■ Object-oriented
- Page 237 and 238: 214 Chapter 15 ■ Object-oriented
- Page 239 and 240: 216 Chapter 15 ■ Object-oriented
- Page 241 and 242: 218 Chapter 15 ■ Object-oriented
- Page 243 and 244: 220 Chapter 15 ■ Object-oriented
172 Chapter 13 ■ Refactoring<br />
Exercises<br />
•13.1 In the cyberspace invaders game, we have already carried out a refactoring, identifying<br />
a superclass Sprite and applying inheritance. Some of the graphical objects in the<br />
game move vertically (bombs, lasers) while some move horizontally (alien, defender).<br />
Consider new superclasses MovesVertically and MovesHorizontally and<br />
draw the class diagrams <strong>for</strong> this new inheritance structure. Assess whether this refactoring<br />
is useful.<br />
13.2 At what stage do you stop the process of refactoring?<br />
13.3 Examine your architectural designs <strong>for</strong> the software case studies (Appendix A) and<br />
see if refactoring is achievable and desirable.<br />
Answers to self-test questions<br />
13.1 Inline Class is the opposite of the Extract Class.<br />
13.2 1. a house has-a roof and a door. A house is-a dwelling<br />
2. a man (and a woman) is-a person<br />
3. an engine has-a piston and a gearbox and an engine<br />
4. a car and a bus is-a vehicle.