[go: up one dir, main page]

0% found this document useful (0 votes)
135 views12 pages

Compound Patterns PDF

The document discusses the Model-View-Controller (MVC) compound pattern. It describes MVC as a set of three components - the Model, View, and Controller. The Model handles the data and logic, the View provides a representation of the Model to the user, and the Controller allows communication between the View and Model. It then provides examples of how MVC can be implemented using other design patterns like Strategy and Observer. It also discusses Model 2, an adaptation of MVC for web applications, and provides code samples of how a servlet and JSP would be used to implement this variation.

Uploaded by

Willy Wonka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views12 pages

Compound Patterns PDF

The document discusses the Model-View-Controller (MVC) compound pattern. It describes MVC as a set of three components - the Model, View, and Controller. The Model handles the data and logic, the View provides a representation of the Model to the user, and the Controller allows communication between the View and Model. It then provides examples of how MVC can be implemented using other design patterns like Strategy and Observer. It also discusses Model 2, an adaptation of MVC for web applications, and provides code samples of how a servlet and JSP would be used to implement this variation.

Uploaded by

Willy Wonka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Chapter: Compound Patterns - Introduction

• Text initial example


– Iteratively develops an implementation for the duck class
– By end of development, have a duck interface, can count how many times
ducks quack, can manage flocks of ducks.
– Patterns incorporated:
1. Adapter, so can deal with geese too.
2. Decorator, to count number of quacks.
3. Abstract factory, to insure all ducks are decorated.
4. Composite, to manage flocks.
5. Observer, so bird watchers can monitor individual birds.
• Example demonstrates
– How a number of patterns can be incorporated into a single implementation
– That is OK to vary from a strict pattern definition in order to fit a particular
problem
• The above example is not a compound pattern!
– Rather, it is just a set of patterns working together
• Compound Pattern defined:
A set of patterns working together that can be applied to many problems.
• A compound pattern is a general solution to a problem
• Note that the definition is the same as that for Design Pattern, but applied to
several patterns working together

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);
...
}

public class Model implements ModelInterface {


ArrayList modelObservers = new ArrayList();
State curState; //abstraction of what View is interested in
...

... action1(...) { //Methods that implement Model logic


...
}

... action2(...) {
...
}
...

void initialize (...) {


//initialize model
}

void registerObserver (ModelObserver m) { //Observer implementation


modelObservers.add(m);
}

void removeObserver (ModelObserver m) {


int i = modelObservers.indexOf(m);
if (i >= 0)
modelObservers.remove(i);
}

public void notifyObservers() {


for (int i = 0; i < observers.size(); i++) {
Observer observer = (Observer)modelObservers.get(i);
observer.update(curState);
}
}

public void measurementsChanged () {


notifyObservers();
}

public void setState(...) {


...
measurementsChanged();
}
}

4
Chapter: Compound Patterns - MVC Sample Code: View
public interface ModelObserver {
public void udateState(...);
}

public class View implements ModelObserver {


ModelInterface model;
ControllerInterface controller;
State modelState;
...

public View (ControllerInterface controller, ModelInterface model) {


this .controller = controller;
this.model = model;
model.registerObserver (this);
...
}

public void updateDisplay (...) { //Display state of Model


//create/update user interface
}

public void createControls (...) { //Display control devices


//create/update controls accessible to user
}

//various enabling and disabling methods for controls

public void performAction (...) { //Relays requests to controller


if //action1()
controller.doAction1(...);
else if // action2()
controller.doAction2(...);
...
}

public void update (State state) { //State pushed by Model


modelState = state;
...
processPushedState();
}

public ... processpushedState() {


//process model state //Do whatever processing
updateDisplay(); //Update display for user
}

...
}

5
Chapter: Compound Patterns - MVC Sample Code: Controller
public interface ControllerInterface {
void start();
void stop();
... doAction1(...);
... doAction2(...);
... doAction3(...);
...
}

public class Controller implements ControllerInterface {


Model model;
View view;
...

public Controller (ModelInterface model) {


this.model = model;
view = new View(this, model);
view.updateDisplay();
view.createControls();
...
model.initiallize(...);
}

public void start () {


...
}

public void stop () {


...
}

public ... doAction1 (...) {


...
model.action1(...);
}

public ... doAction2 (...) {


...
model.action2(...);
}

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

• Comparing this to the MVC Pattern


1. The servlet is the Controller
2. The database is the M odel (nothing really different here)
3. The JSP is the V iew

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 {

public void init () throws ServletException {


Model model = new Model();
model.initialize();
getServletContext().setAttribute("model", model);
}

//doPost method

public void doGet (HttpServletRequest request, HttpServletResponse response)


throws IOException, ServletException
{
Model model = (Model) getServletContext().getAttribute("model");

... param = request.getParameter("XXX"); // See if there is a value for this tag


if (param != null) {
model.XXXmethod(...); // Call model method assoc’d with this tag
}

... //Other checks for tags

request.setAttribute("model", model); //Send JSP a bean with Model in it

RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/View.jsp");


dispatcher.forward(request, response);
}
}

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
...

<form method="post" action="model/servlet/Model">

<input type="submit" name="XXX" value="YYY"> // Actions available to user


...
</form>

</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

You might also like