Title: Review
1CSC 335 Object-Oriented Programming and Design
Review Strategy Design Pattern MVC
2Pattern Strategy
- Name Strategy (a.k.a Policy)
- Problem You want to encapsulate a family of
algorithms and make them interchangeable.
Strategy lets the the algorithm vary
independently from the clients that use it (GoF) - Solution Create an abstract strategy class (or
interface) and extend (or implement) it in
numerous ways. Each subclass defines the same
method names in different ways
3Pattern Strategy
- Consequences
- Allows families of algorithms. Known uses
- Layout managers in Java
- Different Poker Strategies in 335 Projects
- Different Packman chase strategies 335 Fall 2001
- TextField validators in dBase and Borland OWL
- Will use different algorithms to verify if the
user input is a valid integer, double, string,
date, yes/no. - Eliminates conditional statements
- Don't need to keep asking, if this is supposed to
be an int do this, else if it is a string do that
4Java Example of Strategy
- this.setLayout( new FlowLayout( ) )
- this.setLayout( new GridLayout( ) )
- In Java, a container contains a layout manager
- There is a default perhaps set in the
constructor - You can change a container's layout manager with
a setLayout message - Could use same approach in Ghosts
- Ghosts have common methods but could change an
instance variable at runtime with - setStrategy( new RandomStategy( ) )
5interface LayoutManager
- Java has a java.awt.LayoutManager interface
- Known Implementing Classes
- GridLayout, FlowLayout, ScrollPaneLayout
- Each class implements the following methods
- addLayoutComponent(String name, Component comp)
- layoutContainer(Container parent)
- minimumLayoutSize(Container parent)
- preferredLayoutSize(Container parent)
- removeLayoutComponent(Component comp)
6General form of Strategy in UML (abstract)
ltltinterfacegtgt Strategy AlgorithmInterface
Context strategy Strategy ContextInterface
implements
ConcreteClassA AlgorithmInterface
ConcreteClassB AlgorithmInterface
ConcreteClassC AlgorithmInterface
7Specific UML Diagram of LayoutManager
ltltinterfacegtgt LayoutManager addLayoutComponent() l
ayoutContainer() minimumLayoutSize()
JPanel panel Panel size Dimension layoutBoss
LayoutManager setLayout(lm LayoutManager) setPref
erredSize(diDimension)
implements
GridLayout addLayoutComponent() layoutContainer()
minimumLayoutSize()
FlowLayout addLayoutComponent() layoutContainer()
minimumLayoutSize()
ScrollPaneLayout addLayoutComponent() layoutContai
ner() minimumLayoutSize()
8Change the strategy at runtime
- Demonstrate LayoutControllerFrame.java
- Source code online and attached
- private class FlowListener implements
ActionListener - // There is another ActionListener for
GridLayout - public void actionPerformed( ActionEvent evt )
- // Change the layout strategy of the JPanel
- // and tell it to lay itself out
- centerPanel.setLayout( new FlowLayout( ) )
- centerPanel.validate( )
-
-
9Another Example
10Another Example
11Another Example
- OozinOz has strategies to decide which Fireworks
to recommend to customers - See Pages 237..247
12Observer Design Pattern
- Observer Define a 1 to many relationship so when
one object changes state, all dependents are
notified and they update the view themselves. - You will be able to switch views at runtime or to
show 2 or more views at the same time - Java supports this with Observer/Observable
- BattleBoat and Poker must use this pattern
- MUD could
13OOP and UI
- Decoupled UIs are good
- One model may have several user interfaces
- Web, phone, PDA, GUI on a desktop
- Each UI will hook into the same model
- But allow IO in different ways
- Desired change UI without changing the model
- Desired change the model without changing the UI
- Do not imbed the UI code inside the model
- Keep the two separate Model-View Separation
14MVC
- Model-View-Controller (MVC) you have used this in
you event-driven programs with a GUI and in your
final project - Model Represents system with the business logic
- View Displays the model
- Controllers Handle user interaction and sends
correct messages to the model and/or view
15Model
- The Model's responsibilities
- Provide access to the state of the system
- Provide access to the system's functionality
- Can notify the view(s) that its state has changed
16Controller
- The controller's responsibilities
- Accept user input
- Button clicks, key presses, mouse movements,
slider bar changes - Send messages to the model, which may in turn
notify it observers - Send appropriate messages to the view
- In Java, listeners are controllers
17View
- The view's responsibilities
- Display the state of the model to the user
- At some point, the model (a.k.a. the observable)
must registers the views (a.k.a. observers) so
the model can notify the observers that its state
has changed
18from http//www.enode.com/x/markup/tutorial/mvc.ht
ml)
19State Pattern