Chapter 10: Mechanisms for Software Reuse

Slides:



Advertisements
Similar presentations
Decorator Pattern Applied to I/O stream classes. Design Principle Classes should be open for extension, but closed for modification –Apply the principle.
Advertisements

Session 9 MultiballWorld, Refactoring Ball using Inheritence, and Intro. to CannonWorld.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
CSSE501 Object-Oriented Development
Design Patterns.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Java exercise review Lesson 25: Exercise 1, 2 and 3.
Session 21 Chapter 10: Mechanisms for Software Reuse.
Session 11 Border Layout, using Panels, Introduction to PinBallGame.
Session 15 Chapter 8: Understanding Inheritance. Lab Exercise Solution Recall that we needed to know if a mousePress occurred on a target. We tried to.
Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Object Oriented.
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Session 19 Chapter 10 – Mechanisms for Software Reuse.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
Session 18 Chapter 8: Understanding Inheritance. Recall Exercise 2 From Tuesday It’s very annoying to move a target from the pallet and drop it in the.
Session 13 Pinball Game Construction Kit (Version 3):
Session 25 Test 2 Review. Test 2 Details Tuesday, November 22 in class –(Anybody planning on taking it on Friday?) Closed book Closed notes, except for.
BallWorld.java Ball.java A functional walkthrough Part 3: the interaction of objects.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Lecture 14 Inheritance vs Composition. Inheritance vs Interface Use inheritance when two objects share a structure or code relation Use inheritance when.
Session 18 Lab 9 Re-cap, Chapter 12: Polymorphism & Using Sound in Java Applications.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
Java exercise review Lesson 26: Exercise 4. Exercise #4 – a sample solution 1.Write the psudocode and the deployment diagram for what you are planning.
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
C10, Mechanisms for Software Reuse. Substitutability A variable declared as one type holds a value of another type In Java either: Subclass: allPiles.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Session 8 Lab 4 comments, MultiballWorld, Refactoring Ball using Inheritence, and Intro. to CannonWorld.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
A structured walkthrough
Modern Programming Tools And Techniques-I
OOP - Object Oriented Programming
Design Patterns: MORE Examples
Object-Oriented Design
Visit for more Learning Resources
Lecture 12 Inheritance.
Inheritance Based on slides by Ethan Apter
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Inheritance and Polymorphism
Chapter 11 Object-Oriented Design
Java Beans Sagun Dhakhwa.
Week 4 Object-Oriented Programming (1): Inheritance
CS2102: Lecture on Abstract Classes and Inheritance
MSIS 670 Object-Oriented Software Engineering
The Object-Oriented Thought Process Chapter 07
Decorator Pattern Richard Gesick.
Inheritance Inheritance is a fundamental Object Oriented concept
More About Inheritance & Interfaces
Focus of the Course Object-Oriented Software Development
Delegation vs inheritance
7. Decorator SE2811 Software Component Design
Introduction to Data Structure
Border Layout, using Panels, Introduction to PinBallGame
Review of Previous Lesson
Review of Previous Lesson
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Chapter 10: Mechanisms for Software Reuse Session 17 Chapter 10: Mechanisms for Software Reuse

An Exercise The 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 Solution public 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 Wrinkle Running 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 Solution public 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 class DeceleratingBall 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 Hierarchy

An Alternative Solution BoundedBalls 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 DeceleratingBalls Now, 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 DeceleratingBalls Now, 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 Hierarchy

An Exercise Add 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 Exercise Add 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 ExpandingBall Here’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 • recursion The 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: decorator.

