Review - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Review

Description:

Problem: You want to encapsulate a family of algorithms and make them interchangeable. ... to switch views at runtime or to show 2 or more views at the same ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 20
Provided by: rickm1
Category:
Tags: firework | make | online | review | show

less

Transcript and Presenter's Notes

Title: Review


1
CSC 335 Object-Oriented Programming and Design
Review Strategy Design Pattern MVC
2
Pattern 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

3
Pattern 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

4
Java 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( ) )

5
interface 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)

6
General form of Strategy in UML (abstract)
ltltinterfacegtgt Strategy AlgorithmInterface
Context strategy Strategy ContextInterface
implements
ConcreteClassA AlgorithmInterface
ConcreteClassB AlgorithmInterface
ConcreteClassC AlgorithmInterface
7
Specific 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()
8
Change 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( )

9
Another Example
10
Another Example
11
Another Example
  • OozinOz has strategies to decide which Fireworks
    to recommend to customers
  • See Pages 237..247

12
Observer 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

13
OOP 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

14
MVC
  • 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

15
Model
  • 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

16
Controller
  • 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

17
View
  • 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

18
from http//www.enode.com/x/markup/tutorial/mvc.ht
ml)
19
State Pattern
Write a Comment
User Comments (0)
About PowerShow.com