Chapter 3. Model View Controller Pattern


In This Chapter

Understanding MVC Elements 46

Building a Simple Example 48

Enabling Multiple Views for one Model 56

Modifying Model Implementation 57

Adding A Controller 59

Summary 63

There was a time when computer programs had very limited user and client interfaces. In 1975, a computer program might interact with a user through a command line. Programs built today often have rich graphical user interfaces that make use of windows, mouse, and keyboard interaction, and that permit the moving and changing of elements. The increased richness of the user interface presents new challenges, and the Model View Controller (or MVC) pattern addresses those challenges to help create more flexible applications.

Applications consist of user interfaces, business logic, and data models. For example, standard UI components such as lists and combo boxes have user interface elements (clickable regions, scrollable regions, and so on), logic that knows how to respond to user input, and data models (the data that populates the component). Although these are three distinct elements, many developers write code that combines all the elements into one object rather than several objects working in combination. When the interface and the data are collapsed into just one object in rich user interfaces, it can lead to some of the following dilemmas:

  • It is difficult to use the data outside that object. For example, if an object defines a user input form and also saves the user input within the object, then it is difficult to send the data to a server. The options in that case are to place the responsibility of client-server communication in the same class or to define an interface in the object that allows access to the data and provides the only means by which to access the data. Either option creates fragile and rigid structures.

  • You cannot easily change the user interface while keeping the same data. If the user interface and the data are locked in the same object, to use a new user interface you'd have to create not only the new user interface, but also transfer all the data from the old user interface to the new one. Changing the user interface is not an uncommon requirement. Consider an application in which you want to chart a data set using different types of graphs. The data remains the same in each case, but the graph style changes (line, bar, and so on). If each graph is locked up with the data set, it's difficult to change graph styles.

  • Multiple simultaneous views of the same data are difficult. For example, you might want to display two or more graph styles of the same data set at the same time. If the data is locked into the user interface, you have to replicate the data for each chart.

  • Synchronized views are difficult. This is an extension of the previous issue. For example, if you not only want to display two or more different graph styles for the same data set at the same time, but you also want to update those graphs over time as the data changes, then you have to update each data set stored in each user interface.

All the preceding issues are problematic because the data and the user interface are locked into one object. The MVC pattern presents a manner in which you can create two or more objects that work together. This approach enables you to create more flexible applications with more reusable parts.

Note

Some people argue that MVC is not a design pattern, but rather an architectural pattern. Although there may be merit to that argument, we still feel that MVC is an important pattern (design or architectural) and we feel that it is extremely useful to ActionScript developers. For that reason, we present the pattern here in this book.





Advanced ActionScript 3 with Design Patterns
Advanced ActionScript 3 with Design Patterns
ISBN: 0321426568
EAN: 2147483647
Year: 2004
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net