|
||||||
How to Do Prototype Based Programming with IoLearn to program using Io - OOP with prototypes
How is it possible to use object oriented programming without classes? Easy - use prototypes and Io.
One of the interesting things about using classes in a programming language such as Io is that it doesn't actually use classes. Instead Io uses a concept known as prototyping, which makes Io a very easy programming language to understand. A new user can be up and running in just a matter of minutes - even if they've never written any code before. The Concept of PrototypingThe core to prototyping is cloning - a prototype object is created and then this is cloned to produce new objects. This concept can easily be demonstrated by looking at Io. Downloading IoBoth Windows and Linux can be obtained from the Io download page at http://www.iolanguage.com/downloads, and the installation is simple:
On some versions of Linux a little extra work may be needed cd <uncompressed folder>
sudo cp libiovmall.so /usr/lib/
Running IoIo can by run in any of three ways:
An Io Script: Prototypes and ClonesThe first line of any Io script should be the shebang line; this tells the system that the Io interpreter is to be used: #!/usr/local/bin/io
The key thing that this script will do is to create a prototype (in this case a car), and it's interesting to note that the car (at this stage) is just a clone of Io's default object: car := Object clone
At the moment car is just an blank object, however it is very easy to add properties and methods to the basic object: car fuel_level := 0
car fuel_capacity := 10
car add_fuel := method(
gallons_in,
gap := fuel_capacity - fuel_level
if ( gallons_in > gap,gallons_in := gap )
writeln("Adding " , gallons_in," gallons")
fuel_level = fuel_level + gallons_in
)
To make another object the programmer can clone the original and then make use of it: jeep := car clone
writeln( jeep fuel_level , " gallons")
jeep add_fuel(5)
writeln( jeep fuel_level , " gallons")
jeep add_fuel(6)
writeln( jeep fuel_level , " gallons")
If the script were to be run now then the output would be: 0 gallons
Adding 5 gallons
5 gallons
Adding 5 gallons
10 gallons
Expanding the PrototypeIn this example a simple prototype (the car) was created and cloned (the jeep), however it is still possible to add further properties and methods to the prototype: car do (
odometer_reading := 0
mpg := 30
fuelgauge := method(writeln("Fuel: ", fuel_level, " gallon"))
odometer := method (writeln ("Distance travelled: ", odometer_reading, " miles"))
travel := method (distance,
gallons_used := distance / mpg
if ( gallons_used > fuel_level,
gallons_used = fuel_level
distance = fuel_level * mpg
writeln ("Maximum distance ", distance, " miles")
)
odometer_reading = odometer_reading + distance
fuel_level = fuel_level - gallons_used
)
)
and these additions will passed on to any clones automatically: jeep fuel_capacity = 12
jeep add_fuel(12)
jeep travel (30)
jeep fuelgauge
jeep odometer
jeep travel (1000)
jeep fuelgauge
jeep odometer
with an output of: Adding 12 gallons
Fuel: 11 gallon
Distance travelled: 30 miles
Maximum distance 330 miles
Fuel: 0 gallon
Distance travelled: 360 miles
And, of course, further clones can be created: smart := car clone
smart mpg = 70
smart add_fuel(12)
smart travel (70)
smart fuelgauge
smart odometer
smart travel (1000)
smart fuelgauge
smart odometer
Adding 10 gallons
Fuel: 9 gallon
Distance travelled: 70 miles
Maximum distance 630 miles
Fuel: 0 gallon
Distance travelled: 700 miles
ConclusionIo provides an excellent introduction into prototype-based programming and provides the programmer with:
It is all so simple that even a complete novice can program with Io with complete ease.
The copyright of the article How to Do Prototype Based Programming with Io in Computer Programming Languages is owned by Mark Alexander Bain. Permission to republish How to Do Prototype Based Programming with Io in print or online must be granted by the author in writing.
Comments
Dec 27, 2008 6:04 PM
Guest :
1 Comment:
|
||||||
|
|
||||||
|
|
||||||