Scala Cake Pattern Simplified for Java

70 views
Skip to first unread message

William la Forge

unread,
Nov 9, 2009, 9:26:22 PM11/9/09
to AgileWikiDevelopers
The Scala Cake pattern is important for Rolonic Software Engineering, as it can be used to implement the elements of a Rolon. But it doesn't really need multiple inheritance to implement. It is really just a invariant of aggregation which makes use of uninitialized constants--which Java also supports. Here's how to implement a 3-layer cake using Java:

public class Component {
  public final A a;
  public final B b;
  public final C c;

  //_configure can only be executed once without raising an exception
  protected void _configure(A a, B b, C c) {
    this.a = a; this.b=b; this.c=c; 
  }

  public void configure(A a, B b, C c) {
    if (this instanceof A) {
      a = this;
    } else if (a==null) {
      a = new A();
    }
    if (this instanceof B) {
      b = this;
    } else if (b==null) {
      b = new B();
    }
    if (this instanceof C) {
      c = this;
    } else if (c==null) {
      c = new C();
    }
    a._configure(a,b,c);
    b._configure(a,b,c);
    c._configure(a,b,c);
  }
}

public class A extends Component {
  protected String fun;

  public String getFun() {return fun;}
  public void setFun(String fun) {this.fun = fun;}
}

public class B extends Component {
  public String games() {return a.fun()+" World!"}
}

public class C extends Component {
}

//sample code
C s = new C();
s.configure(null,null,null);
s.a.setFun("Hello");
System.out.println(s.b.games());

Reply all
Reply to author
Forward
0 new messages