DOC PREVIEW
Duke CPS 100E - Model, View, Controller

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CompSci 100E10.1MVC: Model, View, Controllerÿ A model is the state and brains of a systemþ In a game it's all the pieces and where they areþ In a spreadsheet it's the data and the formulaeÿ The view is how we look at the modelþ Spread sheet has graphs, charts, cells, text, …þ Game has board, number of opponents, hit-points, …ÿ When the model changes, the views reflect the changesþ The model tells the views how/if it has changedþ Model sends information to views ORþ View asks model for informationCompSci 100E10.2MVC: interfaces and inheritanceÿ A model might have multiple viewsþ Tell all the views "I've changed"þ Who manages the views? This requires state: store viewsþ Why can't we keep this state in an interface?ÿ See IModel and AbstractModelþ One specifies behavior, the other provides defaultþ Don’t rewrite code if we don't have to, maintaining viewswill be the same for all modelsÿ See IView and SimpleViewþ No default/shared view state/behavior: text and GUICompSci 100E10.3Does SimpleViewer know Model?ÿ What does the SimpleViewer know about its model?þ If we look at code, is there any application-specific logic?þ What if we wanted to play a game, start a new game?ÿ Control in MVC with SimpleViewer and IModelþ Loading a file calls initialize()þ Entering text calls process()þ Model calls view with messages, errors, and complete updateÿ This isn't complete general, but it's pretty genericþ For this input, here's the outputÿ Note Java API’s Observer interface and Observable classCompSci 100E10.4Stack: What problems does it solve?ÿ Stacks are used to avoid recursion, a stack can replace theimplicit/actual stack of functions called recursivelyÿ Stacks are used to evaluate arithmetic expressions, toimplement compilers, to implement interpretersþ The Java Virtual Machine (JVM) is a stack-based machineþ Postscript is a stack-based languageþ Stacks are used to evaluate arithmetic expressions in manylanguagesÿ Small set of operations: LIFO or last in is first out accessþ Operations: push, pop, top, create, clear, sizeþ More in postscript, e.g., swap, dup, rotate, …CompSci 100E10.5Simple stack exampleÿ Stack is part of java.util.Collections hierarchyþ It's an OO abomination, extends Vector (like ArrayList)o Should be implemented using Vectoro Doesn't model "is-a" inheritanceþ What does pop do? What does push do?Stack s = new Stack();s.push("panda");s.push("grizzly");s.push("brown");System.out.println("size = " + s.size());System.out.println(s.peek());Object o = s.pop();System.out.println(s.peek());System.out.println(s.pop());CompSci 100E10.6Implementation is very simpleÿ Extends Vector, so simply wraps Vector/ArrayListmethods in better namesþ push==add, pop==removeþ Note: code below for ArrayList, Vector is actually used.public Object push(Object o){add(o);return o;}public Object pop(Object o){return remove(size()-1);}CompSci 100E10.7Usesrather than "is-a"ÿ Suppose there's a private ArrayList, myStorageþ Doesn't extend Vector, simply uses Vector/ArrayListþ Disadvantages of this approach?o Synchronization issuespublic Object push(Object o){myStorage.add(o);return o;}public Object pop(Object o){return myStorage.remove(size()-1);}CompSci 100E10.8Postfix, prefix, and infix notationÿ Postfix notation used in some HP calculatorsþ No parentheses needed, precedence rules still respected35+ 42*7+3- 297+*þ Read expressiono For number/operand: pusho For operator: pop, pop, operate, pushÿ See Postfix.java for example code, key ideas:þ Use StringTokenizer, handy tool for parsingþ Note: Exceptions thrown, what are these?ÿ What about prefix and infix notations, advantages?CompSci 100E10.9Exceptionsÿ Exceptions are raised or thrown in exceptional casesþ Bad indexes, null pointers, illegal arguments, …þ File not found, URL malformed, …ÿ Runtime exceptions aren't meant to be handled or caughtþ Bad index in array, don't try to handle this in codeþ Null pointer stops your program, don't code that way!ÿ Other exceptions must be caught or rethrownþ See FileNotFoundException and IOException in Scannerclass implementationÿ RuntimeException extends Exception, catch not requiredCompSci 100E10.10Java Exceptionsÿ Many I/O operations can throw Exceptionsþ Code handles it for yourþ However, need to know what is going onþ (Review pages in Chapter 2)ÿ Catching Exceptionsþ Use try-catch blocktry {// statements that might generate exception}catch (Exception_type var){// code that deals with exception}ÿ Method can pass on responsibility for exception with throwsclauseCompSci 100E10.11Prefix notation in actionÿ Scheme/LISP and other functional languages tendto use a prefix notation(define (square x) (* x x))(define (expt b n)(if (= n 0)1(* b (expt b (- n 1)))))CompSci 100E10.12Postfixnotationinactionÿ Practical example of use of stack abstractionÿ Put operator after operands in expressionþ Use stack to evaluateo operand: push onto stacko operator: pop operands push resultÿ PostScript is a stack language mostly used for printingþ drawing an “X” with two equivalent sets of code%!200 200 moveto100 100 rlineto200 300 moveto100 –100 rlinetostroke showpage%!100 –100 200 300 100 100 200 200moveto rlineto moveto rlinetostroke


View Full Document

Duke CPS 100E - Model, View, Controller

Documents in this Course
Topics

Topics

9 pages

Lecture

Lecture

3 pages

Notes

Notes

2 pages

Hashing

Hashing

19 pages

Lecture

Lecture

59 pages

Lecture

Lecture

6 pages

Lecture

Lecture

4 pages

Lecture

Lecture

20 pages

Lecture

Lecture

12 pages

Lecture

Lecture

12 pages

Lecture

Lecture

7 pages

Lecture

Lecture

8 pages

Lecture

Lecture

10 pages

Lecture

Lecture

4 pages

Notes

Notes

16 pages

Lecture

Lecture

5 pages

Lecture

Lecture

9 pages

Lecture

Lecture

4 pages

Lecture

Lecture

13 pages

Lecture

Lecture

6 pages

Lecture

Lecture

16 pages

Lecture

Lecture

5 pages

Lecture

Lecture

5 pages

Lecture

Lecture

12 pages

Lecture

Lecture

12 pages

Lecture

Lecture

10 pages

Sets

Sets

14 pages

Lecture

Lecture

9 pages

Lecture

Lecture

4 pages

Test 1

Test 1

7 pages

Load more
Download Model, View, Controller
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Model, View, Controller and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Model, View, Controller 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?