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

This preview shows page 1-2-3-4 out of 12 pages.

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

Unformatted text preview:

MVC: Model, View, ControllerMVC: interfaces and inheritanceDoes SimpleViewer know Model?Stack: What problems does it solve?Simple stack exampleImplementation is very simpleUses rather than "is-a"Postfix, prefix, and infix notationExceptionsJava ExceptionsPrefix notation in actionPostfix notation in actionCompSci 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 views will 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 the implicit/actual stack of functions called recursivelyStacks are used to evaluate arithmetic expressions, to implement 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 many languages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)oShould be implemented using VectoroDoesn'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/ArrayList methods 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.7Uses rather than "is-a"Suppose there's a private ArrayList, myStorageDoesn't extend Vector, simply uses Vector/ArrayListDisadvantages of this approach?oSynchronization issues public 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 respected3 5 + 4 2 * 7 + 3 - 2 9 7 + *Read expressionoFor number/operand: pushoFor 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 Scanner class 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 throws clauseCompSci 100E10.11Prefix notation in actionScheme/LISP and other functional languages tend to use a prefix notation(define (square x) (* x x))(define (expt b n) (if (= n 0) 1 (* b (expt b (- n 1)))))CompSci 100E10.12Postfix notation in actionPractical example of use of stack abstractionPut operator after operands in expressionUse stack to evaluateooperand: push onto stackooperator: 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?