• Tidak ada hasil yang ditemukan

ITSJUST AVIEW 157

Related sections include:

Design by Contract, page 109

Programming by Coincidence, page 172

Challenges

How many tasks do you perform in parallel when you get ready for work in the morning? Could you express this in a UML activity diagram? Can you find some way to get ready more quickly by increasing concurrency?

the receiver. In fact, there could be multiple receivers, each one focused on its own agenda (of which the sender is blissfully unaware).

We need to exercise some care in using events, however. In an early ver- sion of Java, for example, one routine receivedall the events destined for a particular application. Not exactly the road to easy maintenance or evolution.

Publish/Subscribe

Why is it bad to push all the events through a single routine? It vio- lates object encapsulation—that one routine now has to have intimate knowledge of the interactions among many objects. It also increases the coupling—and we’re trying to decrease coupling. Because the objects themselves have to have knowledge of these events as well, you are probably going to violate theDRY principle, orthogonality, and perhaps even sections of the Geneva Convention. You may have seen this kind of code—it is usually dominated by a hugecasestatement or multiway

if-then. We can do better.

Objects should be able to register to receive only the events they need, and should never be sent events they don’t need. We don’t want to spam our objects! Instead, we can use a publish/subscribe protocol, illustrated using theUML sequence diagram in Figure 5.4 on the next page.7

A sequence diagram shows the flow of messages among several objects, with objects arranged in columns. Each message is shown as a labeled arrow from the sender’s column to the receiver’s column. An asterisk in the label means that more than one message of this type can be sent.

If we are interested in certain events generated by aPublisher, all we have to do is register ourselves. ThePublisher keeps track of all in- terestedSubscriberobjects; when thePublishergenerates an event of interest, it will call each Subscriberin turn and notify them that the event has occurred.

7. See also the Observer pattern in [GHJV95] for more information.

ITSJUST AVIEW 159

Figure 5.4. Publish/subscribe protocol

Subscriber one

Subscriber

two Publisher

register notify*

register notify*

notify*

unsubscribe

notify*

There are several variations on this theme—mirroring other commu- nication styles. Objects may use publish/subscribe on a peer-to-peer basis (as we saw above); they may use a “software bus” where a centralized object maintains the database of listeners and dispatches messages appropriately. You might even have a scheme where critical events get broadcast to all listeners—registered or not. One possible implementation of events in a distributed environment is illustrated by theCORBA Event Service, described in the box on the following page.

We can use this publish/subscribe mechanism to implement a very important design concept: the separation of a model from views of the model. Let’s start with aGUI-based example, using the Smalltalk design in which this concept was born.

Model-View-Controller

Suppose you have a spreadsheet application. In addition to the num- bers in the spreadsheet itself, you also have a graph that displays the

TheCORBAEvent Service

TheCORBA Event Service allows participating objects to send and receive event notifications via a common bus, theevent channel. The event channel arbitrates event handling, and also decouples event producers from event consumers. It works in two basic ways: push andpull.

In push mode, event suppliers inform the event channel that an event has occurred. The channel then automatically distributes that event to all client objects that have registered interest.

In pull mode, clients periodically poll the event channel, which in turn polls the supplier that offers event data corresponding to the request.

Although theCORBAEvent Service can be used to implement all of the event models discussed in this section, you can also view it as a different animal. CORBAfacilitates communication among objects written in different programming languages running on geographi- cally dispersed machines with different architectures. Sitting on top ofCORBA, the event service gives you a decoupled way of interact- ing with applications around the world, written by people you’ve never met, using programming languages you’d rather not know about.

numbers as a bar chart and a running total dialog box that shows the sum of a column in the spreadsheet.

Obviously, we don’t want to have three separate copies of the data. So we create amodel—the data itself, with common operations to manip- ulate it. Then we can create separate views that display the data in different ways: as a spreadsheet, as a graph, or in a totals box. Each of these views may have its owncontroller. The graph view may have a controller that allows you to zoom in or out, or pan around the data, for example. None of this affects the data itself, just that view.

This is the key concept behind the Model-View-Controller (MVC) idiom:

separating the model from both theGUIthat represents itandthe con- trols that manage the view.8

8. The view and controller are tightly coupled, and in some implementations of MVC the view and controller are a single component.

ITSJUST AVIEW 161

By doing so, you can take advantage of some interesting possibilities.

You can support multiple views of the same data model. You can use common viewers on many different data models. You can even support multiple controllers to provide nontraditional input mechanisms.

TIP42

Separate Views from Models

By loosening the coupling between the model and the view/controller, you buy yourself a lot of flexibility at low cost. In fact, this technique is one of the most important ways of maintaining reversibility (see Reversibility, page 44).

Java Tree View

A good example of an MVC design can be found in the Java tree widget.

The tree widget (which displays a clickable, traversable tree) is actually a set of several different classes organized in an MVC pattern.

