Download presentation
Presentation is loading. Please wait.
Published byGwendoline Chandler Modified over 9 years ago
1
Object Oriented Programming Lecture 4: Refactoring, An Applet Example, Idiom - Animation applets, Introduction to the Laboratorial exercise www2.hh.se/staff/jebe/oop2005/
2
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...
3
Shadowing of variables Public class { int myVariable;.... public doStuff(int test){ int myVariable; myVariable = test; this.MyVariable = test; } Inner scope myVariable shadows the outer scope myVariable global scope local scope
4
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
5
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...
6
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
7
Refactoring of methods Class ComputeThings { public void computeMany1(){ anything(); compute1(); compute2(); compute3(); } public void computeMany2(){ something(); compute1(); compute2(); compute3(); } These parts are similar....
8
Refactoring of methods Class computeThings { public void computeMany1(){ anything(); computeAlot(); } public void computeMany2(){ something(); computeAlot(); } public void computeAlot(){ compute1(); compute2(); compute3(); }
9
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
10
Refactoring by Inheritance Class computeStuff1{ superCompute1(){ compute1(); compute2(); compute3(); }.... } Class computeStuff2{ somethingElse(){ compute1(); compute2(); compute3(); }... } Both classes uses identical code sections
11
Refactoring by Inheritance Class ComputeStuff1 extends Common{... superCompute1(){ super.computeAlot(); }.... } Class ComputeStuff2 extends Common{... somethingElse(){ super.computeAlot(); }... } Class Common{... computeAlot(){ compute1(); compute2(); compute3(); }... }
12
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
13
Refactoring by Delegation Class ComputeStuff1{ Compute compVar; superCompute1(){ compVar.computeAlot(); }.... } Class ComputeStuff2{... somethingElse(){ compVar.computeAlot(); }... } Class Compute{... computeAlot(){ compute1(); compute2(); compute3(); }... }
14
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
15
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
16
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
17
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...
18
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
19
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!
20
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
21
Drawing the time on screen - the paint() method Public void paint(graphics g){... g.setFont(font); g.setColor(color); g.drawString(hour + ”:” + minute + ”:” + second); }
22
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
23
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
24
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!
25
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!
26
DoubleBuffered Animation Applet – Refactoring by Inheritance Public class abstract DBAnimationApplet extends AnimationApplet{... Graphics backGraphics; Image backImage; Dimension dim; Boolean doubleBuffered;... }
27
The init() method Public final void init(){ dim = getSize(); backImage = new Image(dim.width, dim.height); backGraphics = backImage.getGraphics(); initAnimator(); } Protected void initAnimator(){}
28
The update() method... Public final void update(Graphics g){ if(doubleBuffered){ paintFrame(backGraphics); g.drawImage(backImage,0,0,this); }else super.update(); } Public void paint(Graphics g){ paintFrame(g); }
29
The constructors Protected DBAnimationApplet(boolean db){ this.doubleBuffered = db; } Protected DBAnimationApplet(){ this.doubleBuffered = true; }
30
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.