#header-inner {background-position: right !important; width: 100% !important;}

11/14/14

LightPoint as an Object.

LightPoint is a part of Stitie Machine 1.1 'Sunsail'.

LightPoint is (State, Strategy) pair, which is equivalent to an Object as in Object Oriented Programming - under condition that Strategy has access to a State.

In Stitie Machine 1.1.34-RC-0-0-3 'Sunsail', there's LightPointObject class that can be used this way:

----
final Certificate cert = new Certificate();

// test #1 : without 'superclass'.
final LightPointObject lpo = new LightPointObject(new HashMapStateImpl(), null);
lpo.addMethod(cert, "add", new Strategy() {
  @Override
  public void execute(Certificate cert) {
    Integer[] args = (Integer[]) lpo.getArgs(cert);
    int result = args[0] + args[1];
    lpo.setReturnValue(cert, result);
  }
  @Override
  public int getPriority(Certificate cert) {
    return 0;
  }
});

lpo.addMethod(cert, "sub", new Strategy() {
  @Override
  public void execute(Certificate cert) {
    Integer[] args = (Integer[]) lpo.getArgs(cert);
    int result = args[0] - args[1];
    lpo.setReturnValue(cert, result);
  }
  @Override
  public int getPriority(Certificate cert) {
  return 0;
  }
});

lpo.setArgs(cert, new Integer[] {3,2});
System.out.println("test #1: without superclass.");

lpo.setMethodToCall(cert, "add");
lpo.execute(cert);
int result = (int) lpo.getReturnValue(cert);
System.out.println("add operation call's result: " + result);

lpo.setMethodToCall(cert, "sub");
lpo.execute(cert);
result = (int) lpo.getReturnValue(cert);
System.out.println("sub operation call's result: " + result);

// test #2: with superclass

LightPointObject lpoSubclass = new LightPointObject(new HashMapStateImpl(), lpo);
lpoSubclass.addMethod(cert, "sub", new Strategy() {
  @Override
  public void execute(Certificate cert) {
    lpoSubclass.setReturnValue(cert, 0);
    // weird value - used only for testing of method inheritance & polymorphism
  }
  @Override
  public int getPriority(Certificate cert) {
    return 0;
  };
});

LightPointObject lpoSubclass2 = new LightPointObject(new HashMapStateImpl(), lpo);
lpoSubclass2.addMethod(cert, "add", new Strategy() {
  @Override
  public void execute(Certificate cert) {
    lpoSubclass2.setReturnValue(cert, 20);
    // weird value - used only for testing of method inheritance & polymorphism
  }
  @Override
  public int getPriority(Certificate cert) {
    return 0;
  }
});

lpoSubclass2.addMethod(cert, "sub", new Strategy() {
  @Override
  public void execute(Certificate cert) {
    lpoSubclass2.setReturnValue(cert, 10);
    // weird value - used only for testing of method inheritance & polymorphism
  }
  @Override
  public int getPriority(Certificate cert) {
    return 0;
  }
});

System.out.println("test #2: with 'superclass'.");

lpoSubclass.setArgs(cert, new Integer[] {3,2});
lpoSubclass2.setArgs(cert, new Integer[] {3,2});

// testing calling method from superclass. (subclass has it not defined).
lpoSubclass.setMethodToCall(cert, "add");
lpoSubclass.execute(cert);
result = (int) lpoSubclass.getReturnValue(cert);
System.out.println("add operation call's result: " + result);

// testing method overriding & polymorphism.
lpoSubclass.setMethodToCall(cert, "sub");
lpoSubclass.execute(cert);
result = (int) lpoSubclass.getReturnValue(cert);
System.out.println("sub operation call's result: " + result);

// testing calling method from superclass. (subclass has it defined).
lpoSubclass2.setMethodToCall(cert, "add");
lpoSubclass2.execute(cert);
result = (int) lpoSubclass2.getReturnValue(cert);
System.out.println("add operation call's result: " + result);

// testing method overriding & polymorphism.
lpoSubclass2.setMethodToCall(cert, "sub");
lpoSubclass2.execute(cert);
result = (int) lpoSubclass2.getReturnValue(cert);
System.out.println("sub operation call's result: " + result);

// in real programming, unlike what's shown in this exercise,
// separate superclass object copy should be made for each of subclass objects,
// to prevent 'state overlapping' & state overwriting.


----

more details - in the code.

See also: Stitie Machine 1.1 'Sunsail'.

No comments:

Post a Comment