To produce a fully functional tree widget, all you need to do is provide a data source that conforms to the TreeModelinterface. Your code now becomes the model for the tree.

The view is created by the TreeCellRenderer and TreeCellEditor

classes, which can be inherited from and customized to provide differ- ent colors, fonts, and icons in the widget.JTreeacts as the controller for the tree widget and provides some general viewing functionality.

Because we have decoupled the model from the view, we simplify the programming a great deal. You don’t have to think about programming a tree widget anymore. Instead, you just provide a data source.

Suppose the vice president comes up to you and wants a quick appli- cation that lets her navigate the company’s organizational chart, which is held in a legacy database on the mainframe. Just write a wrapper that takes the mainframe data, presents it as a TreeModel, andvoilà:

you have a fully navigable tree widget.

Now you can get fancy and start using the viewer classes; you can change how nodes are rendered, and use special icons, fonts, or colors.

When the VP comes back and says the new corporate standards dictate

the use of a Skull and Crossbones icon for certain employees, you can make the changes toTreeCellRenderer without touching any other code.

Beyond GUIs

While MVC is typically taught in the context of GUI development, it is really a general-purpose programming technique. The view is an inter- pretation of the model (perhaps a subset)—it doesn’t need to be graph- ical. The controller is more of a coordination mechanism, and doesn’t have to be related to any sort of input device.

Model. The abstract data model representing the target object.

The model has no direct knowledge of any views or controllers.

View. A way to interpret the model. It subscribes to changes in the model and logical events from the controller.

Controller. A way to control the view and provide the model with new data. It publishes events to both the model and the view.

Let’s look at a nongraphical example.

Baseball is a unique institution. Where else can you learn such gems of trivia as “this has become the highest-scoring game played on a Tues- day, in the rain, under artificial lights, between teams whose names start with a vowel?” Suppose we were charged with developing software to support those intrepid announcers who must dutifully report on the scores, the statistics, and the trivia.

Clearly we need information on the game in progress—the teams play- ing, the conditions, the player at bat, the score, and so on. These facts form our models; they will be updated as new information arrives (a pitcher is changed, a player strikes out, it starts raining ).

We’ll then have a number of view objects that use these models. One view might look for runs so it can update the current score. Another may receive notifications of new batters, and retrieve a brief summary of their year-to-date statistics. A third viewer may look at the data and check for new world records. We might even have a trivia viewer, responsible for coming up with those weird and useless facts that thrill the viewing public.

ITSJUST AVIEW 163

Figure 5.5. Baseball reporting. Viewerssubscribe to models.

Scores

Conditions

Score collector

Batter stats

Records

Trivia

Display filter

TV feed generator

Web page formatter

Tele- prompter

model subscribes to viewer

But we don’t want to flood the poor announcer with all of these views directly. Instead, we’ll have each view generate notifications of “inter- esting” events, and let some higher-level object schedule what gets shown.9

These viewer objects have suddenly become models for the higher-level object, which itself might then be a model for different formatting view- ers. One formatting viewer might create the teleprompter script for the announcer, another might generate video captions directly on the satel- lite uplink, another might update the network’s or team’s Web pages (see Figure 5.5).

This kind of model-viewer network is a common (and valuable) design technique. Each link decouples raw data from the events that created it—each new viewer is an abstraction. And because the relationships are a network (not just a linear chain), we have a lot of flexibility. Each

9. The fact that a plane flies overhead probably isn’t interesting unless it’s the 100th plane to fly overhead that night.

model may havemanyviewers, and one viewer may work with multiple models.

In advanced systems such as this one, it can be handy to have de- bugging views—specialized views that show you in-depth details of the model. Adding a facility to trace individual events can be a great time saver as well.

Still Coupled (After All These Years)

Despite the decrease in coupling we have achieved, listeners and event generators (subscribers and publishers) still have some knowledge of each other. In Java, for instance, they must agree on common interface definitions and calling conventions.

In the next section, we’ll look at ways of reducing coupling even further by using a form of publish and subscribe wherenoneof the participants need know about each other, or call each other directly.

Related sections include:

Orthogonality, page 34 Reversibility, page 44

Decoupling and the Law of Demeter, page 138 Blackboards, page 165

It’s All Writing, page 248

Exercises

29. Suppose you have an airline reservation system that includes the concept Answer

on p. 296 of a flight:

public interface Flight {

// Return false if flight full.

public boolean addPassenger(Passenger p);

public void addToWaitList(Passenger p);

public int getFlightCapacity();

public int getNumPassengers();

}

If you add a passenger to the wait list, they’ll be put on the flight automat- ically when an opening becomes available.

BLACKBOARDS 165

There’s a massive reporting job that goes through looking for overbooked or full flights to suggest when additional flights might be scheduled. It works fine, but it takes hours to run.

We’d like to have a little more flexibility in processing wait-list passengers, and we’ve got to do something about that big report—it takes too long to run. Use the ideas from this section to redesign this interface.