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, ControllerA model is the state and brains of a systemIn a game it's all the pieces and where they areIn a spreadsheet it's the data and the formulaeThe view is how we look at the modelSpread sheet has graphs, charts, cells, text, …Game has board, number of opponents, hit-points, …When the model changes, the views reflect the changesThe model tells the views how/if it has changedModel sends information to views ORView asks model for informationCompSci 100E10.2MVC: interfaces and inheritanceA model might have multiple viewsTell all the views "I've changed"Who manages the views? This requires state: store viewsWhy can't we keep this state in an interface?See IModel and AbstractModelOne specifies behavior, the other provides default Don’t rewrite code if we don't have to, maintaining views will be the same for all modelsSee IView and SimpleViewNo 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 IModelLoading a file calls initialize()Entering text calls process()Model calls view with messages, errors, and complete updateThis isn't complete general, but it's pretty genericFor this input, here's the outputNote 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 recursivelyStacks are used to evaluate arithmetic expressions, to implement compilers, to implement interpretersThe Java Virtual Machine (JVM) is a stack-based machinePostscript is a stack-based languageStacks are used to evaluate arithmetic expressions in many languagesSmall set of operations: LIFO or last in is first out accessOperations: push, pop, top, create, clear, sizeMore in postscript, e.g., swap, dup, rotate, …CompSci 100E10.5Simple stack exampleStack is part of java.util.Collections hierarchyIt's an OO abomination, extends Vector (like ArrayList)oShould be implemented using VectoroDoesn't model "is-a" inheritanceWhat 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 simpleExtends Vector, so simply wraps Vector/ArrayList methods in better namespush==add, pop==removeNote: 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, myStorageDoesn't extend Vector, simply uses Vector/ArrayListDisadvantages 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 notationPostfix notation used in some HP calculatorsNo parentheses needed, precedence rules still respected3 5 + 4 2 * 7 + 3 - 2 9 7 + *Read expressionoFor number/operand: pushoFor operator: pop, pop, operate, pushSee Postfix.java for example code, key ideas:Use StringTokenizer, handy tool for parsingNote: Exceptions thrown, what are these? What about prefix and infix notations, advantages?CompSci 100E10.9ExceptionsExceptions are raised or thrown in exceptional casesBad indexes, null pointers, illegal arguments, …File not found, URL malformed, …Runtime exceptions aren't meant to be handled or caughtBad index in array, don't try to handle this in codeNull pointer stops your program, don't code that way!Other exceptions must be caught or rethrownSee FileNotFoundException and IOException in Scanner class implementationRuntimeException extends Exception, catch not requiredCompSci 100E10.10Java ExceptionsMany I/O operations can throw ExceptionsCode handles it for yourHowever, need to know what is going on(Review pages in Chapter 2)Catching ExceptionsUse 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 actionScheme/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 actionPractical example of use of stack abstractionPut operator after operands in expressionUse stack to evaluateooperand: push onto stackooperator: pop operands push resultPostScript is a stack language mostly used for printingdrawing 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