
Software Engineering G22.2440-001 Session 7 – Sub-Topic 5 Introduction to Design and Architectural Patterns Dr. Jean-Claude Franchitti New York University Computer Science Department Courant Institute of Mathematical Sciences 1 First Example A Dice Game A Player rolls 10x 2 dices If result = 7, score=score + 10 points At the end, score of the player is registred in the highscore table. 2 1 Activity Diagram menu [highscore] [start] [exit] view Start Highscore turn=0 Roll Dice turn++ [true] Turn<10 [false] Update highscore 3 Analysis Diagram… <<Actor>> Rolls Die Player faceValue : int = 1 name : String 1 2 score : int = 0; roll() Die() 1 Plays play() 1 Player() 1 DiceGame Includes DiceGame() 1 start() 1 Scoring 1 Entry HighScore name:String : type = initval score:int : type = initval Highscore() add() 1 0..* Entry(name:String,score:int)() 4 2 Design Stage Manage User Interface Manage Persistence of highscore in a file or in relational database Realize a layered architecture : Apply the Layer Architectural Pattern 5 Layer Helps structure an application that can be decomposed into groups of subtasks in which each group of subtasks is at a particular level of abstraction. 6 3 Layer: examples 7 Layer :Structure 8 4 Layer: Structure 9 Layer and components… 10 5 Layers : Variants Relaxed Layered System: – A layer « j » can use service of j-1, j-2… – A layer can be partially opaque • Some service to layer j+1, others to all upper services… Layering through inheritance: – Lower layers are implemented as base classes – Higher level can override lower level… 11 Layers : Known Uses • Virtual machines: JVM and binary code format • API : Layer that encapsulates lower layers • Information System – Presentation, Application logic, Domain Layer, Database • Windows NT (relaxed for: kernel and IO and hardware) – System services, – Resource management (Object manager, security monitor, process manager, I/O manager, VM manager, LPC), – Kernel (exception handling, interrupt, multipro synchro, threads), – HAL (Hardware Abstraction Level) – Hardware 12 6 Layers: benefits Reuse of layers Support for standardization (POSIX) Dependencies are kept local Exchangeabilities : – Replacement of old implementation with Adapter Pattern – Dynamic exchange with Bridge Pattern 13 Layers: Liabilities Cascades of changing behavior Lower efficiency Unnecessary work: functions of a layer called many times for one service Difficulty of establishing correct granularity of layers: Too few layers -> less benefits, too many layers -> complexity and overhead… 14 7 Applying Layer Architecture UI Core Play View High Score Persistence Fichier ou BDD 15 Package decomposition <<layer>> UI <<layer>> <<subsystem>> Core Util <<layer>> Persist 16 8 Layer « core » Contain business logic classes… Adapt analysis classes for implementation Use of singleton Idiom… 17 Singleton (Idiom) Ensure a class only has one instance, and provide a global point of access to it. 18 9 Singleton Structure 19 Core « Layer »:First diagram Design Analysis HighScore Entry Rolls $ hs : HighScore = null <<Actor>> name:String : type = initval Player Die Highscore() score:int : type = initval name : String 1 2 add() 1 0..* score : int = 0; faceValue : int = 1 load() Entry(name:String,score:int)() save() play() roll() Player() Die() Plays 1 1 DiceGame Singleton... $ dg = null 1 DiceGame() DiceGame Includes getInstance() start() DiceGame() 1 start() 2 1 -player Scoring -dies Die 1 Player faceValue : int = 1 name : String 1 score : int = 0; Entry roll() HighScore Die() name:String : type = initval Player() display() score:int : type = initval display() Highscore() add() 1 0..* Entry(name:String,score:int)() 20 10 Util « subsystem » • Need for random numbers • Use java.util « random » functionality… • The random number generator is shared by the die… • Another singleton... Subsystem « util » Random (from util) $ serialVersionUID : long = 3905348978240129619L seed : long $ multiplier : long = 0x5DEECE66DL $ addend : long = 0xBL Singleton $ mask : long = (1L << 48) - 1 $ BITS_PER_BYTE : int = 8 $ BYTES_PER_INT : int = 4 nextNextGaussian : double haveNextNextGaussian : boolean = false Random() Randomizer Random() 1 setSeed() getInstance() next() getValue() nextBytes() nextInt() nextInt() nextLong() nextBoolean() nextFloat() nextDouble() nextGaussian() 11 Package decomposition <<layer>> UI <<layer>> <<subsystem>> Core Util <<layer>> Persist 23 Observer One-to-many dependency between objects: change of one object will automatically notify observers 24 12 Observer: Applicability A change to one object requires changing an unknown set of other objects Object should be able to notify other objects that may not be known at the beginning 25 Observer: Structure 26 13 Observer: Consequences Abstract coupling between subject and observer Support for broadcast communication Hard to maintain 27 Applying Observer Pattern Observable (from util) changed : boolean = false <<Interface>> Observable() Observer addObserver() (from util) deleteObserver() 0..* notifyObservers() update(o : Observable, arg : Object) : void notifyObservers() deleteObservers() setChanged() clearChanged() hasChanged() countObservers() Die (from Core) DieView faceValue : int = 1 DieView(die : Die) roll() update(o : Observable, arg : Object) : void Player Die() (from Core) display() name : String PlayerView score : int = 0; Player() PlayerView(player : Player) display() update(o : Observable, arg : Object) : void 28 14 Observer View <<Interface>> Observer (from util) update(o : Observable, arg : Object) : void DieView DieView(die : Die) update(o : Observable, arg : Object) : void PlayerView PlayerView(player : Player) update(o : Observable, arg : Object) : void 29 Views are graphical objects Observable (from util) changed : boolean = false <<Interface>> Observable() Observer addObserver() (from util) deleteObserver() 0..* notifyObservers() update(o : Observable, arg : Object) : void notifyObservers() deleteObservers() setChanged() clearChanged() hasChanged() Panel countObservers() (from awt) Panel() Panel() constructComponentName() addNotify() Die (from Core) DieView faceValue : int = 1 roll() DieView(die : Die) update(o : Observable, arg : Object) : void Player Die() display() (from Core) setValue() name : String PlayerView score : int = 0; PlayerView(player : Player) Player() update(o : Observable, arg : Object) : void display() 30 15 Setting up Observer : RollForm : : PlayerView : Die : DieView Playe 1: display( ) 2: PlayerView(Player) 3: addObserver(Observer) 4: return component 5: display() 6: DieView(Die) 7: addObserver(Observer) 8: return component 31 Observer : Change Propagation : Die : Randomizer : DieView : JLabel 1: getValue( ) 2: setValue(int) 3: notifyObservers( ) 4: update(Observable, Object) 5: getState() 3 6: setText(3) 32 16 UI/Core Separation... HighScore Entry $ hs : HighScore = null name:String : type = initval Highscore() score:int : type = initval add() 1 0..* load() Entry(name:String,score:int)() save() DiceGame <<Interface>> Singleton... Displayable $ dg = null DiceGame() display() getInstance() start() 2 -dies -player Die faceValue : int = 1 1 Player name : String roll() score : int = 0; Die() display() Player() setValue() display() Layered Architecture... RollForm (from UI) roll_action() cancel_action() RollForm() +thePlayerViewPlayerView 2 DieView (from UI) (from UI) +theDieView 1 PlayerView() UI DieView() update() update() <<Interface>> Observable <<Interface>> Decoupling classes Observer Displayable (from util) (from util) and interfaces 0..* Die Core faceValue : int = 1 Player name : String roll() score : int = 0; Die() display() Player() setValue() display() 34 17 MVC • Java AWT Java: Delegation Model – Event propagation from user interface to core application classes • MVC: – State change propagation to graphical objects Package decomposition <<layer>> UI <<layer>> <<subsystem>> Core Util <<layer>> Persist 36 18 Pattern Factory Method Intent – Define an interface for creating an object, but let sub-classes decide which class to instantiate – let a class defer instantiation to subclasses – Also known as Virtual Constructor 37 Factory Method Applicability : Use when – a class cannot anticipate the class of objects it must create – a class wants its subclasses to specify the objects it creates – classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass to delegate. 38 19 Structure Facteur Produit produit=Fabrication() Fabrication() UneOperation() FacteurConcret ProduitConcret return new ProduitConcret() Fabrication() 39 Factory method Consequences – Provide hooks for subclasses – connects parallel class hierarchies Known uses – MacApp, ET++ – ClassView in smalltalk80 MVC (controller creation) – Orbix ORB for generating PROXY object 40 20 « Persist » Layer Persistence technical classes Ensure independence of Core/Persist – Be able to switch « persistent engine » For example: – Persistence via « Serialization » – Persistence via a relational database (JDBC). 41 Applying Factory HighScore (from Core) $ hs : HighScore = null Abstract produc Highscore() add() load() save() HighScoreSr Concrete product HighScoreJDBC $ filename : String = "/tmp/high.score" Highscore() Highscore() load() load() save() save() JdbcKit SrKit Concrete Factory makeKit() makeKit() Abstract Factory PersistKit makeKit() 42 21 Applying Factory : DiceGame : S rKit : HighScoreSr : RealPlayer 1: SrKit( ) A ttention! DiceGame voit S rKit comme un PersistKit
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages126 Page
-
File Size-