But here’s a beautiful example of what using a decorator can do for you: protected void initializeArrayOfBalls( Color ballColor ) { ballArray = new MovableBall [ BallArraySize ]; for (int i = 0; i < BallArraySize; i++) { ballArray[i] =new ExpandingBall( new DeceleratingBall( new BoundedBall( 10, 15, 5, 3.0+i, 6.0-i, this))); ballArray[i].setColor( ballColor ); } Since a decorator is substitutable for instances of its base class, you can decorate a decorator!

How a Decorator Works...

The Decorator Pattern The Problem We would like to add a behavior to a set of classes that share a common interface. A Tempting Solution that Fails Use inheritance to create a new class of objects that has the behavior. Use instances of this class when you need the behavior, and use instances of the superclass otherwise. This solution is impractical. Why? We will need to create multiple subclasses and replicate the behavior in each. What if we would like to add more behavior to the extended object? We have to make (many!) more subclasses!

The Problem is Prevalent... Why Does This Problem Matter? It occurs in many domains and in many applications: • We want to add features to individual balls in our ball games or to individual card piles in our card games. • We want to add features to individual streams in the Java library, such as buffering the input we read from a stream. • We want to add windowing features to individual objects in a word processor or drawing program.

Java I/O: Example of Combining Inheritance and Composition An abstract concept of reading a stream of bytes in sequence – the class InputStream Several concrete realizations that differ in their data source: ByteArrayInputStream – array of bytes FileInputStream – external file PipedInputStream – another process generates a stream of bytes SequenceInputStream ObjectInputStream

Java I/O: Example of Combining Inheritance and Composition Each is declared as a subclass of InputStream, so any can be substituted for type InputStream Programs using InputStream can be written to process a stream of bytes independent of the data source

Java I/O: Example of Combining Inheritance and Composition However, additional functionality that’s independent of the data source is often needed, e.g., line numbers, buffering to allow rereading These features are provided by defining a subclass FilterInputStream of InputStream FilterInputStream can hold an InputStream component as its data source. Therefore, it’s substitutable for InputStream and can augment any type of data source Example of a decorator (or filter or wrapper) design pattern

The Solution Create a decorator class. 1. Encapsulate an instance of the base class as an instance variable of the decorator. Implement the new behavior in the decorator. Delegate as much of the new behavior to the instance variable as possible. Send all other messages recursively to the encapsulated object. 2. Use inheritance to extend the decorator class from the contained class.

How Does the Decorator Pattern Work? This is a second example of using inheritance and composition together. Inheritance creates substitutable classes. This allows a decorated object to be used in all the same places as the encapsulated object! The superclass acts as an interface for all of its subclasses.

How Does the Decorator Pattern Work? An application won’t know—or need to know—what sort of MovableBall it is using; the ball responds to all the same messages. Composition uses substitution to reuse the code in the base class, but in a way that is controlled by the decorator. This allows us to add the same behavior to all of the classes in the hierarchy!

When to Use a Decorator? In a way, the decorator pattern allows us to add new behavior to a single instance, rather than to the whole class. This works well whenever the new behavior is orthogonal to the existing behaviors, that is, it is related to the existing behaviors but does not fit in the current way we break things up. For example, we can’t point to a particular place in the Ball hierarchy and say, “That’s where deceleration belongs!” Deceleration cuts across the class hierarchy.

When to Use a Decorator? Using a decorator makes sense any time there are two varieties of some object, but the variations are not directly related. • balls that bounce of the walls and balls that decelerate Would implementing BoundedBall as a decorator be a good idea?

Section 10.5 Novel Forms of Software Reuse Dynamic Composition with inheritance, the link between child and parent class is established at compile time with composition, the link between the new abstraction and old can be changed at run-time Example: Frog class that changes behavior as it grow from a tadpole to an adult

Dynamic Composition – Frog class class Frog { private FrogBehavior behavior; public Frog() { behavior = new TadpoleBehavior(); } public void grow () { //see if behavior should change if (behavior.growUp()) behavior = new AdultFrogBehavior();

Dynamic Composition – Frog class abstract class FrogBehavior { public boolean growUp() { return false; } public void grow (); public void swim (); } class TadpoleBehavior extends FrogBehavior { private int age = 0; public boolean growUp() { if (++age > 24) return true; public void grow() {...} public void swim() {...} class AdultFrogBehavior extends FrogBehavior {

Section 10.5 Novel Forms of Software Reuse Inheritance of Inner Classes as in listener classes Unnamed Classes – difficult to read, so I’d recommend avoiding