Re: Entity and Identity

From: Nilone <reaanb_at_gmail.com>
Date: Sun, 9 Aug 2009 02:02:48 -0700 (PDT)
Message-ID: <46fe0c47-6f0d-4984-ba6f-1ce61b520046_at_d23g2000vbm.googlegroups.com>


As an example, let's model squares as subtypes of rectangles, and create mutable objects based on them. Syntax is made up as I go along.

type Point2D
{
  int x, y;

  Point2D(int _x, _y)
{

    x = _x;
    y = _y;
  }
}

type Size2D
{
  int width, height;

  Size2D(int _w, _h)
{

    width = _w;
    height = _h;
  }
}

type Rectangle
{
  // Attributes of a type are public
  Point2D position;
  Size2D size;

  // Only constructor can initialize value   Rectangle(int _x, _y, _w, _h)
{

    position = new Point2D(_x, _y);
    size = new Size2D(_w, _h);
  }

  // Read-only attributes only
  int Area()
{

    return size.width*size.height;
  }
}

// A square is a rectangle
type Square : Rectangle
{
  // Can't override Area()

  // Constructor required to create new type   Square(int _x, _y, _s)
{

    position = new Point2D(_x, _y);
    size = new Size2D(_s, _s);
  }
}

// Button interface exports button type and // defines behavior for all button state machines interface IButton
{
  // All Buttons must implement Move
  void Move(int newx, newy);

  // Events are functions that must be implemented   // by client. Interfaces are bidirectional.   event Clicked();
}

// RectButton is a Rectangle-typed state machine // with an IButton interface.
statemachine RectButton : IButton, Rectangle {
  RectButton(Rectangle r)
{

    state = r;
  }

  // Implementing Move
  void Move(int newx, newy)
{

    state = new Rectangle(newx, newy, state.size.width, state.size.height);
  }

  void Stretch(int addx, addy)
{

    state = new Rectangle(state.position.x, state.position.y, state.size.width+addx,

state.size.height+addy);
  }
}

// SquareButton is a Square-typed state machine // and inherits the interface of RectButton statemachine SquareButton : RectButton, Square {
  SquareButton(Square s)
{

    state = s
  }

  // Implementing Move
  void Move(int newx, newy)
{

    state = new Square(newx, newy, state.size.width);   }

  void Stretch(int adds)
{

    state.size = new Size2D(state.x2 + adds, state.y2 + adds);   }
}

void main()
{
  RectButton rb = new RectButton(10, 10, 100, 50);   SquareButton sb = new SquareButton(20, 20, 70);   IButton b = rb;
  b.move(40, 40);
  b = sb;
  b.move(60, 60);
  rb.state = sb.state;
}

It is straightforward to create polymorphically compatible mutable state machines with polymorphically compatible state. It also seems to help to prevent faulty reasoning which would have been accepted in traditional OO classes. Received on Sun Aug 09 2009 - 11:02:48 CEST

Original text of this message