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

Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

Jan 01, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

Object Oriented Programming

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

Page 2: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

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...

Page 3: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

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

Page 4: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

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

Page 5: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

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...

Page 6: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

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

Page 7: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

Refactoring of methodsClass ComputeThings {

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

}public void computeMany2(){

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

}}

These parts are similar....

Page 8: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

Refactoring of methodsClass computeThings {

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

}public void computeMany2(){

something();computeAlot();

}public void computeAlot(){

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

}}

Page 9: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

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

Page 10: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

Refactoring by Inheritance

Class computeStuff1{superCompute1(){

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

}....

}

Class computeStuff2{somethingElse(){

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

}...

}

Both classes uses

identical code

sections

Page 11: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

Refactoring by InheritanceClass ComputeStuff1 extends

Common{...superCompute1(){

super.computeAlot();}....

}

Class ComputeStuff2 extends

Common{...somethingElse(){

super.computeAlot();}...

}

Class Common{...computeAlot(){

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

}...

}

Page 12: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

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

Page 13: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

Refactoring by DelegationClass ComputeStuff1{

Compute compVar;

superCompute1(){compVar.computeAlot();

}....

}

Class ComputeStuff2{

...somethingElse(){

compVar.computeAlot();}...

}

Class Compute{...computeAlot(){

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

}...

}

Page 14: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

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

Page 15: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

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

Page 16: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

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

Page 17: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

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...

Page 18: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

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

Page 19: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

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!

Page 20: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

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

Page 21: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

Drawing the time on screen- the paint() method

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

}

Page 22: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

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

Page 23: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

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

Page 24: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

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!

Page 25: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

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!

Page 26: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

DoubleBuffered Animation Applet – Refactoring by Inheritance

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

}

Page 27: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

The init() method

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

}

Protected void initAnimator(){}

Page 28: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

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);

}

Page 29: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

The constructors

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

}

Protected DBAnimationApplet(){this.doubleBuffered = true;

}

Page 30: Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise

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