21.08.2013 Views

Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

12.8 Model, view controller (observer, observable) 157<br />

methods that provide all the functionality of the system. The detailed structure of the<br />

system is hidden from its users. The façade class knows about the structure of the group<br />

of classes and uses them as necessary. However, the classes in the group do not need to<br />

know about the façade class, so any changes to the group do not impact on the users.<br />

12.7 ● Immutable<br />

An immutable object is one that, once created, does not change its state. In the cyberspace<br />

invaders game, there could be objects that do not move and do not change while<br />

the game is in progress. These objects do not change their state (their internal variables).<br />

We can write an Immutable class by providing methods to access the values of<br />

the object, but none to change them. The variables within an immutable class must not<br />

be declared as constants, because they are changed (once) by the constructor method.<br />

12.8 ● Model, view controller (observer, observable)<br />

Many software systems have at their center a model or a simulation of the application of<br />

interest. The simulation consists of objects and a set of rules governing how they interact.<br />

In the cyberspace invaders game, the model consists of such objects as the alien, lasers and<br />

bombs that move and interact according to certain rules. The model ensures that bombs<br />

moves vertically. If a bomb strikes the defender, the user loses and the game is over.<br />

The core model is surrounded by classes that support a user interface. The user interface<br />

consists of two parts: the elements that control the system (inputs) and the elements<br />

that allow us to view in<strong>for</strong>mation (the outputs). In the game, the user moves the<br />

mouse to move the defender object and can fire a laser by clicking on the mouse button.<br />

These are controls. The screen shows the position of the objects, such as the alien<br />

and a laser. These are outputs (the view).<br />

To summarize, systems often have three components:<br />

■ the model<br />

■ the view<br />

■ the controller.<br />

The MVC pattern recognizes that many systems have this three-part structure –<br />

model, view and controller – and that the software architecture should be explicitly partitioned<br />

in the same way. This has the advantage that the view and/or the control can be<br />

changed without changing the core. For example, the user could control the defender<br />

object with a joystick instead of the mouse, or the display could be sent across the internet<br />

to the screens of multiple players. So, if the system is partitioned, we can easily change<br />

the view or the control or both.<br />

For this pattern to work properly there has to be clear communication between the<br />

three components:<br />

■ when the user alters a control, the model must be told<br />

■ when the model changes, the view must be told.

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

Saved successfully!

Ooh no, something went wrong!