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

13.3 ● Move Method 13.6 Inline Class 167 A method is written within a class, but it uses very little of its current class. Instead it uses many of the facilities of another class. The remedy is to move the method from one class to the other. In the game, the software needs to check whether pairs of items have collided, for example, a laser and an alien. For clarity, this collision detection is carried out by a method. This method is part of both class Laser and class Alien. This creates strong coupling between the objects. Instead move the method to a class that is responsible for checking collisions between all the objects in the game. In general, moving a method may be necessary when: ■ a class has too many methods ■ a class has high coupling with another class. Moving a method is a common refactoring situation. 13.4 ● Move Data A variable is declared within a class, but another class uses it more. The remedy is to move the variable declaration. Clearly this also means moving the get and set methods. 13.5 ● Extract Class A class has become too large and complex; it is doing the work of two classes. The remedy is to create a new class, extracting the appropriate variables and methods from the old class. Ideally classes emerge from design as self-contained components that model some element of the application. In practice, as design proceeds, classes sometimes take on extra roles. Variables and methods are gradually added until the class becomes cumbersome. It becomes a candidate for fission. 13.6 ● Inline Class A class is very small, or it does not do very much. The remedy is to incorporate it into some other class – ideally the class that uses it the most.

168 Chapter 13 ■ Refactoring One way in which a class can become very small is when it has been the subject of the Move Method and the Move Variable refactorings, so that it has become sucked dry. This illustrates how many of the refactorings are interconnected – using one leads to using another, and so on. SELF-TEST QUESTION 13.1 Compare the factoring Inline Class with the factoring Extract Class. 13.7 ● Identify composition or inheritance Once we have identified the classes within a software system, the next step is to review the relationships between the classes. The classes that make up the software collaborate with each other to achieve the required behavior, but they use each other in different ways. There are two ways in which classes relate to each other: 1. composition – one object creates another object from a class using new. An example is a window object that creates a button object. 2. inheritance – one class inherits from another. An example is a class that extends the library Frame class. The important task of design is to distinguish these two cases, so that inheritance can be successfully applied or avoided. One way of checking that we have correctly identified the appropriate relationships between classes is to use the “is-a” or “has-a” test: ■ the use of the phrase “is-a” in the description of an object (or class) signifies that it is probably an inheritance relationship. ■ the use of the phrase “has-a” indicates that there is no inheritance relationship. Instead the relationship is composition. (An alternative phrase that has the same meaning is “consists-of ”.) We return to the cyberspace invaders game, designed in Chapter 11, seeking to find any inheritance relationships. If we can find any such relationships, we can simplify and shorten the program, making good use of reuse. In the game, several of the classes – Defender, Alien, Laser and Bomb – incorporate the same methods. These methods are: getX, getY, getHeight and getWidth that obtain the position and size of the graphical objects. We will remove these ingredients from each class and place them in a superclass. We will name this class Sprite, since the word sprite is a commonly used term for a moving graphical object in games programming. The UML class diagram for the Sprite class is:

168 Chapter 13 ■ Refactoring<br />

One way in which a class can become very small is when it has been the subject of<br />

the Move Method and the Move Variable refactorings, so that it has become sucked<br />

dry. This illustrates how many of the refactorings are interconnected – using one leads<br />

to using another, and so on.<br />

SELF-TEST QUESTION<br />

13.1 Compare the factoring Inline Class with the factoring Extract Class.<br />

13.7 ● Identify composition or inheritance<br />

Once we have identified the classes within a software system, the next step is to review<br />

the relationships between the classes. The classes that make up the software collaborate<br />

with each other to achieve the required behavior, but they use each other in different<br />

ways. There are two ways in which classes relate to each other:<br />

1. composition – one object creates another object from a class using new. An example<br />

is a window object that creates a button object.<br />

2. inheritance – one class inherits from another. An example is a class that extends the<br />

library Frame class.<br />

The important task of design is to distinguish these two cases, so that inheritance<br />

can be successfully applied or avoided. One way of checking that we have correctly<br />

identified the appropriate relationships between classes is to use the “is-a” or “has-a”<br />

test:<br />

■ the use of the phrase “is-a” in the description of an object (or class) signifies that it<br />

is probably an inheritance relationship.<br />

■ the use of the phrase “has-a” indicates that there is no inheritance relationship.<br />

Instead the relationship is composition. (An alternative phrase that has the same<br />

meaning is “consists-of ”.)<br />

We return to the cyberspace invaders game, designed in Chapter 11, seeking to<br />

find any inheritance relationships. If we can find any such relationships, we can simplify<br />

and shorten the program, making good use of reuse. In the game, several of the<br />

classes – Defender, Alien, Laser and Bomb – incorporate the same methods. These<br />

methods are: getX, getY, getHeight and getWidth that obtain the position and<br />

size of the graphical objects. We will remove these ingredients from each class and<br />

place them in a superclass. We will name this class Sprite, since the word sprite is a<br />

commonly used term <strong>for</strong> a moving graphical object in games programming. The<br />

UML class diagram <strong>for</strong> the Sprite class is:

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

Saved successfully!

Ooh no, something went wrong!