software engineering i 02161
play

Software Engineering I (02161) Week 7 Assoc. Prof. Hubert - PowerPoint PPT Presentation

Software Engineering I (02161) Week 7 Assoc. Prof. Hubert Baumeister DTU Compute Technical University of Denmark Spring 2016 Contents State machines Library Application and GUI Layered Architecture: Persistence Layer Example Vending


  1. Software Engineering I (02161) Week 7 Assoc. Prof. Hubert Baumeister DTU Compute Technical University of Denmark Spring 2016

  2. Contents State machines Library Application and GUI Layered Architecture: Persistence Layer

  3. Example Vending Machine ◮ Events ◮ Input coins ◮ Press button for bananas or apples ◮ Press cancel ◮ Displays ◮ current amount of money input ◮ Effects (Actions the machine performs) ◮ Return money ◮ Dispense banana or apple

  4. State transition table event state state Fruit f selected and not enough money Idle (I) (F(f)) guard action guard action dispense f and rest dispense f and rest enough money for f enough money for f money → I money → I not enough money not enough money Select fruit f → F(f) → F ( f ) for f for f no fruits of type f no fruits of type f return money → I return money → I available available enough money for dispense f and rest fruit f money → I add money to current Input money money → I not enough money add money to current for f money →F(f) cancel return money → I return money → I ◮ Easy to check for completeness: Does every state implement a reaction to every event? ◮ Easy to describe behavior: finite number of events and states → Good for this type of situations. For example, embedded systems

  5. UML State Machines

  6. Example: Safe ◮ Task: Implement a control panel for a safe in a dungeon ◮ The lock should be visible only when a candle has been removed ◮ The safe door opens only when the key is turned after the candle has been replaced again ◮ If the key is turned without replacing the candle, a killer rabbit is released SecurePanelController <<enumeration>> State candleRemoved keyTurned currentState wait open safeClosed 1 openSafe lock finalState revealLock releaseKillerRabbit

  7. Example: Safe

  8. Transitions ◮ General form trigger [guard]/effect source state target state ◮ Triggers (events, method calls) ◮ Guard: boolean expression ◮ Effect: a statement ◮ Fireing a transition ◮ trigger + guard is true then the effect is executed

  9. Implementation 1: Class diagram SecretPanelController <<enumeration>> Event currentState candleRemoved handleEvent keyTurned openSafe revealLock currentState releaseKillerRabbit 1 <<enumeration>> State wait open lock finalState

  10. Implementation 1 public class SecretPanelController { enum State = { wait, lock, open, finalState }; enum Event = { candelRemoved, keyTurned, openSafe, revealLock, releaseKillerRabit }; private State state = States.wait; public void handleEvent (Event anEvent) { switch (currentState) { case open : switch (anEvent) { case safeClosed : CurrentState = state.wait; break; } break; case wait : switch (anEvent) { case candleRemoved : if (isDoorOpen) { RevealLock(); currentState = state.lock; } break; } break; case lock : switch (anEvent) {...} break; } } } }

  11. Implementation 2: Class diagram SecurePanelController <<enumeration>> State candleRemoved currentState wait keyTurned open safeClosed 1 lock openSafe finalState revealLock releaseKillerRabbit

  12. Implementation 2 public class SecretPanelController { enum State { wait, lock, open, finalState }; State state = State.wait; public void candleRemoved() { switch (state) { case wait: if (doorClosed()) { state = states.lock; break; } } } public void keyTurned() { switch (state) { case lock: if (candleOut()) { state = states.open; } else { state = states.finalState; releaseRabbit(); } break; } } ... }

  13. Implementation 3: Using the state pattern

  14. State Pattern State Pattern ”Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.” Design Pattern book AClass State sd: StatePattern request1 request1 request2 request2 ... * changeState State1 State2 request1 request1 request2 request2

  15. Vending machine Implementation Uses the state pattern VendingMachine «interface» m.setCurrentMoney(m.getCurrentMoney() + i); VendingMachineState dispensedItem: Fruit currentMoney: int input(m: VendingMachine, money: int) totalMoney: int select(m: VendingMachinef: fruit) if (!m.hasFruit(fruit)) { 1 restMoney: int cancel(m: VendingMachine) m.setIdleState(); input(money: int) return; select(f: fruit) } cancel() if (m.hasEnoughMoneyFor(fruit)) { ~setIdleState() m.setIdleState(); ~dispense(f: Fruit) m.dispense(fruit); ~setCurrentStateForFruit(f: Fruit) } else { ~hasFruit(f: Fruit) m.setCurrentStateForFruit(fruit); IdleState } input(m: VendingMachine, money: int) select(m: VendingMachinef: fruit) cancel(m: VendingMachine) m.dispense(null); super.input(m, i); * if (m.hasEnoughMoneyFor(selectedFruit)) { «enumeration» FruitSelectionState m.setIdleState(); Fruit m.dispense(selectedFruit); input(m: VendingMachine, money: int) APPLE } select(m: VendingMachinef: fruit) BANANA cancel(m: VendingMachine) 1 m.setIdleState(); super.cancel(m);

  16. Sub states ◮ Substates help structure complex state diagrams (similar to subroutines)

  17. Contents State machines Library Application and GUI Layered Architecture: Persistence Layer

  18. Library Application: Text based UI User Screen 0) Exit 1) Login as administrator 1 Login Screen password adminadmin Logged in. Admin Screen 0) Logoff 0 Logged off.

  19. Example Library Application Offers the menu for - login as admin - borrowing and returning media - searching for media - exiting the application [pw incorrect]/print "Login failed" login User Screen Login Screen exit logoff/print "Logged off" [wrong selection]/print "Wrong selection" [pw correct]/print "Logged in" Admin Screen Offers the menu for - managing users - managing media - logoff

  20. Library App: Focus on user dialog Use state UserDialog to group the user screen activities User dialog [pw incorrect]/print "Login failed" login User Screen Login Screen [wrong selection]/print "Wrong selection" exit logoff/print "Logged off" [pw correct]/print "Logged in" Admin Dialog

  21. Library App: Overview Focus on the sequence of dialogs instead of screens exit User dialog logoff/print "Logged off" [pw correct]/print "Logged in" Admin Dialog

  22. Library App: Focus on admin dialog Use state AdminDialog to group the admin screen activities

  23. Library App UI: State Pattern {screen.processInput();} LibraryUI {abstract} Screen printMenu processInput printMenu 0..1 1 readInput processInput setScreen ...? basicLoop main ... {screen.printMenu();} * UserScreen AdminScreen LoginScreen printMenu printMenu printMenu processInput processInput processInput 1 ...? ...? ...? LibraryApp

  24. Library App: main application public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out, true); LibraryUI ui = new LibraryUI(); ui.basicLoop(in, out); } Basic loop public void basicLoop(BufferedReader in, PrintWriter out) throws IOException { String selection; do { printMenu(out); selection = readInput(in); } while (!processInput(selection, out)); } public void printMenu(PrintWriter out) throws IOException { screen.printMenu(out); } public boolean processInput(String input, PrintWriter out) throws IOException { return screen.processInput(input,out); }

  25. Library App user interface exercise (programming exercise 5) 1) Given tests for the functionality login; implement the tests using the state pattern 2) Design, test, and implement the remaining functionality of the library application

  26. Contents State machines Library Application and GUI Layered Architecture: Persistence Layer

  27. Layered Architecture: Persistency Layer for the library application ◮ Data stored in two files users.txt & media.txt ; address has no file Presentation Layer ◮ A book LibraryUI dtu.library.app.Book b01 some book author Application/Domain Layer some book title PersistentObject Mar 13, 2011 LibraryApp <empty line> Medium ◮ A user User dtu.library.app.User Book Cd cpr-number Address Some Name a@b.dk Kongevejen Persistency Layer 2120 PersistencyLayer Hellerup b01 c01 <empty line>

  28. Persistency Layer LibraryApp 1 PersistencyLayer PersistentObject key:String ... cache_users storeOn(out:PrintWriter) 0..1 clearDatabase() getKey():String createMedium(m:Medium) key:String cache_media 0..1 createUser(u:User) readMedium(sig:String):Medium readUser(cpr:String):User updateMedium(m:Medium) updateUser(m:User) deleteMedium(sig:String) User Medium deleteUser(cpr:String) ... ... getUsers(): List<User> getMedia(): List<Medium> ... ... borrowedMedia ... getKey():String getKey():String * storeOn(out:PrintWriter) storeOn(out:PrintWriter) readFromReader(r:Buff.Read. readFromReader(r:Buff.Read. ol:PersistencyLayer) ol:PersistencyLayer) { { return getCprNumber(); return getSignature(); } }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend