Architectural Patterns Support Lecture Patterns Pattern: A representation of a proven solution. Problem Applicable Forces Solution Consequences Benefits.

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Events ● Anything that happens in a GUI is an event. For example: – User clicks a button, presses return when typing text, or chooses a menu item ( ActionEvent.
Satzinger, Jackson, and Burd Object-Orieneted Analysis & Design
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Lecture 241 CS110 Lecture 25 Tuesday, May 4, 2004 Announcements –final exam Thursday, May 20, 8:00 AM McCormack, Floor 01, Room 0608 (easier than last.
Object-Oriented Analysis and Design
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter Day 9. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 8 Questions from last Class?? Problem set 2 posted  10 programs from.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Io package as Java’s basic I/O system continue’d.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 4 Slide 1 Slide 1 Message Analysis Table.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
11/9: Recursion, Method Overloading About Scoping.java Recursion Method overloading.
1 Another group of Patterns Architectural Patterns.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
GUI programming Graphical user interface-based programming.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Systems Analysis and Design in a Changing World, 3rd Edition
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
Architectural Patterns Support Lecture. Software Architecture l Architecture is OVERLOADED System architecture Application architecture l Architecture.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
1 GUI programming Graphical user interface-based programming Chapter G1 (pages )
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 8.1 Test-Driving the Car Payment Calculator.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11.
Java the UML Way versjon Only to be used in connection with the book "Java the UML Way", by Else Lervik and.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
SEA Side – Extreme Programming 1 SEA Side Software Engineering Annotations Architectural Patterns Professor Sara Stoecklin Director of Software Engineering-
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 28.1 Java Speech API 28.2 Downloading and.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
GUIs and Events Rick Mercer.
Introduction Many Java application use a graphical user interface or GUI (pronounced “gooey”). A GUI is a graphical window or windows that provide interaction.
GUI- Model-View-Controller
CSC 205 Programming II Lecture 5 AWT - I.
Introduction to Graphics
Chapter 14 – Exception Handling
A First Look at GUI Applications
“Form Ever Follows Function” Louis Henri Sullivan
Ellen Walker Hiram College
Event-driven programming for GUI
The structure of Interactive Software
Chapter 5 Processing Input with Applets
GUI- Model-View-Controller
Presentation transcript:

Architectural Patterns Support Lecture

Patterns Pattern: A representation of a proven solution. Problem Applicable Forces Solution Consequences Benefits

Software Architecture l Architecture is OVERLOADED System architecture Application architecture l Architecture for this presentation is l The modules, processes, interconnections, and protocols which constitute the deployed software system. l Different from the behavioral architecture which describes how the business classes execute the business processes.

Architecture Specification l Document which defines in text and diagrams the design, flow and technologies of the design. l Shows how persistence, communication, and behavior are to be implemented in the system.

Architectural Layers - Patterns Presentation interactions with the user – HTML, thick client, MVC, web services Domain (Logic) Business rules, validations, calculations, verifications Data Storage database Environmental Session management, messaging

Presentation Architectural Patterns l Model View Controller – our focus l Application Controller l Input Controller Page Controller Front Controller l View Controller Template View Transform View Two Step View

Model View Controller Model View Controller (MVC) – an architectural pattern that modularizes GUI applications into three well-formed object-oriented components namely the view (presentation) component, the model (data) component, and the controller (action) component. WHY? We use MVC because of REUSE : one screen view may be used with various controllers one data model may be used with many screens

Model View Controller MODEL VIEW Controller 1. When a user modifies the view on the screen 2. The controller modifies the model with the new input. 3. The model validates the change and updates the view.

Model View Controller There are various uses for the MVC application pattern such as 1)Simple division of responsibilities 2)Allowing one view and multiple controllers. 3)Allowing one model with multiple views. 4)Allowing one view and multiple models

Model View Controller Our MVC example will have one view and two controllers. One controller will allow viewing of the value of an item. The other controller will allow the editing of the field. A.We will ONLY have the view items in the view including a get method allowing the return of a specific graphical component to the controller to attach an action listener. B.We will define two controllers – one for displaying the data, the other for editing. C.We will have one model to hold the data.

Model View Controller Scenario 1: We want to use our screen for the VRS to allow the customer to enter their member information. We will need several classes. One for the member – the model class information (this could be multiple classes), one for the member display or view (again this could be multiple classes), one for the controller for that view (at least one for each view), and one for the database table for updating the database.

Model View Controller Scenario 1: Membership Application First Name Last Name Phone Number Format xxx-xxx-xxxx Credit Card Number Address Only Master Card Accepted Expiration Date Format MMYYYY Format 8 to 20 alphabetic characters SUBMIT

Model View Controller Example import java.applet.*; import java.awt.*; import javax.swing.*; public class MemberView extends Panel { private JLabel firstNameLabel; private JTextField firstNameTextField; private JLabel lastNameLabel; private JTextField lastNameTextField; ……// others First lets look at the View Define the View with a name that allows you to realize it is indeed a view. Define the components within the view.

Model View Controller Example public JTextField getFirstNameTextField() { return firstNameTextField; } // end getFirstNameTextField public JTextField getLastNameTextField() { return lastNameTextField; } // end getLastNameTextField Add two methods that return the components needing the action listeners. Note: these methods return the actual instance of the components so that the controller can add the action listeners. View

Model View Controller Example public void addFirstNameLabel() { firstNameLabel = new JLabel(“First Name"); add(firstNameLabel); firstNameLabel.setBounds (10,10,100,25); } // end addFirstNameLabel public void addFirstNameTextField() { firstNameTextField = new JTextField(); addfirstNameTextField); firstNameTextField.setBackground (Color.yellow); firstNameTextField.setBounds (80,10,100,25); } // end addFirstNameTextField Add the first name label and textfield to the component in their respective methods. View

Model View Controller Example public void addLastNameLabel() { lastNameLabel = new JLabel(“Last Name"); add(lastNameLabel); lastNameLabel.setBounds (200,10,180,25); } // end addLastNameLabel public void addLastNameTextField() { lastNameTextField = new JTextField (); add(lastNameTextField); lastNameTextField.setBackground (Color.red); lastNameTextField.setBounds ( 400,10,60,25); } // end addLastNameTextField Add the last name label and textfield in their respective methods. View

Model View Controller Example public MembertView ( ) { setLayout(null); addFirstNameLabel(); addFirstNameTextField(); addLastNameLabel(); addLastNameTextField(); setSize (350,350); } // end constructor } // end View Not in the constructor, you can set the layout to null so you can use the direct positioning, add the four components and set the size of the panel. This completes the view. View

Model View Controller Example public class Member { private String firstName; private String lastName; …….// other elements public void setFirstName (String firstName) { this.firstName = firstName; } public void setLastName (String lastName) { this.lastName= lastName;} ….. public String getFirstName() { return firstName; } public String getLastName() { return lastName;} ……. public Member () { firstName = new String("Sara"); lastName = new String (“Stoecklin"); } // end constructor } // end class We have defined our model as a record with the necessary data. Now lets look at the Model or Record.

Model View Controller Example import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class MemberController implements WindowListener { private JTextField firstNameTextField; private JTextField lastNameTextField; MemberView memberView; Memberl member; Then we define both the view namely MemberView and the model called Member. Now lets look at the Controller. First, we define the components which we wish to add action listerners namely the firstName and lastName textfield.

Model View Controller Example public MemberController (MemberView memberView) { this.memberView = memberView; member = new Member(); setFirstNameTextField (); setLastNameTextField(); } // end constructor In the constructor, we pass the name of the view we wish to use for this controller. Controller

Model View Controller Example public MemberController (MemberView memberView) { this.memberView = memberView; member = new Member(); } // end constructor We then make an instance of both the view and the model. Controller

Model View Controller Example public void editView ( ) { setFirstNameTextField (); setLastNameTextField(); } // end editView We then call two methods, one method to set up the firstName textfield with an action listener and the other to set up the lastName textfield’s listener. Controller

Model View Controller Example public void setFirstNameTextField() { nameTextField = memberView.getFirstNameTextField(); firstNameTextField.addActionListener (new ActionListener() { public void actionPerformed (ActionEvent e) { member.setFirstName(nameTextField.getText()); System.out.println (“Member First Name: " + member.getFirstName()); } // end actionPerformed } // end new );// end addActionListener parameter } // end setFirstNameTextField() In this method we first get the name textfield from the view using the get method in the view class.. Controller

Model View Controller Example public void setFirstNameTextField() { firstNameTextField = memberView.getFirstNameTextField(); firstNameTextField.addActionListener (new ActionListener() { public void actionPerformed (ActionEvent e) { member.setFirstName(firstNameTextField.getText()); System.out.println (“Member First Name: " + member.getFirstName()); } // end actionPerformed } // end new );// end addActionListener parameter } // end setFirstNameTextField() We then add the action listener as an anomalous listener instance with the embedded action listener for ONLY the textfield. Controller

Model View Controller Example public void setNameTextField() { nameTextField = MemberView.getNameTextField(); nameTextField.addActionListener (new ActionListener() { public void actionPerformed (ActionEvent e) { member.setName(nameTextField.getText()); System.out.println (“Member First Name: " + member.getFirstName()); } // end actionPerformed } // end new );// end addActionListener parameter } // end setFirstNameTextField() Set the Name in the model (record) to the name keyed in the text when the textfield encounters an enter action. Then print the field Controller

Model View Controller Example public void setLastNameTextField() { lastNameTextField = memberView.getLastNameTextField(); lastNameTextField.addActionListener (new ActionListener() { public void actionPerformed (ActionEvent e) { memberRecord.setLastName(lastNameTextField.getText()); System.out.println (“Member Last Name: " + member.getLastName()); } // end actionPerformed } // end new );// end addActionListener parameter } // end seLasttNameTextField() The DOB textfield follows the same pattern. Controller

Model View Controller Example public void windowClosing (WindowEvent e) { System.exit(0); } // end windowClosing public void windowClosed (WindowEvent e) { System.exit(0); } // end windoeClosed public void windowOpened(WindowEvent e) { } public void windowIconified (WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } } // end class End the class. Controller

Model View Controller Example import java.applet.*; import java.awt.*; import javax.swing.*; public class MVC extends JApplet { privateMemberView mvcMemberView; private Member mvcMember; private MemberController mvcMemberController; private Container container; public void init( ) { container = getContentPane(); container.setLayout (new GridLayout()); mvcMemberView = new MemberView (); mvcMemberController = new MemberController(mvcMemberView); mvcMemberController.editview(); container.add (mvcMemberView); container.setBounds (50,50,500,500); } // end init } // end MVC Set up the view, record and controller variables. Now the Applet. Your use case controller.

Model View Controller Example import java.applet.*; import java.awt.*; import javax.swing.*; public class MVC extends JApplet { private MemberView mvcMemberView; private MemberRecord mvcMemberRecord; private MemberController mvcMemberController; private Container container; public void init( ) { container = getContentPane(); container.setLayout (new GridLayout()); mvcMemberView = new MemberView (); mvcMemberController = new MemberController(mvcMemberView); mvcMemberController.editView(); container.add (mvcMemberView); container.setBounds (50,50,500,500); } // end init } // end MVC Make an instance of the view. Make an instance of the controller passing the view as a parameter. Applet your use case controller

Model View Controller Example import java.applet.*; import java.awt.*; import javax.swing.*; public class MVC extends JApplet { private MemberView mvcMemberView; private Member mvcMemberRecord; private MemberController mvcMemberController; private Container container; public void init( ) { container = getContentPane(); container.setLayout (new GridLayout()); mvcMemberView = new MemberView (); mvcMemberController = new MemberController(mvcMemberView); mvcMemberController.editView(); container.add (mvcMemberView); container.setBounds (50,50,500,500); } // end init } // end MVC Call the editView routine to allow the textfields to be connected to the action listener. Applet - your use case controller

Model View Controller Once you begin having many interacting classes, some type of diagram abstraction is most helpful in making changes to the design of your system. The Unified Modeling Language (UML) is a good tool for modeling these class interactions. We will first model the MVC example using the application rather than the applet.

Model View Controller UseCase The line represents a class or instance of a class. Instances are represented by an underlined class name. REMEMBER: not all developers use a driver. Instead they have the controlling class drive the application. We use one because it makes teaching and understanding easier.

Model View Controller UseCase These lines represent the invoking of the constructor for the Container with calls to the methods getContentPane and setLayout for new GridLayout. Container create () getContentPane () setLayout (newGridLayout)

Model View Controller Driver It then invokes the Member class with no parameters, the MemberView class with no parameters, and the MemberController with two parameters.. create () Member View create () Member Controller create (member, membertView) Container create () getContentPane () setLayout (newGridLayout)

Model View Controller Use Case We then add the member view panel to the container. And then we call the edit routine in the controller. We then setBounds and make the frame viewable. create () Member View create () Member Controller create (member, memberView) Container create () getContentPane () setLayout (newGridLayout ()) add (membertView) edit () setBounds()

Model View Controller Example import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Application extends JFrame implements WindowListener{ private Container container; private MemberController memberController; // change this line private Member member; private MemberView memberView; Declare the variables for the controller, record and the view. Now the Application if you did not want an applet

Model View Controller Example public Application ( ) { super ("Test for Application GUI"); container = getContentPane(); container.setLayout(new GridLayout()); member = new MemberRecord (); memberView = new MemberView(); memberController = new MemberController(memberView, memberRecord); container.add (memberView); memberController.editView(); container.setBounds (10,10,700,700); setSize (700,700); setVisible (true); } // end constructor Create instances of the view and the record and of the MemberController with the parameters of the view and the record. Call the editView routine in the controller. Now the Application

Model View Controller Example public void windowClosing (WindowEvent e) { System.exit(1); } // end windowClosing public void windowClosed (WindowEvent e) { } public void windowOpened(WindowEvent e) { } public void windowIconified (WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } public static void main (String args [ ] ) { Application myApplication = new Application(); myApplication.setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE); } // end main } // end Application Perform the necessary window activity. Now the Application

Model View Controller Scenario 1: When the applet appears, you can enter the firstName and lastName, etc. Membership Application First Name Last Name Phone Number Format xxx-xxx-xxxx Credit Card Number Address Only Master Card Accepted Expiration Date Format MMYYYY Format 8 to 20 alphabetic characters SUBMIT

Model View Controller Scenario 2: Now write more code that allows ONLY displaying of the data within the record and does not allow editing those fields. You should be able to reuse some classes.

Model View Controller Scenario 2: Membership Application First Name Last Name Phone Number Format xxx-xxx-xxxx Credit Card Number Address Only Master Card Accepted Expiration Date Format MMYYYY Format 8 to 20 alphabetic characters SUBMIT This applet displays the data in the model upon construction.

Model View Controller Example Scenario 2: The View will be the same. The Model or Record will also be the same.

Model View Controller Now lets look at the Controller. We will need three new methods. One for allowing non editable viewing of the firstName textfield. One for allowing non editable viewing of the lastName textfield. One method called by the driver to call these methods.

Model View Controller Controller public void displayView () { displayFirstNameTextField (); displayLastNameTextField(); } // end display view public void displayNameTextField() { firstNameTextField = memberView.getFirstNameTextField(); firstNameTextField.setText(member.getFirstName()); firstNameTextField.setEditable(false); } // end viewFirstNameTextField() public void displayLastNameTextField() { lastNameTextField = memberView.getLastNameTextField(); lastNameTextField.setText(member.getLastName()); lastNameTextField.setEditable(false); } // end viewNameTextField() This method called by the driver. These methods allow display of the data in the model and prohibits it from editing.

Model View Controller import java.applet.*; import java.awt.*; import javax.swing.*; public class MVCDisplay extends JApplet { privateMemberView mvcMemberView; private Member mvcMember; private MemberController mvcMemberController; private Container container; public void init( ) { container = getContentPane(); container.setLayout (new GridLayout()); mvcMemberView = new MemberView (); mvcMemberController = new MemberController(mvcMemberView); mvcMemberController.displayView(); container.add (mvcMemberView); container.setBounds (50,50,500,500); } // end init } // end MVC This applet calls the displayView method. Applet