CSC 7322 Object Oriented Development J Paul Gibson
CSC 7322 : Object Oriented Development J Paul Gibson, A 207 paul. gibson@int-edu. eu http: //www-public. it-sudparis. eu/~gibson/Teaching/CSC 7322/ Design Patterns …/~gibson/Teaching/CSC 7322/L 5 -Design. Pattern-MVC. pdf 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 1
Introduction pattern noun 1 a model, guide or set of instructions for making something • a dress pattern. 2 a decorative design, often consisting of repeated motifs, eg on wallpaper or fabric. 3 a piece, eg of fabric, as a sample. 4 any excellent example suitable for imitation. 5 a coherent series of occurrences or set of features • a pattern of events. verb (patterned, patterning) (usually pattern something on another thing) to model it on another type, design, etc. ETYMOLOGY: 14 c as patron; French, from Latin patronus example or defender. SOURCE - Chambers 21 st Century Dictionary 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 2
Introduction design verb (designed, designing) 1 to develop or prepare a plan, drawing or model of something before it is built or made. 2 formal to plan, intend or develop something for a particular purpose. noun 1 a plan, drawing or model showing how something is to be made. 2 the art or job of making such drawings, plans, etc. 3 the way in which something has been made. 4 a picture, pattern, arrangement of shapes, etc used eg as decoration. 5 a plan, purpose or intention. designable adj. designedly adverb intentionally; on purpose. designing adj, derog using cunning and deceit to achieve a purpose. designingly adverb. by design intentionally. have designs on someone or something to have plans to appropriate them or it. ETYMOLOGY: 16 c: from French désigner. SOURCE - Chambers 21 st Century Dictionary 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 3
Introduction design pattern programming A description of an object-oriented design technique which names, abstracts and identifies aspects of a design structure that are useful for creating an objectoriented design. The design pattern identifies classes and instances, their roles, collaborations and responsibilities. Each design pattern focuses on a particular object-oriented design problem or issue. It describes when it applies, whether it can be applied in the presence of other design constraints, and the consequences and trade-offs of its use. Home (http: //st-www. cs. uiuc. edu/users/patterns. html) ["Design Patterns: Elements of Reusable Object-Oriented Software", Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides]. (1997 -07 -21) SOURCE – Free On-Line Dictionary Of Computing (http: //foldoc. org/) 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 4
Introduction 1970’s - Christopher Alexander (an architect) worked on « Pattern Languages » which were applied in many domains: a structured method of describing good design practices within a field of expertise. Using a pattern language permits non-experts in a field to successfully solve very large, complex design problems. A single problem is documented with its typical place (the syntax), and use (the grammar) with the most common and recognized good solution seen in the real world, like the entries seen in dictionaries. Such an entry is considered to be a single design pattern; and a rich set of patterns form a language. 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 5
Introduction Early research: THING-MODEL-VIEW-EDITOR an Example from a planning system, Trygve Reenskaug, 1979. Using Pattern Languages for Object-Oriented Programs, Kent Beck and Ward Cunningham, 1987. A Cookbook for Using View-Controller User the Model. Interface Paradigm in Smalltalk-80, Krasner and Pope, 1988. 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 6
Introduction Breakthrough into mainstream: Gang of Four (GOF) Design Patterns: Abstraction and Reuse of Object-Oriented Design, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, 1993 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 7
Introduction Further reading: • Design Patterns for Object-Oriented Software Development, Pree and Sikora, 1997 • The origins of pattern theory: the future of theory, and the generation of a living world, C. Alexander, 1999 • Software Factories Assembling Applications with Patterns, Models, Frameworks and Tools, Greenfield and Short, 2003. • The Model-View-Controller (MVC) Its Past and Present, Trygve Reenskaug, 2003. • What Is Web 2. 0: Design Patterns and Business Models for the Next Generation of Software, O'Reilly, 2007 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 8
Definitions Alexander : As an element in the world, each pattern is a relationship between a certain context, a certain system of forces which occurs repeatedly in that context, and a certain spatial configuration which allows these forces to resolve themselves. As an element of language, a pattern is an instruction, which shows how this spatial configuration can be used, over and over again, to resolve the given system of forces, wherever the context makes it relevant. Each pattern describes a problem that occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice. 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 9
Definitions Erich Gamma (Go. F): Patterns provide you with tools that help you with design problems. They do so not by giving a pat solution but by explaining trade-offs. Even though patterns are abstracted from concrete uses, they also provide you valuable implementation hints. From my perspective it is the fact that patterns are implementable that makes them so valuable. Patterns are distilled from the experiences of experts. They enable you to repeat a successful design done by someone else. However, since patterns enable many implementation variations you still have to keep the brain turned on. Since patterns provide you with names for design building blocks they provide you with a vocabulary to describe and discuss a particular design. I think patterns as a whole can help people learn object-oriented thinking: how you can leverage polymorphism, design for composition, delegation, balance responsibilities, and provide pluggable behavior. 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 10
Elements of Design Patterns • Pattern Name • Problem statement -context where it might be applied • Solution - elements of the design, their relations, responsibilities, and collaborations (including a template of the solution) • Consequences - Results and trade-offs 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 11
The 23 Go. F patterns are generally considered the foundation for all other patterns. They are categorized in three groups: Creational, Structural, and Behavioral. 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 12
Design pattern composition: MVC example Traditional version of MVC as a compound pattern 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 13
Design pattern composition: MVC example Alternative version of MVC as a compound pattern 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 14
Learning by PBL • The best way to learn about patterns is to look at examples. • We shall do these in UML/Java, but any (OO) language can be used to model/implement/re-use a pattern. • It is best if you discover patterns yourself, rather than being shown them – but this is not guaranteed to happen! • You can also read about them. A good web site is: http: //sourcemaking. com/design_patterns 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 15
The Model View Design Pattern – PBL Session Dowload the code from the module web site: …~gibson/Teaching/CSC 7322/Code/Design. Pattern-MVC. zip Note the package structure – particularly the MVC components: • Models • Views • Controllers Let us examine some of this code together 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 16
The Model View Design Pattern – PBL Session Paddle. Specification 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 17
The Model View Design Pattern – PBL Session Paddle. Specification 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 18
The Model View Design Pattern – PBL Session Paddle. Specification /** * The lower bound on the horizontal position of the paddle */ final int MINIMUM_position = 0; /** * The upper bound on the horizontal position of the paddle */ final int MAXIMUM_position = 31; /** * @return true if the <code> MINIMUM_position </code> value is * less than the <code> MAXIMUM_position</code> */ boolean INVARIANT_OF_CLASS = (MINIMUM_position <= MAXIMUM_position); 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 19
The Model View Design Pattern – PBL Session Paddle. Specification 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 20
The Model View Design Pattern – PBL Session Paddle. Specification 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 21
The Model View Design Pattern – PBL Session Paddle. Specification 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 22
The Model View Design Pattern – PBL Session Paddle. Specification 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 23
The Model View Design Pattern – PBL Session Paddle Implementation: Paddle 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 24
The Model View Design Pattern – PBL Session Paddle Implementation: Paddle TO DO: Examine the code and check that you understand it. 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 25
The Model View Design Pattern – PBL Session Paddle Implementation: Paddle TO DO: Examine the code and check that you understand it. 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 26
The Model View Design Pattern – PBL Session Paddle Implementation: Paddle QUESTION: Do the methods that change the state respect the invariant? public void update. Position(){ if (direction. To. Right && position < MAXIMUM_position) position++; else if (!direction. To. Right && position > MINIMUM_position) position--; else change. Direction(); } public void change. Direction(){ direction. To. Right= ! direction. To. Right; } 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 27
The Model View Design Pattern – PBL Session Paddle Implementation: Testing the Paddle Model 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 28
The Model View Design Pattern – PBL Session Unit Testing the Paddle Model TO DO: Check that you understand the unit test code 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 29
The Model View Design Pattern – PBL Session Paddle View Specification Once we have tested our model (the Paddle) we can develop a (graphical) view: 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 30
The Model View Design Pattern – PBL Session Paddle View Specification QUESTION: Why do we specify these methods? 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 31
The Model View Design Pattern – PBL Session Paddle View Test public class Test_Paddle. View { public static void main(String args[]) { Paddle paddle. Model = new Paddle(0); Paddle. View paddle. View = new Paddle. View(paddle. Model); paddle. View. update. View(); } } 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 32
The Model View Design Pattern – PBL Session Paddle View – the implementation 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 33
The Model View Design Pattern – PBL Session Paddle View – the implementation public Paddle. View(Paddle. Specification rv. Paddle){ TO DO: Check your understanding of the code frame = new JFrame("Paddle MVC"); frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); frame. set. Size(VIEW_WIDTH+(BORDER/2), VIEW_HEIGHT+(BORDER/2)); frame. set. Visible(true); frame. set. Resizable(false); canvas = new Paddle. View. Canvas(rv. Paddle, VIEW_WIDTH, VIEW_HEIGHT); frame. get. Content. Pane(). add(canvas, Border. Layout. CENTER ); } 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 34
The Model View Design Pattern – PBL Session Paddle View Canvas – where the drawing is done inside the View frame TO DO: Check your understanding of the code 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 35
The Model View Design Pattern – PBL Session Paddle View – the implementation 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 36
The Model View Design Pattern – PBL Session Extending the model so that it can be animated inside the view 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 37
The Model View Design Pattern – PBL Session Extending the model so that it can be animated inside the view 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 38
The Model View Design Pattern – PBL Session Extending the model so that it can be animated inside the view public void run(){ do{ try { Thread. sleep(DELAY); } catch (Interrupted. Exception e) {e. print. Stack. Trace(); } update. Position(); if (paddle. View !=null) paddle. View. update. View(); } while (true); } QUESTION: do you understand how the delay is implemented? 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 39
The Model View Design Pattern – PBL Session Testing the animated view public class Test_Runnable. Viewable. Paddle { public static void main(String args[]) { Runnable. Viewable. Paddle rv. Paddle = new Runnable. Viewable. Paddle(); Paddle. View paddle. View = new Paddle. View(rv. Paddle); rv. Paddle. set. View(paddle. View); Thread paddle. Thread = new Thread(rv. Paddle); paddle. Thread. run(); } } 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 40
The Model View Design Pattern – PBL Session Adding a controller to the system – the specification 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 41
The Model View Design Pattern – PBL Session Adding a controller to the system – the implementation public class Paddle. Controller extends Paddle. Controller. Abstraction{ /** * The model being controlled by the controller */ Paddle. Specification paddle; /** * @param rv. Paddle is the model to be controlled by the controller */ public Paddle. Controller(Paddle. Specification rv. Paddle){ this. paddle = rv. Paddle; } /** * Change direction when a key is typed */ public void key. Typed(Key. Event e){ paddle. change. Direction(); } } 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 42
The Model View Design Pattern – PBL Session Adding a controller to the system – the MVC structure 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 43
The Model View Design Pattern – PBL Session Adding a controller to the system – the MVC structure public Paddle. MVC(){ // Construct model rv. Paddle = new Runnable. Viewable. Paddle(); // Construct view which can see model paddle. View = new Paddle. View(rv. Paddle); //Allow the model to see view in order to make updates when state changes rv. Paddle. set. View(paddle. View); //Construct controller Paddle. Controller paddle. Controller = new Paddle. Controller(rv. Paddle); //The frame which contains the view must allow the controller to react to key presses paddle. View. get. Frame(). add. Key. Listener(paddle. Controller); } 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 44
The Model View Design Pattern – PBL Session Adding a controller to the system – the MVC structure Now we just need a method that starts a thread containing the runnable viewable paddle public void startgame(){ Thread paddle. Thread = new Thread((Runnable) rv. Paddle); paddle. Thread. run(); } 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 45
The Model View Design Pattern – PBL Session Adding a controller to the system – the MVC system application public class Paddle. MVC_Application { public static void main(String[] args){ Paddle. MVC application = new Paddle. MVC(); application. startgame(); } } 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 46
The Model View Design Pattern – PBL Session The MVC problem The use of the MVC design pattern should make it easier to maintain/extend/update the Paddle application. TO DO: 1. 2. 3. Change the model so that the paddle doesn’t bounce it wraps around Change the controller so that you have to hold down a button to move left and hold down a button to move right Change the view so that the direction of the next move is represented graphically QUESTION: How many different Paddle systems are now possible (based on 2 different models, two different views and 2 different controllers)? Can you instantiate all of them and test their behaviour? 2013: J Paul Gibson TSP: Software Engineering CSC 7322/Design. Pattern-MVC. 47
- Slides: 47