Presentation is loading. Please wait.

Presentation is loading. Please wait.

ISP666 MVC & Design Patterns. Outline Review Event Programming Model Model-View-Controller Revisit Simple Calculator Break Design Patterns Exercise.

Similar presentations


Presentation on theme: "ISP666 MVC & Design Patterns. Outline Review Event Programming Model Model-View-Controller Revisit Simple Calculator Break Design Patterns Exercise."— Presentation transcript:

1 ISP666 MVC & Design Patterns

2 Outline Review Event Programming Model Model-View-Controller Revisit Simple Calculator Break Design Patterns Exercise

3 Event-driven programming It gives user full control For application programmer, it is about two things: –Registering event listeners –Writing event-handling code for the listeners Caution –Keep your event-handling code short and run fast Why?

4 How to connect application logic to GUI? Model: information the application is dealing with –e.g. Chips, circuits in case of a ECAD tool –e.g. Student, class data in case of a class registration system View: visual display of the model –Diagrams, images –Tables –Buttons, menus Controller: connecting the two

5 Model-View-Controller (MVC) Model and View must be separated! Same model may have multiple views E.g. A demographic data may be displayed as a table or a graph Same view may be used to deal with multiple Models Code reuse View and controller might be combined

6 MVC a = 50% b = 30% c = 20% model

7 Model Class Keep references to all its views –May not be necessary if all the possible changes of model are from view Notify all its views if its value changed –Could be handled in views and controller if all the change come from view

8 Model Class Keep a reference to its model Notify the model when it is changed by the user action

9 Controller Translate user action event to method invocation on model Its functionality may spread in multiple classes –In Event-handlers

10 Simple Calculator Model: a math expression and a result View: strings in text box, buttons Controller: event handling code for key buttons and text boxes 12*(7.5-1)+5 79 4 0 21 5 3 6 8 / * + - ().= Clear

11 MVC Discussion A well architectured GUI application is said to always separate View and Model Discuss –Will it make the code shorter? –What are the benefits? break

12 Interaction among Objects Can be very complicated –Composite, aggregate, inheritance We also strive to achieve –Reusability of code –Extensibility (flexibility) of our system Ad hoc solutions may not work well

13 Design Patterns Famous book of the Gang of Four (aka GoF book) –Gamma, Helm, Johnson & Vlissides –“Design Patterns – Elements of Reusable Object-Oriented Software”, 1995 Elegant solutions to recurring problems in Object-Oriented software design

14 Type of Patterns Creational Pattern –Object creation Structural Pattern –Composition of classes and objects Behavioral Pattern –Characteristic of object interaction

15 UML Class Diagram Notation

16 Abstract Factory Pattern Application does not need to know which kind of button should be drawn (Motif Button, or Windows Button) It just draw a button, system will draw the current button according to current look-and- feel setting. Creational Change look-and-feel with one line of code, how?

17 Abstract Factory

18 Adaptor Pattern Aka. Wrapper Pattern Let different classes conform to the same interface Structural There's an existing TextView class that you want to add it to your graphic editor program as a Shape object (like LineShape, PolygonShape, etc.), but TextView is not created with Shape interface in mind. What to do?

19 Class Adaptor

20 Object Adaptor

21 Strategy Pattern Aka. Policy Define a family of algorithms, and make them interchangable for clients that use it Several different line-breaking algorithms are available for a editor you are developing, e.g. HtmlLineBreaker, TexLineBreaker, ArrayLineBreaker, etc. Now you don't want rendering code to explicitly ask for an algorithm (for the sake of MVC) but still get an appropriate algorithm given the type of the content. How to achieve that?

22 Strategy

23 Exercise Let's design an arcade game, say, a “bombing the enemy submarines” game. –You control a surface ship to bomb enemy submarines below –Enemy submarines will fire back, there are different types of subs, firing different missiles at you It should be played on the Web, on cell phone, or download to the computer for offline play. More content can be added by user through scripting.


Download ppt "ISP666 MVC & Design Patterns. Outline Review Event Programming Model Model-View-Controller Revisit Simple Calculator Break Design Patterns Exercise."

Similar presentations


Ads by Google