Compound Patterns PDF
Compound Patterns PDF
1
Chapter: Compound Patterns - Model-View-Controller (MVC) Compound
Pattern
• Consider a system composed of
1. A system that represents the main set of methods and objects with which
the user wants to interact (the M odel)
– Holds data, state, application logic
2. A display of some sort (the V iew)
– Provides a representation of the model to the user
3. A component/behavior that allows communication between the V iew and
M odel (the Controller)
• The flow of control is modeled as
1. The user uses the V iew to perform some action on the M odel
2. The action is relayed to the Controller
3. The Controller makes a request to the M odel to perform the action
4. The M odel informs the V iew when its state has changed as a result of
performing the action
• Flow of control:
2
Chapter: Compound Patterns - Model-View-Controller (MVC) Compound
Pattern (2)
• Note that
– The Controller can ask the V iew to update its display
– The V iew can directly query the M odel for its state
• Such a system can be implemented using
1. Strategy Pattern
– In terms of the V iew and Controller
– The Controller represents the behavior of the V iew
2. Observer Pattern
– In terms of the V iew and M odel
– The M odel is the observable, the V iew is the observer
– The Controller can be an observer also
• While could have V iew communicate directly with M odel (i.e., eliminate Controller),
do not want to because
– This would give V iew two responsibilities, violating Single Responsibility
Principle
– Do not want such tight coupling between V iew and M odel
∗ This would preclude using a different M odel
3
Chapter: Compound Patterns - MVC Sample Code: Model
public interface ModelInterface {
... action1(...);
... action2(...);
...
void initialize (...);
...
void registerObserver(ModelObserver o);
void removeObserver(ModelObserver o);
...
}
... action2(...) {
...
}
...
4
Chapter: Compound Patterns - MVC Sample Code: View
public interface ModelObserver {
public void udateState(...);
}
...
}
5
Chapter: Compound Patterns - MVC Sample Code: Controller
public interface ControllerInterface {
void start();
void stop();
... doAction1(...);
... doAction2(...);
... doAction3(...);
...
}
6
Chapter: Compound Patterns - MVC Sample Code: Driver
public class MVCDriver {
public static void main (String[] args) {
ModelInterface model = new Model();
ControllerInterface interface = new Controller(model);
}
}
7
Chapter: Compound Patterns - MVC Model 2
• Model 2 is MVC adapted for use with the Internet
• It implements the client/server model
• Flow of control:
1. Browser makes an HTTP request to a servlet
– Data usually sent as a form
2. Servlet processes request, which usually results in creation of a JavaBean
– JavaBean is sent to the model - usually a database
3. The servlet fowards control to a JSP
– This generates a page that represents the view of the database
– This view is provided by the JavaBean
4. The page is sent to the browser, where it is displayed
8
Chapter: Compound Patterns - MVC m2 Example
• There are three steps needed to implement MVC m2
1. Adapt the M odel for use with MVC m2
– In the standard MVC, both V iew and Controller are transparent from
the M odel
– Consequently, the M odel needs no refactoring
2. Create a servlet
– The servelt receives an HTTP request and translates it into actions to
be applied to the M odel
3. Create a JSP
– The JSP receives a JavaBean from the servlet
– This JavaBean represents the model
– The JSP extracts what it needs
9
Chapter: Compound Patterns - MVC m2 Example: Servlet Code
public class ViewServlet extends HttpServlet {
//doPost method
10
Chapter: Compound Patterns - MVC m2 Example: JSP
<jsp:useBean id="model" scope="request" class="Model"/>
<html>
<head>
<title>Model</title>
</head>
<body>
...
XXX = <jsp:getProperty name="model" property = XXX" /> //Extract property to be displayed
...
</body>
</html>
11
Chapter: Compound Patterns - MVC m2 v Standard MVC
• V iew not explicitly registered as an observer of the M odel
– Is informed of state changes by Controller
– Receives a Bean from which the M odel state can be extracted
– Only needs to be informed when an HTTP response is generated
• Strategy not explicit
– Servlet (Controller) represents the Strategy object
– Not composed with V iew as in standard MVC
– It still represents the behavior of the V iew, and can be swapped out
12