More Than an Ordinary Class

Consider a class Bird:


    class Bird is Spirit
    {
	function fly_to (new_position: Position)
	{
	    while ( currentPosition != new_position )
	    {
		updateCurrentPosition (new_position);
		flipWings();
		delay (500); // delay 500ms
	    }
	};
    };

And we have a flock of birds scattering in a scene with a Moon:


    object MoonAndBirds is Scene
    {
	object Moon is Spirit
	{
		position = randomPosition();
		color = "pale yellow";
	};
	flock: Birds... =  /* a list of birds */
	       ( new Bird (randomPosition()),
		 new Bird (randomPosition()),
		 new Bird (randomPosition()),
	       );
    };

Now we want to write a script for all birds in the flock to fly to the center position of the Moon, and then disappear. If we write things like:

    foreach (bird in flock)
    {
	bird.fly_to (Moon.centerPosition());
	flock.delete (bird);
    }

Do we get the expected result? No. We do not want birds to fly to the Moon one after another. Instead, we want them to fly in parallel.

The solution is easy: make the fly_to function a thread:


    class Bird is Spirit
    {
	thread fly_to (new_position: Position)
	{
	    while ( currentPosition != new_position )
	    {
		updateCurrentPosition (new_position);
		flipWings();
		wait (500); // suspend for 500ms
	    }
	};
    };

Now, consider the same script:

    foreach (bird in flock)
    {
	bird.fly_to (Moon.centerPosition());
	flock.delete (bird);
    }

We get the result we want. For each bird in the flock, we started a thread of flying, and then delete the bird. The actual destroy of the bird will not happen until the thread instance is terminated.

fly_to is a subclass of thread which is defined in Bird's superclass, Spirit. The thread class is a member class of Spirit; and fly_to is a member class of Bird. Therefore, the following code is also acceptable:


    class Bird is Spirit
    {
	class fly_to is thread
	{
	    enter (new_position: Position)
	    {   while ( currentPosition != new_position )
	        {
		    updateCurrentPosition (new_position);
		    flipWings();
		    wait (500); // suspend for 500ms
	        }
	    }
	};
    };

Statement bird.fly_to() actually created an instance of thread within the object bird. thread is a user-defined class. For this particular case, we can design a specific thread whose active instances are limited to one. This will prohibit a bird having multiple active flying activities the same time.

Using the same method, we can create various domain-specific frameworks for various applications. For examples:


	class Account is AtomicData
	{
		transaction deposit(...) {...};
		transaction withdraw(...) {...};
	};

	class NameSever is DistributedServer
	{
		remoteService register(...) {...};
		remoteService lookup(...) {...};
	};

	class Pharmacy is Task
	{
		entry drop_prescription(...) {...};
		entry take_medicine(...) {...};
	};

	class FaxMachine is ActiveObject
	{
		port recevie (...) {...};
		port send (...) {...};
	};



Exercise



[ Go back to tutorial | Tutorial home page | Transframe home page ]

More examples: