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

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.

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.

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

Saved successfully!

Ooh no, something went wrong!