object oriented programming lecture 4: refactoring, an applet example, idiom - animation applets,...

30
Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise www2.hh.se/staff/jebe/oop2005/

Upload: gwendoline-chandler

Post on 01-Jan-2016

230 views

Category:

Documents


2 download

TRANSCRIPT

Object Oriented Programming

Lecture 4:Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise www2.hh.se/staff/jebe/oop2005/

Last time

Definition of Design Patterns The Singleton pattern The Iterator/Enumeration Pattern The Collections Framework

Data structures, Iterator, Enumeration Abstract Coupling

Explanation and definition of...

Shadowing of variablesPublic class {

int myVariable;....public doStuff(int test){

int myVariable;

myVariable = test;

this.MyVariable = test;}

}

Inner scope myVariable shadows the outer scope myVariable

global scopelocal

scope

Design rule using Shadowing In general - avoid using identical names for

variables For critical variables in sections where

something unexpected might happen that would leave variables in an inconsistent state

An example could Parallel tasks in multi threaded environments

accessing a global variable Unexpected exception

Otherwise Shadowing should be avoided Should not be needed in properly written

programs

Generics Generics is a new feature in java 1.5 Avoids the need for type casts when

working with collections Usually the programmer knows what type is

contained in a collection Benefits:

Type checking can be done at compile time and errors avoided during run time

Reduces ”type casting clutter” in the code Let’s see an example...

Refactoring

Refactoring is a technique that can be used to collect identical parts into a generic component

What is a generic component? Classes with general conent that can be

extended, adapted and reused in different programs

Can be reused without having to modify the current source code

Refactoring of methodsClass ComputeThings {

public void computeMany1(){anything();compute1();compute2();compute3();

}public void computeMany2(){

something();compute1();compute2();compute3();

}}

These parts are similar....

Refactoring of methodsClass computeThings {

public void computeMany1(){anything();computeAlot();

}public void computeMany2(){

something();computeAlot();

}public void computeAlot(){

compute1();compute2();compute3();

}}

Refactoring of methods

Benefits with method refactoring Reduces lines of written code Changes can be done more safely

without having to do the same changes at several places

Refactoring by Inheritance

Class computeStuff1{superCompute1(){

compute1();compute2();compute3();

}....

}

Class computeStuff2{somethingElse(){

compute1();compute2();compute3();

}...

}

Both classes uses

identical code

sections

Refactoring by InheritanceClass ComputeStuff1 extends

Common{...superCompute1(){

super.computeAlot();}....

}

Class ComputeStuff2 extends

Common{...somethingElse(){

super.computeAlot();}...

}

Class Common{...computeAlot(){

compute1();compute2();compute3();

}...

}

Refactoring by Delegation

Instead of inheriting factorized code Let the object that collects the

commonly recurring code be separate Can be accessed by:

A reference to the object Declaring the methods static

Example: The classes Sorting and Helpers in the laboratiorial exercise

Refactoring by DelegationClass ComputeStuff1{

Compute compVar;

superCompute1(){compVar.computeAlot();

}....

}

Class ComputeStuff2{

...somethingElse(){

compVar.computeAlot();}...

}

Class Compute{...computeAlot(){

compute1();compute2();compute3();

}...

}

Support in Netbeans

Refactor Classes and Interfaces Renames the class and updates all

references in project to that class Encapsulation of fields

Create accessors and mutators (”setters” and ”getters” in Netbeans)

updates all references to that field or method

The Applets Design Pattern

Java applets can be embedded in web pages, they are not standalone applications

Applets are downloaded from a web server and executed on the client in a web browser or appletviewer

Applets must extend the Applet class Can be found in java.applet package

Interaction between the context and applet

An applet interacts with the context according to a contractual interface Init() – initialize the applet when it is

initially loaded Start() – activates the applet and is

invoked when entering the webpage Stop() – deactivates the applet Destroy() - destroys the applet when the

webpage is discarded

Simple animation applet

The applet displays the current time HH:MM:SS

In order draw on the screen we must overload the paint() method

Requires a Thread to control the Animation

Problem: We can’t extend both Applet and Thread...

The Runnable Interface The Solution: Java provides the Runnable

Interface that is implemented by a class that should run in a Thread

Classes that implements Runnable interface can pass itself as argument to the Thread() constructor

We need to define the run() method that is invoked when starting the Thread

The start() method

public void start(){if(clockThread != null){

clockThread = new Thread(this);clockThread.start();

}}

Public void stop()clockThread = null;

Calls run()!

Kill the Thread!

Controlling the Animation -the run() method

public void run(){while(Thread.currentThread() == clockThread){

repaint();try{Thread.currentThread().sleep(1000);}catch(InterruptedException e){};

}}

Calls paint() to draw the time on the screen

Sleep 1 second before redrawing

Drawing the time on screen- the paint() method

Public void paint(graphics g){...g.setFont(font);g.setColor(color);g.drawString(hour + ”:” + minute + ”:” + second);

}

Idiom: Animation Applet An Idiom: ”How we can program a

template that can be reused for a recurring problem” It should be possible to customize and adapt for

a specific problem Commonly, applets produce some graphical

output that changes without interaction from the user (animation)

Using the Animation Applet Idiom, we can extend AnimationApplet and just redefine the paint method

Double buffering If painting directly on the screen, it

will ”flicker” Double buffering can be used to solve

this problem Double buffering means that we first

invisibly draw each part of the screen in the background (memory)

Then this picture can be drawn entirely at once

Double buffering When calling repaint(); it will in turn call the

update(); method update(); will clear the screen using the

background color and call the paint method paint(Graphics g);

Solution: To avoid the the ”flicker”, we simply need to override the update method!

Instead of clearing, let update(); paint the double buffered image!

A Generic Double buffered Animation Applet

What do we need to do? We need to create a background image. We need to override the update() method. We need a method to do the drawing in the

background image. It seem very similar to the Animation Applet In fact, we can extend and reuse the

Animation Applet code!

DoubleBuffered Animation Applet – Refactoring by Inheritance

Public class abstract DBAnimationApplet extends AnimationApplet{...Graphics backGraphics;Image backImage;Dimension dim;Boolean doubleBuffered;...

}

The init() method

Public final void init(){dim = getSize();backImage = new Image(dim.width, dim.height);backGraphics = backImage.getGraphics();initAnimator();

}

Protected void initAnimator(){}

The update() method...Public final void update(Graphics g){

if(doubleBuffered){paintFrame(backGraphics);g.drawImage(backImage,0,0,this);

}elsesuper.update();

}

Public void paint(Graphics g){paintFrame(g);

}

The constructors

Protected DBAnimationApplet(boolean db){this.doubleBuffered = db;

}

Protected DBAnimationApplet(){this.doubleBuffered = true;

}

How do we use the DoubleBuffered Animation Applet

Extend the DBAnimationApplet Use the appropriate constructor to

choose doublebuffer/not doublebuffer Define the abstract paintFrame()

method ...and that is it