<< Chapter < Page Chapter >> Page >
The module uses the Ballworld program to demonstrate key ideas in abstract classes, polymorphism, inheritance and other coding techniques. In particular, this module will contrast a composition-based architecture with an inheritance-based architecture.

In this module we will explore what is gained by modifying the inheritance-based Ballworld system into a composition-based system.

In the inheritance-based Ballworld system, we were able to generate quite a bit of flexibility and extensibility. For instance, we could develop new kinds of balls and add them into the system without recompiling the rest of the code. This was accomplished by having the invariant ball creation and management code deal only with the abstract ball while the variant behaviors were encapsulated into the concrete subclasses.

The problem with inheritance

Inheritance seems to work quite well, but suppose we want to do more than just put different kinds of balls on the screen? What if we wanted to be able to change how a ball behaves, after it has been created? What if we want create balls that do a multiple of different behaviors, such as change color and radius? While working solutions using an inheritance-based system do exist, they are cumbersome, inefficient and most importantly, inconsistent with any sort of clear abstract model of what balls should be like.

The problem lies in the very nature of inheritance. When we attempted to separate the variant from the invariant behaviors, we overlooked a crucial aspect of inheritance. In our model, the superclass represented the invariant behaviors of a ball while the subclasses represented the variant behaviors. The separation seemed clear enough in the UML diagram, except that when one has an actual object instance , both the superclass and subclass behaviors are bound into a single entity. A ball object cannot change its variant updateState behavior because it is inextricably bound with to the invariant behaviors. A ball object cannot be composed of multiple updateState behaviors because that code cannot be isolated from the rest of the ball's code. If you want a curving behavior, you have to get it packaged in a whole ball object--you can't get just the behavior.

A clear sympton of this problem is the common code to call the superclass constructor found in all the subclasses' constructors. This tells us that the superclass is really right there in the subclass with everything else. The fact that the code is repeated from class to class says that it is invariant code in the middle of what we want to be variant code.

The inheritance-based model of Ballworld does not separate the variant and the invariant at the proper place. There is invariant code mixed together with the variant code.
That's why they can't be separated and the invariant behaviors are dragged along with the variant behaviors. This is what makes dynamically changing behaviors and multiply composed behaviors so difficult in this system.

Pizzas and shapes

To understand what we can do to remedy the problems with our inheritance-based model, let's digress for a bit and consider a simple model of pizzas. Here, we have a pizza which has a price and has a shape. A shape, be it a circle, square, rectangle of triangle, is capable of determining its own area. A pizza, when requested to calculate its price per square inch, simply takes its price and divides it by the area of its shape. To obtain that area, the Pizza delegates to the IShape , since it is the shape that knows how to calculate its area, not the pizza.

Pizzas and shapes

A pizza has-a shape, which is able to calculate its area.
Delegation is the handing of a calculation off to another object for it process. Here, the pizza is only interested in the result of the area calculation, not how it is performed.
To the pizza, the shape represents an abstract algorithm to calculate the area.
The Pizza and the IShape classes represent the invariant processes involved with calculating the price per square inch ration, while the concrete Circle , Square , Triangle and Rectangle classes represent the variant area calculations for different shapes. What wee see from this example is that
objects can be used to represent pure behavior, not just tangible entities.
Interfaces are particularly useful here as they are expressly designed to represent pure, abstract behavior.

Questions & Answers

what are the importance of studying economics
Bherla Reply
To know if the country is growing or not through the country's GDP
Ariel
What is the law of demand
Yaw Reply
price increase demand decrease...price decrease demand increase
Mujahid
ıf the price increase the demand decrease and if the demand increase the price decrease
MUBARAK
all other things being equal, an increase in demand causes a decrease in supply and vice versa
SETHUAH
yah
Johnson
how is the economy of usa now
Johnson
What is demand
jude Reply
Demand is the quantity of goods and services a consumer is willing and able to purchase at various prices over a given period of time.
Yaw
yea
SETHUAH
Okay congratulations I'll join you guys later .
Aj
yes
MUBARAK
calculate elasticity of income exercises
HABANABAKIZE Reply
If potatoes cost Jane $1 per kilogram and she has $5 that could possibly spend on potatoes or other items. If she feels that the first kilogram of potatoes is worth $1.50, the second kilogram is worth$1.14, the third is worth $1.05 and subsequent kilograms are worth $0.30, how many kilograms of potatoes will she purchase? What if she only had $2 to spend?
Susan Reply
cause of poverty in urban
DAVY Reply
QI: (A) Asume the following cost data are for a purely competitive producer: At a product price Of $56. will this firm produce in the short run? Why Why not? If it is preferable to produce, what will be the profit-maximizing Or loss-minimizing Output? Explain. What economic profit or loss will the
Falak Reply
what is money
DAVY Reply
money is any asset that is generally acceptable for the exchange of goods and for the settlement of debts
Mnoko
what is economic
Stephen Reply
economics is the study of ways in which people use resources to satisfy their wants
Falak
what is Price mechanism
Dhany Reply
what is Economics
ERNESTINA
The study of resource allocation,distribution and consumption.
Emelyn
introduction to economics
Uday Reply
welfare definition of economics
Uday
examine the wealth and welfare definitions of economics
Uday
read book by ml jhingan
Anand
What do we mean by Asian tigers
Aeesha Reply
Dm me I will tell u
Shailendra
Hi
Aeesha
hi
Pixel
What is Average revenue
KEMZO
How are u doing
KEMZO
it is so fantastic
metasebia
uday
Uday
it is a group of 4 countries named Singapore, South Korea, Taiwan and Hong Kong because their economies are growing very faster
Anand
fyn
EDWARD
Please, average revenue is an amount of money you gained after deducted your total expenditure from your total income.
EDWARD
what's a demand
Edward Reply
it is the quantity of commodities that consumers are willing and able to purchase at particular prices and at a given time
Munanag
quantity of commodities dgat consumers are willing to pat at particular price
Omed
demand depends upon 2 things 1wish to buy 2 have purchasing power of that deserving commodity except any from both can't be said demand.
Bashir
Demand is a various quantity of a commodities that a consumer is willing and able to buy at a particular price within a given period of time. All other things been equal.
Vedzi
State the law of demand
Vedzi
The desire to get something is called demand.
Mahabuba
what is the use of something should pay for its opportunity foregone to indicate?
Random Reply
Why in monopoly does the firm maximize profits when its marginal revenue equals marginal cost
astrid Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play




Source:  OpenStax, Principles of object-oriented programming. OpenStax CNX. May 10, 2013 Download for free at http://legacy.cnx.org/content/col10213/1.37
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?

Ask