DOC PREVIEW
UNI CS 4550 - Mechanisms for Software Reuse

This preview shows page 1-2-17-18-19-36-37 out of 37 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 37 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Session 17An ExerciseA Possible SolutionA New WrinkleA New SolutionHow Good is Our Solution?Slide 7“So what? It works.”The Full Ball HierarchyAn Alternative SolutionSlide 11Using DeceleratingBallsSlide 13Slide 14How Good is Our New Solution?Slide 16The New Ball HierarchySlide 18Slide 19Using An ExpandingBallDo You Recognize a Pattern?Slide 22How a Decorator Works...The Decorator PatternThe Problem is Prevalent...Java I/O: Example of Combining Inheritance and CompositionSlide 27Slide 28The SolutionHow Does the Decorator Pattern Work?Slide 31When to Use a Decorator?Slide 33Section 10.5 Novel Forms of Software ReuseDynamic Composition – Frog classSlide 36Slide 37Session 17Chapter 10: Mechanisms for Software ReuseAn ExerciseThe physics department would like for us to write a simple “ball world” program that it can use to teach concepts of friction. In this program, we need for some MovableBalls to decelerate. Every time one of these decelerating balls moves, its speed decreases by 5%.Add a DeceleratingBall class to the Ball hierarchy for this purpose.A Possible Solutionpublic class DeceleratingBall extends MovableBall {public DeceleratingBall( int x, int y, int r, double dx, double dy ) {super( x, y, r, dx, dy );}public void move() {super.move();setMotion( xMotion() * 0.95, yMotion() * 0.95 );}}We create a DeceleratingBall just as we would a MovableBall:DeceleratingBall b = new DeceleratingBall( 10, 15, 5, 5.0, 10.0 );A New WrinkleRunning the program, we realize that some decelerating balls need to bounce off the walls they hit, too. So we need a class of BoundedBalls that decelerate.Can you fix the problem?A New Solutionpublic class DeceleratingBoundedBall extends BoundedBall {public DeceleratingBoundedBall(int x, int y, int r,double dx, double dy, Frame f ){super( x, y, r, dx, dy, f );}public void move() {super.move();setMotion( xMotion() * 0.95, yMotion() * 0.95 );}}How Good is Our Solution?Our approach to this family of problems is straightforward: implement a decelerating version of any Ball class that needs a decelerating counterpart.What are the strengths of this approach?• It is simple.• It is easy to implement right now.How Good is Our Solution?What are the weaknesses of this approach?• It is tedious.• It repeats codes. The move() methods in the classDeceleratingBall and the class Decelerating-BoundedBall are identical!You may be asking yourself, “So what? It works.”“So what? It works.”What happens if we need to change the deceleration factor, say, from 95% to 80%?•We must remember to make the change in two different classes.What happens if we need to add deceleration behavior to other MovableBalls?•More subclasses!What happens if we need to add another kind of behavior to our ball classes, including the decelerating balls?•Even more subclasses!Solutions that make future extensions to the system unbearable are probably not very good solutions at all...The Full Ball HierarchyAn Alternative SolutionBoundedBalls respond to the same set of messages as MovableBalls. So they are substitutable for one another. Can we use this to our advantage?public class DeceleratingBall extends MovableBall {private MovableBall workerBall;public DeceleratingBall( MovableBall aBall ) {super();workerBall = aBall;}public void move() {workerBall.move();workerBall.setMotion(workerBall.xMotion() * 0.95,workerBall.yMotion() * 0.95 );}An Alternative Solution// *** ALL OTHER MESSAGES ARE DELEGATED// *** DIRECTLY TO THE INSTANCE VARIABLE!public void paint( Graphics g ) {workerBall.paint( g );}public void setColor( Color newColor ) {workerBall.setColor( newColor );}protected int radius() {return workerBall.radius();}...// -------------------------------------------protected double xMotion() {return workerBall.xMotion();}...}Using DeceleratingBallsNow, we create a DeceleratingBall by giving it a MovableBall to direct:DeceleratingBall b =new DeceleratingBall(new MovableBall(10, 15, 5, 2.0, 5.0 ) );What is the advantage of this?Using DeceleratingBallsNow, we create a DeceleratingBall by giving it a MovableBall to direct:DeceleratingBall b =new DeceleratingBall(new BoundedBall(10, 15, 5, 2.0, 5.0, this ) );How Good is Our New Solution?What are the weaknesses of our new approach?• It is more complex.• Decelerating balls are a bit “bigger” and “slower” at run time.How Good is Our New Solution?What are the strengths of our new approach?• It “says it once and only once”. The move() method specific to deceleration behavior occurs in one class. The deceleration factor lives in exactly one class.• We can add deceleration behavior to any MovableBall with this same class!• We can add deceleration behavior to any future subclass of MovableBall—with no new code!!• The tedious task of writing the delegation methods can be done automatically within many OO programming tools. In any case, writing them once seems more palatable than writing multiple subclasses for deceleration throughout the hierarchy.The New Ball HierarchyAn ExerciseAdd an ExpandingBall class to the MovableBall hierarchy. An ExpandingBall becomes a little bit larger every time it moves. Use the delegation technique we used for the DeceleratingBall class.An ExerciseAdd an ExpandingBall class to the MovableBall hierarchy. An ExpandingBall becomes a little bit larger every time it moves. Use the delegation technique we used for the DeceleratingBall class.public class ExpandingBall extends MovableBall {private MovableBall workerBall;public ExpandingBall( MovableBall aBall ) {super();workerBall = aBall;}public void move() {workerBall.move();workerBall.region().height =(workerBall.region().height * 11) / 10;workerBall.region().width =(workerBall.region().width * 11) / 10;}// delegate the rest of the messages ...}Using An ExpandingBallHere’s how we might use an ExpandingBall in the MultiBallWorld:protected void initializeArrayOfBalls( Color c ) {ballArray = new MovableBall [ BallArraySize ];for (int i = 0; i < BallArraySize; i++) {ballArray[i] =new ExpandingBall(new BoundedBall(10, 15, 5, 3.0+i, 6.0-i, this) );ballArray[i].setColor( ballColor );}}Do You Recognize a Pattern?We added flexibility and extensibility to our system by combining composition and inheritance.• substitution• delegation• recursionThe new twist in this solution is that DeceleratingBall and ExpandingBall use substitution on a class in their own class hierarchy!This new twist is so common that it has its own name:


View Full Document

UNI CS 4550 - Mechanisms for Software Reuse

Download Mechanisms for Software Reuse
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Mechanisms for Software Reuse and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Mechanisms for Software Reuse 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?