Konzepte objektorientierter Programmierung



Similar documents
INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012

Course/Year W080/807 Expected Solution Subject: Software Development to Question No: 1

Konzepte objektorientierter Programmierung

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

Homework/Program #5 Solutions

file://c:\dokumente und Einstellungen\Marco Favorito\Desktop\ScanCmds.html

Programming with Java GUI components

Medientechnik. Übung MVC

LAYOUT MANAGERS. Layout Managers Page 1. java.lang.object. java.awt.component. java.awt.container. java.awt.window. java.awt.panel

Swing. A Quick Tutorial on Programming Swing Applications


Assignment # 2: Design Patterns and GUIs

How to Convert an Application into an Applet.

GUI Event-Driven Programming

Using A Frame for Output

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Yosemite National Park, California. CSE 114 Computer Science I Inheritance

How To Build A Swing Program In Java.Java.Netbeans.Netcode.Com (For Windows) (For Linux) (Java) (Javax) (Windows) (Powerpoint) (Netbeans) (Sun) (

Extending Desktop Applications to the Web

CS 335 Lecture 06 Java Programming GUI and Swing

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

How Scala Improved Our Java

1 Hour, Closed Notes, Browser open to Java API docs is OK

Graphical User Interfaces

CSS 543 Program 3: Online Tic-Tac-Toe Game Professor: Munehiro Fukuda Due date: see the syllabus

JiST Graphical User Interface Event Viewer. Mark Fong

JIDE Action Framework Developer Guide

Gadget: A Tool for Extracting the Dynamic Structure of Java Programs

Principles of Software Construction: Objects, Design and Concurrency. GUIs with Swing. toad Spring 2013

MiniDraw Introducing a framework... and a few patterns

Fondamenti di Java. Introduzione alla costruzione di GUI (graphic user interface)

Developing GUI Applications: Architectural Patterns Revisited

LAB4 Making Classes and Objects

Java Programming Language

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

AP Computer Science Static Methods, Strings, User Input

How To Use The Command Pattern In Java.Com (Programming) To Create A Program That Is Atomic And Is Not A Command Pattern (Programmer)

ICOM 4015: Advanced Programming

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Implementação. Interfaces Pessoa Máquina 2010/ Salvador Abreu baseado em material Alan Dix. Thursday, June 2, 2011

Sample CSE8A midterm Multiple Choice (circle one)

Schueler-Organisiertes Lernen am Beispiel von Grafischen Benutzer-Schnittstellen in Java Tag der Offenen Tür - GTS 2008

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

ECE 122. Engineering Problem Solving with Java

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Patterns in. Lecture 2 GoF Design Patterns Creational. Sharif University of Technology. Department of Computer Engineering

Informatik II. // ActionListener hinzufügen btnconvert.addactionlistener(this); super.setdefaultcloseoperation(jframe.

UI software architectures & Modeling interaction

Twin A Design Pattern for Modeling Multiple Inheritance

CS170 Lab 11 Abstract Data Types & Objects

Nexawebホワイトペーパー. Developing with Nexaweb ~ Nexaweb to Improve Development Productivity and Maintainability

Java GUI Programming. Building the GUI for the Microsoft Windows Calculator Lecture 2. Des Traynor 2005

public class demo1swing extends JFrame implements ActionListener{

Automated Data Collection for Usability Evaluation in Early Stages of Application Development

Construction of classes with classes

method is never called because it is automatically called by the window manager. An example of overriding the paint() method in an Applet follows:

Grundlæggende Programmering IT-C, Forår Model solutions to. Written exam in Introductory Programming

CS193j, Stanford Handout #10 OOP 3

OBJECT ORIENTED PROGRAMMING LANGUAGE

AP Computer Science Java Subset

Syllabus for CS 134 Java Programming

Java CPD (I) Frans Coenen Department of Computer Science

Project 4 DB A Simple database program

Java Interview Questions and Answers

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

A flexible Software Architecture for Presentation Layers demonstrated on Medical Documentation with Episodes and Inclusion of Topological Report

java Features Version April 19, 2013 by Thorsten Kracht

Karsten Lentzsch JGoodies SWING WITH STYLE

DHBW Karlsruhe, Vorlesung Programmieren, Remote Musterlösungen

Model-View-Controller (MVC) Design Pattern

A Pattern for Designing Abstract Machines

Web-Service Example. Service Oriented Architecture

Assignment No.3. /*-- Program for addition of two numbers using C++ --*/

Copyright Descriptor Systems, Course materials may not be reproduced in whole or in part without prior written consent of Joel Barnum

Integrating Formal Models into the Programming Languages Course

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 2 November 21, 2011 SOLUTIONS.

Essentials of the Java(TM) Programming Language, Part 1

Object Oriented Programming with Java. School of Computer Science University of KwaZulu-Natal

DECORATOR PATTERN IN WEB APPLICATION

A Thread Monitoring System for Multithreaded Java Programs

During the process of creating ColorSwitch, you will learn how to do these tasks:

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Chapter 2 Introduction to Java programming

J a v a Quiz (Unit 3, Test 0 Practice)

XFlash A Web Application Design Framework with Model-Driven Methodology

Lab 5: Bank Account. Defining objects & classes

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Computing Concepts with Java Essentials

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

From Systems to Services

Quick Start Guide to Logging in to Online Banking

Appium mobile test automation

Software Design: Figures

public class Craps extends JFrame implements ActionListener { final int WON = 0,LOST =1, CONTINUE = 2;

Unit Testing and JUnit

Composing Concerns with a Framework Approach

Transcription:

Konzepte objektorientierter Programmierung Prof. Dr. Peter Müller Werner Dietl Software Component Technology Exercises 5: Frameworks Wintersemester 05/06

2 Homework 1 Observer Pattern From: Gamma, Helm, Johnson, Vlissides: Design Patterns

3 Java support for the Observer Pattern Subject java.util.observable - void addobserver(observer o) - void setchanged() - void notifyobservers(object arg) Observer java.util.observer - void update(observable o, Object arg) Events java.util.eventobject - EventObject(Object source) - Object getsource()

4 Events abstract public class ChangeEvent extends java.util.eventobject { public ChangeEvent( Object source, int v, int c ) { super(source); value = v; change = c; public int getvalue() { return value; public int getchange() { return change; private int value; private int change;

5 Specialized Events public class IncreaseEvent extends ChangeEvent { public IncreaseEvent(Object s,int v,int c) { super(s, v, c); public class DecreaseEvent extends ChangeEvent { public DecreaseEvent(Object s,int v,int c) { super(s, v, c);

6 ActiveValue Setup public class ActiveValue extends java.util.observable { private int myval; public void run() { int change; Random crnd = new Random(); myval = 0; while( true ) {

7 ActiveValue Main Loop while( true ) { change = crnd.nextint( 11 ) - 5; myval += change; sendevent( change ); Thread.sleep( 1000 );

8 ActiveValue Event Notification public void sendevent( int change ) { java.util.eventobject e; if( change >= 0 ) { e = new IncreaseEvent(this,myval,change); else { e = new DecreaseEvent(this,myval,change); setchanged(); notifyobservers( e );

9 ActiveValue Changing the Value public void reset() { setvalue( 0 ); public void setvalue( int value ) { int change; change = value - myval; myval = value; sendevent( change );

10 Class Hierarchy so far Observable Observer EventObject ChangeEvent ActiveValue DecreaseEvent IncreaseEvent

11 SignChangeObserver public class SignChangeObserver implements java.util.observer { public void update( java.util.observable s, Object e ) { if(!(e instanceof ChangeEvent)) return; ChangeEvent ce = (ChangeEvent) e; if( ce instanceof IncreaseEvent ) { System.out.println("+"); else { System.out.println("-");

12 TextChangeObserver public class TextChangeObserver implements java.util.observer { public void update(observable s, Object e) { if(!(e instanceof ChangeEvent)) return; ChangeEvent ce = (ChangeEvent) e; if( ce instanceof IncreaseEvent ) { System.out.print("++++ "); else { System.out.print("---- "); System.out.println("New Value: +ce.getvalue() + " Change: + ce.getchange() );

13 TextMain public class TextMain { public static void main( String[] args ) { ActiveValue v = new ActiveValue(); Observer o1 = new TextChangeObserver(); Observer o2 = new SignChangeObserver(); v.addobserver( o1 ); v.addobserver( o2 ); v.run();

14 Class Hierarchy Observer Observable EventObject ActiveValue ChangeEvent TextChangeObserver SignChangeObserver DecreaseEvent IncreaseEvent

15 GuiMain Adding a Graphical UI public class GuiMain { public static void main( String[] args ) { ActiveValue v = new ActiveValue(); Observer o1 = new GuiChangeObserver(); Observer o2 = new GuiSignChangeObserver(); v.addobserver( o1 ); v.addobserver( o2 ); v.run();

16 GuiChangeObserver Update public class GuiChangeObserver implements java.util.observer { public void update( Observable s, Event e ) { if(! (e instanceof ChangeEvent) ) return; ChangeEvent ce = (ChangeEvent) e; if( ce instanceof IncreaseEvent ) { sign.setforeground( Color.RED ); sign.settext("++++"); else { value.settext("new Value: " + ce.getvalue()); change.settext("change: " + ce.getchange());

17 Adding Components public Component createcomponents() { JPanel pane = new JPanel(); pane.setborder( BorderFactory.createEmptyBorder( 30, 30, // top, left 30, 30) // bottom, right ); pane.setlayout(new GridLayout(0, 3)); pane.add(sign); pane.add(value); pane.add(change); return pane;

18 Screenshot & Demo

19 Model-View-Controller Architecture View View View Report changes Read data Send events Update view Initiate operation Model (Application Interface) View View Controller Report changes

20 Adding a Simple Reset Controller button = new JButton(); button.addactionlistener( new ActionListener() { public void actionperformed( ActionEvent e) { thevalue.reset(); ); pane.add(button, BorderLayout.CENTER);

21 Composite Pattern From: Gamma, Helm, Johnson, Vlissides: Design Patterns

22 Composite Pattern in Swing Used many times Composite is called Container java.lang.object java.awt.component java.awt.container javax.swing.jcomponent javax.swing.jpanel

23 Homework 2 Factory Method Pattern From: Gamma, Helm, Johnson, Vlissides: Design Patterns

24 Example Classes Document Application ImageDocument DrawingApplication

25 Abstract Class Application public abstract class Application { public Application() { documents = new ArrayList<Document>(); // the factory method! protected abstract Document createdocument(); public void newdocument() { Document toadd = createdocument(); documents.add( toadd ); toadd.open();...

26 Interface Document public interface Document { void open(); void save(); void reload(); void close();

27 Class ImageDocument public class ImageDocument implements Document { public void open() { System.out.println("ImageDocument.open"); public void save() { System.out.println("ImageDocument.save");...

28 Class DrawingApplication public class DrawingApplication extends Application { public DrawingApplication() { super(); // the overridden factory method returns // a concrete object of type ImageDocument. protected Document createdocument() { return new ImageDocument(); public static void main( String[] args ) { DrawingApplication appl = new DrawingApplication(); appl.newdocument(); appl.newdocument(); appl.newdocument(); appl.saveall(); appl.closeall();

29 Example Classes Document Application ImageDocument DrawingApplication

30 Questions?