home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


Comments

In step 1, you declared a Vehicle thing to serve as a base thing. This base thing functions as a template for all subsequent descendents of Vehicle; you do not intend to create objects of type Vehicle. This is not always the case when defining classes for an application. You might find that defining a lone thing suffices to represent some entity. Do not use inheritance just for the sake of using inheritance. No requirement of the object-oriented paradigm states you must always create an inheritance tree.

It is important to remember that you should only provide a minimal interface for a thing. An overabundant interface is as bad a design as an inadequate interface.

2.3 Learn the concept of encapsulation?

Problem

I want to extend my knowledge of object-oriented concepts. I am familiar with inheritance and am ready to learn about encapsulation. I hear encapsulation is an important principle of object-oriented programming and I want to take advantage of its traits.

Technique

As with any programming concept (especially object orientation), the technique required is quite easy. The technique is to begin with something simple and build upon that. You will continue the use of the definitive language outlined in How-To 2.2.

In the following “Steps” section, you will use the Vehicle thing from How-To 2.2. Vehicles have various (physical) parts, so those parts will be described by the parts keyword.

Steps

1.  Declare parts for the Vehicle thing. Those parts represent the state of an object at some point in time.
thing Vehicle
    exposed service powerSwitch
    exposed service accelerate
    exposed service decelerate
    inherit part speed
    inherit part isMoving
    inherit part available
2.  Now provide services for the Vehicle, so users can obtain the values of Vehicle parts.
thing Vehicle
    exposed service powerSwitch
    exposed service accelerate
    exposed service decelerate
    inherit part speed
    inherit part isMoving
    inherit part available
    exposed service getSpeed
    exposed service isMoving
    exposed service getAvailable
3.  Now specify services for Vehicle, so that users can change the values of this Vehicle parts.
thing Vehicle
    exposed service powerSwitch
    exposed service accelerate
    exposed service decelerate
    inherit part speed
    inherit part isMoving
    inherit part available
    exposed service getSpeed
    exposed service isMoving
    exposed service getAvailable
    exposed service setSpeed
    exposed service setAvailable

How It Works

Encapsulation specifies the containment of functionality (services) and parts within a thing. The concept specifies that you should hide parts within a thing. Only the thing should know about its internal parts.

Another aspect of encapsulation is maintaining responsibility for a thing’s parts and services. Each thing has a contract stating a responsibility to its clients. A thing should be able to maintain its own existence—that is, to stand on its own. A thing is considered cohesive if it can fully encapsulate itself.

You must remember to protect the internal state of your objects; if you don’t, those objects will become unstable or corrupt. An unstable object is untrustworthy; you can never count on the state of a corrupt object.

Comments

Inheritance and encapsulation are very important with respect to object-oriented principles. However, they are but two important concepts. A third concept, namely polymorphism, is addressed in the next How-To.

2.4 Learn the concept of polymorphism?

Problem

I have a good understanding of inheritance and encapsulation and feel I have to grasp the concept of polymorphism to round out my knowledge of object-oriented technology.

Technique

Of the three object-oriented principles, polymorphism is probably the most difficult to comprehend. Effective polymorphic behavior relies on proper implementation of inheritance and encapsulation. The technique for ensuring effective polymorphic behavior is to fully understand its intent and when polymorphism should and should not be used.

Returning to the Vehicle example from previous How-Tos, let’s apply polymorphism to the picture.

Steps

1.  The declaration of Vehicle is reproduced here:
thing Vehicle
    exposed service powerSwitch
    exposed service accelerate
    exposed service decelerate
    inherit part speed
    inherit part isMoving
    inherit part available
    exposed service getSpeed
    exposed service isMoving
    exposed service getAvailable
    exposed service setSpeed
    exposed service setAvailable
2.  Next, the declaration of MotorVehicle:
thing MotorVehicle descendantOf Vehicle
      exposed service powerSwitch
      exposed service accelerate
      exposed service decelerate
      inherit part speed
      inherit part isMoving
      inherit part available
      exposed service getSpeed
      exposed service isMoving
      exposed service getAvailable
      exposed service setSpeed
      exposed service setAvailable
      exposed service steering
3.  The following is the declaration for Truck:
thing Truck descendantOf MotorVehicle
    exposed service powerSwitch
    exposed service accelerate
    exposed service decelerate
    inherit part speed
    inherit part isMoving
    inherit part available
    exposed service getSpeed
    exposed service isMoving
    exposed service getAvailable
    exposed service setSpeed
    exposed service setAvailable
    exposed service steering
    exposed service fourWheelDrive
4.  The following is a snippet of code using the pseudo-language:
Truck aTruck := create Truck.
aTruck->setSpeed(55).
Vehicle baseVehicle := aTruck.
integer speed := baseVehicle->getSpeed.
Print(speed).
Print(aTruck->getSpeed).
MotorVehicle anotherVehicle := create MotorVehicle.
anotherVehicle->setSpeed(40).
baseVehicle := anotherVehicle.
Print(baseVehicle->getSpeed).
baseVehicle->setSpeed(85).
Print(anotherVehicle->getSpeed).
5.  The output from this program is
55
55
40
85


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.