Chapter 3 – Design Patterns: Model-View- Controller
Total Page:16
File Type:pdf, Size:1020Kb
SOFTWARE ARCHITECTURES Chapter 3 – Design Patterns: Model-View- Controller Martin Mugisha Brief History Smalltalk programmers developed the concept of Model-View-Controllers, like most other software engineering concepts. These programmers were gathered at the Learning Research Group (LRG) of Xerox PARC based in Palo Alto, California. This group included Alan Kay, Dan Ingalls and Red Kaehler among others. C language which was developed at Bell Labs was already out there and thus they were a few design standards in place[ 1] . The arrival of Smalltalk would however change all these standards and set the future tone for programming. This language is where the concept of Model-View- Controller first emerged. However, Ted Kaehler is the one most credited for this design pattern. He had a paper in 1978 titled ‘A note on DynaBook requirements’. The first name however for it was not MVC but ‘Thing-Model-View-Set’. The aim of the MVC pattern was to mediate the way the user could interact with the software[ 1] . This pattern has been greatly accredited with the later development of modern Graphical User Interfaces(GUI). Without Kaehler, and his MVC, we would have still been using terminal to input our commands. Introduction Model-View-Controller is an architectural pattern that is used for implementing user interfaces. Software is divided into three inter connected parts. These are the Model, View, and Controller. These inter connection is aimed to separate internal representation of information from the way it is presented to accepted users[ 2] . fig 1 SOFTWARE ARCHITECTURES As shown in fig 1, the MVC has three components that interact to show us our unique information. Component Interaction Below is a detailed description of the interaction of the components in the MVC design pattern: 1. Controller A controller aids in changing the particular state of the model. The controller takes input from the mouse and keyboard inputs from the user and in turn commanding the model and view to change as required. A controller interprets interactions from the view and translates them into actions to be performed by the model. User interactions range from HTTP POST and GET in Web applications or clicks and menu selections in Standalone applications. The controller is also responsible for setting the appropriate view to the appropriate user. 2. Model A model is an object representing date or even an activity. A database table or even some particular plant-floor production machine process. The model manages the behavior and also the data of the software application domain. The model accepts requests for information and responds to the set of instructions meant to change that particular state. The model shows application data and rules that manage access to update this data. The model shows the state and low-level behavior of the component. It controls the state and all its changes 3. View The view is the visual representation of the state of the model. The view renders the contents of a model through accessing the data and specifying how the data should be presented. The view controls the graphical and textual output representations of the software application. A view typically attaches to a model and renders its contents for display. In summary the MVC frame work likes like this: Input- Processing Output ControllerModelView SOFTWARE ARCHITECTURES Implementation of an MVC In the section, I will talk about a few implementations of the MVC design pattern and in particular with Web applications. The MVC pattern has become a popular design pattern with large scale web enterprise application[ 2] . Fig 2[ 2] below shows a sample implementation of MVC design patern. The application is broken down into particular functions, tasks or operations each of which is related to the particular user[ 2] . Each fuction refers to a single controller which may refer to one or more controllers and usually just a single view. Each function deals with an HTTP GET and POST request[ 2] . Fig 2 . The Model This is a business entity which has all the properties and functions required by a single business entity. It is always a subclass of an abstract super class with properties and functions common to all database tables. The table is responsible for an array of responsibility ranging from data validation, business rules to task specific behavior while SOFTWARE ARCHITECTURES actual generation of Data Manipulation Language (DML) statements is handled in a separate class[ 2] . The DML This can also be called the Data Access Object and this is the only object in the framework, which has the permission to communicate with the database. This object can only be called by a model component. This helps in isolating the Model from the underlying database and as such eases the applications ability to be switched from one RDBMS to another simply by switching the DML class[ 2] . The View This an implementation of a series of scripts that are combined with specific output from each database class to produce an XML document in this case. This file will also include data associated with user menus pagination and scrolling. The XML is then transformed into an HTML document by using generic XSL style sheets[ 2] . The Controller The component is implemented as a series of functions which interact with either one or more models[ 2] . Each controller is a class and you can have an array of them interacting with different models. Each of them often deal with the following: Handling HTTP POST and GET request. Instantiates an object for each business entity It calls methods on those appropriate objects and thus dealing with a number of database occurrences both as input and as output. It calls the relevant view object. A good way of understanding what all this means is that in a business of selling shoes for example. There are mangers, sales clerks and the owner. Each of this can send particular requests to the model through the controller and get views that show what is in the database but relevant to them. A manger can have administrative privileges where he can see everyone’s work hours, wages and sales. A sales clerk can only see what shoes are available in the store and sale them but can’t see anyone else wage or work hours. The owner can see all of this information and more like when his supplier is expected to bring in more stock and how much he spends on the stock plus his gross and net profile. Project For a project to do further research into this concept, I chose to create a social media application based on anonymous story telling where stories were tailored for each user based on information the gave us on where they went to school at. I used PHP as the scripting language combined with MySQL database. Overview on PHP PHP is at the forefront of the Web 2.0 boom. Though it’s a relatively young programming language, just over fifteen years, there are millions of developers and SOFTWARE ARCHITECTURES powers over twenty million websites. Its large open source community and also leading players in the IT market like IBM, Oracle and Microsoft endorse PHP[ 3] . The development of PHP started in 1995 by Rasmus Lerdof[ 3] . He created a personal collection of Perl scripts and transferred them into a package written in C. This package came to be known as Personal Home Page or PHP for short[ 3] . This package was later available as PHP/FI. The FI stood for Form Interpreter. It showed a lot of similarities to Perl but yet was much easier to use[ 3] . Two years later, Lerdof released PHP2.0 Fig 3 By 1997, Zeev Suraski and Andi Gutmans had started to rewrite PHP to make the language better suited for ecommerce applications. The worked with Lerdof and changed the meaning of PHP to ‘Hypertext Preprocessor; as it was widely known today. Which resulted into PHP 3.0. By 2000 Suraski and Gutmans had released PHP 4. This feature had simple object oriented and session handling capabilities. At this point, the number of Web applications using PHP had reached 2 million as shown in fig 3[ 3] . The large PHP community at his point in conjunction with Suraski and Gutmans released PHP 5 in 2004[ 3] . This next iteration included a full support for full object orientation, XML integration and the SOAP protocol[ 3] . Below is an example of PHP OOP implementation: class Person { public $firstName; public $lastName; public function __construct($firstName, $lastName = ''){ // optional second argument $this->firstName = $firstName; $this->lastName = $lastName; } SOFTWARE ARCHITECTURES public function greet() { return 'Hello, my name is ' . $this->firstName . ' ' . $this->lastName . '.'; } public static function staticGreet($firstName, $lastName) { return 'Hello, my name is ' . $firstName . ' ' . $lastName . '.'; } } $he = new Person('John', 'Smith'); $she = new Person('Sally', 'Davis'); $other = new Person('iAmine'); echo $he->greet(); // prints "Hello, my name is John Smith." echo '<br />'; echo $she->greet(); // prints "Hello, my name is Sally Davis." echo '<br />'; echo $other->greet(); // prints "Hello, my name is iAmine ." echo '<br />'; echo Person::staticGreet('Jane', 'Doe'); // prints "Hello, my name is Jane Doe." PHP 5.1 came in late 2005 and introduced an abstraction layer called PDO[ 3] . This eased PHP’s use with various databases from different vendors[ 3] . By this point, the number of web 2.0 applications with PHP was reaching 20 million as shown if fig 3. PHP today is a fully comprehensive programming language with solid object orientation support. It has often been referred to as a scripting language but it is more of a dynamic programming language. Unlike the traditional C and Java, PHP doesn’t need to be compiled but rather interpreted at run time.