Skip to content

ITupleFactory Interface

sequenze edited this page Jul 4, 2017 · 84 revisions

Defines the operations neccessary to create custom tuples used by the concrete spaces.

Inheritance Hierarchy

Object

Syntax

public abstract interface ITupleFactory

Methods

Name                         Description
Create(Object[]) Returns a new instance of a tuple.

Remarks

The ITupleFactory allows one to create custom Tuples and use these in a more traditional OOP approach. Whilest the basic Tuple class only gives access to the underlying fields, a custom Tuple can encapsulate the fields using properties and methods.

Examples

The following example demonstrates one example of using custom tuples in conjunction with a tuple factory. The program allows shows how two different vehicles can be created and stored within the tuple space. By using a tuple factory, one can create more advanced tuple entities allowing a more clean code.

    // Tuple type identifiers used by the VehicleFactory
    public static class VehicleTypes
    {
        public const int Car = 0;
        public const int Bus = 1;
        public const int Airplane = 2;
        public const int Bicycle = 3;
        public const int MotorCycle = 4;
    }

    // Custom factory for instantiation of different vehicles.
    public class VehicleFactory : ITupleFactory
    {
        public ITuple Create(params object[] fields)
        {
            switch ((int)fields[0])
            {
                case VehicleTypes.Car: return new Car(fields);
                case VehicleTypes.Airplane: return new Airplane(fields);
                default: return new dotSpace.Objects.Space.Tuple(fields);
            }
        }
    }

    // Custom Car tuple implementing the ITuple interface
    public class Car : ITuple
    {
        public Car(params object[] fields)
        {
            this.Fields = fields;
        }

        public object this[int idx]
        {
            get { return this.Fields[idx]; }
            set { this.Fields[idx] = value; }
        }

        public object[] Fields { get; set; }
        public int Size { get { return this.Fields.Length; } }

        // Define properties to read access of the underlying values.
        // The properties could allow writing also by using the 'set' accessor.
        public string Brand { get { return (string)this[1]; } }
        public int TopSpeed { get { return (int)this[2]; } }
        public string FuelType { get { return (string)this[3]; } }
    }

    // Custom Airplane tuple implementing the ITuple interface
    public class Airplane : ITuple
    {
        public Airplane(params object[] fields)
        {
            this.Fields = fields;
        }

        public object this[int idx]
        {
            get { return this.Fields[idx]; }
            set { this.Fields[idx] = value; }
        }

        public object[] Fields { get; set; }
        public int Size { get { return this.Fields.Length; } }

        // Define properties to read access of the underlying values.
        // The properties could allow writing also by using the 'set' accessor.
        public string Brand { get { return (string)this[1]; } }
        public int TopSpeed { get { return (int)this[2]; } }
        public string FuelType { get { return (string)this[3]; } }
        public int Length { get { return (int)this[4]; } }
        public int Wingspan { get { return (int)this[5]; } }
        public int NrEngines { get { return (int)this[6]; } }
    }

    static void Main(string[] args)
    {
        // Create a new FIFO space, and pass the custom TupleFactory. 
        // Had the custom factory been omitted, the default factory would have been used in stead.
        // The default factory does not allow custom tuples.
        FifoSpace space = new FifoSpace(new VehicleFactory());

        // Create a new car and a cew airplane
        space.Put(VehicleTypes.Car, "Toyota", 175, "Diesel");
        space.Put(VehicleTypes.Airplane, "Airbus", 925, "HighOctane", 92, 81, 4);

        // Retrieve the tuple we just created
        Car car = (Car)space.Get(VehicleTypes.Car, typeof(string), typeof(int), typeof(string));
        Airplane airplane = (Airplane)space.Get(VehicleTypes.Airplane, typeof(string), typeof(int), typeof(string), typeof(int), typeof(int), typeof(int));
        
        // Print their underlying values to the console using their properties.
        Console.WriteLine("CAR: {0} {1} {2}", car.Brand, car.FuelType, car.TopSpeed);
        Console.WriteLine("AIRPLANE: {0} {1} {2} {3} {4} {5}", airplane.Brand, airplane.FuelType, airplane.TopSpeed, airplane.NrEngines, airplane.Wingspan, airplane.Length);
        Console.Read();
    }
    /*
    The following is printed to the console:
    CAR: Toyota Diesel 175
    AIRPLANE: Airbus HighOctane 925 4 81 92
    */

Clone this wiki locally