Commit 4ccbfc4a authored by Dominik Gruntz's avatar Dominik Gruntz
Browse files

06_Composite: solutions

parent 70929def
package patterns.composite.sol1;
public abstract class Figure {
public abstract void draw(String prefix);
GroupFigure parent;
}
package patterns.composite.sol1;
import java.util.LinkedList;
import java.util.List;
public class GroupFigure extends Figure {
private final String name;
private final List<Figure> figures = new LinkedList<>();
public GroupFigure(String name, Figure... figures) {
this.name = name;
for (Figure f : figures) {
addFigure(f);
}
}
public void addFigure(Figure f) {
if (f instanceof GroupFigure) {
GroupFigure gf = this;
while (gf != null && gf != f) {
gf = gf.parent;
}
if (gf == f) {
throw new IllegalArgumentException();
}
}
if (f.parent != null) {
throw new IllegalArgumentException();
}
figures.add(f);
f.parent = this;
}
@Override
public void draw(String prefix) {
System.out.println(prefix + name);
for (Figure f : figures) {
f.draw(prefix + ">>");
}
}
}
package patterns.composite.sol1;
public class OvalFigure extends Figure {
private final String name;
public OvalFigure(String name) {
this.name = name;
}
@Override
public void draw(String prefix) {
System.out.println(prefix + name);
}
}
package patterns.composite.sol1;
public class RectangleFigure extends Figure {
private final String name;
public RectangleFigure(String name) {
this.name = name;
}
@Override
public void draw(String prefix) {
System.out.println(prefix + name);
}
}
package patterns.composite.sol1;
public class Test1 {
public static void main(String[] args) {
Figure f1 = new RectangleFigure("f1");
Figure f2 = new RectangleFigure("f2");
Figure f3 = new OvalFigure("f3");
GroupFigure g1 = new GroupFigure("g1", f1, f2);
GroupFigure g2 = new GroupFigure("g2", f3, f1);
g1.draw("");
g2.draw("");
}
}
package patterns.composite.sol1;
public class Test2 {
public static void main(String[] args) {
Figure f1 = new RectangleFigure("f1");
Figure f2 = new RectangleFigure("f2");
Figure f3 = new OvalFigure("f3");
GroupFigure g1 = new GroupFigure("g1", f1, f2);
GroupFigure g2 = new GroupFigure("g2", g1, f3);
g1.addFigure(g2);
g2.draw("");
}
}
package patterns.composite.sol2;
public abstract class Figure {
public abstract void draw(String prefix);
boolean contained; // indicates whether this figure is part of a composite
}
package patterns.composite.sol2;
import java.util.LinkedList;
import java.util.List;
public final class GroupFigure extends Figure {
private final String name;
private final List<Figure> figures = new LinkedList<>();
public GroupFigure(String name, Figure... figures) {
this.name = name;
for (Figure f : figures) {
addFigure(f);
}
}
public void addFigure(Figure f) {
if (f.contained || contained || f == this) throw new IllegalArgumentException();
f.contained = true;
figures.add(f);
}
@Override
public void draw(String prefix) {
System.out.println(prefix + name);
for (Figure f : figures) {
f.draw(prefix + ">>");
}
}
}
package patterns.composite.sol2;
public class OvalFigure extends Figure {
private final String name;
public OvalFigure(String name) {
this.name = name;
}
@Override
public void draw(String prefix) {
System.out.println(prefix + name);
}
}
package patterns.composite.sol2;
public class RectangleFigure extends Figure {
private final String name;
public RectangleFigure(String name) {
this.name = name;
}
@Override
public void draw(String prefix) {
System.out.println(prefix + name);
}
}
package patterns.composite.sol2;
public class Test1 {
public static void main(String[] args) {
Figure f1 = new RectangleFigure("f1");
Figure f2 = new RectangleFigure("f2");
Figure f3 = new OvalFigure("f3");
GroupFigure g1 = new GroupFigure("g1", f1, f2);
GroupFigure g2 = new GroupFigure("g2", f3, f1);
g1.draw("");
g2.draw("");
}
}
package patterns.composite.sol2;
public class Test2 {
public static void main(String[] args) {
Figure f1 = new RectangleFigure("f1");
Figure f2 = new RectangleFigure("f2");
Figure f3 = new OvalFigure("f3");
GroupFigure g1 = new GroupFigure("g1", f1, f2);
GroupFigure g2 = new GroupFigure("g2", g1, f3);
g1.addFigure(g2);
g2.draw("");
}
}
package patterns.composite.sol2;
public class Test3 {
public static void main(String[] args) {
Figure f1 = new RectangleFigure("f1");
Figure f2 = new RectangleFigure("f2");
Figure f3 = new OvalFigure("f3");
GroupFigure g1 = new GroupFigure("g1", f1, f2, f3);
g1.addFigure(g1);
g1.draw("");
}
}
package patterns.composite.sol3;
public abstract class Figure {
public Figure(GroupFigure parent) {
if(parent != null) parent.addFigure(this);
}
public abstract void draw(String prefix);
}
package patterns.composite.sol3;
import java.util.LinkedList;
import java.util.List;
public class GroupFigure extends Figure {
private final String name;
private final List<Figure> figures = new LinkedList<>();
public GroupFigure(GroupFigure parent, String name) {
super(parent);
this.name = name;
}
void addFigure(Figure f) {
figures.add(f);
}
@Override
public void draw(String prefix) {
System.out.println(prefix + name);
for (Figure f : figures) {
f.draw(prefix + ">>");
}
}
}
package patterns.composite.sol3;
public class OvalFigure extends Figure {
private final String name;
public OvalFigure(GroupFigure parent, String name) {
super(parent);
this.name = name;
}
@Override
public void draw(String prefix) {
System.out.println(prefix + name);
}
}
package patterns.composite.sol3;
public class RectangleFigure extends Figure {
private final String name;
public RectangleFigure(GroupFigure parent, String name) {
super(parent);
this.name = name;
}
@Override
public void draw(String prefix) {
System.out.println(prefix + name);
}
}
package patterns.composite.sol3;
public class Test1 {
public static void main(String[] args) {
GroupFigure g1 = new GroupFigure(null, "g1");
GroupFigure g2 = new GroupFigure(null, "g2");
new RectangleFigure(g1, "f1");
new RectangleFigure(g1, "f2");
new OvalFigure(g2, "f3");
// it is not possible to add f1 to g2 as well!
g1.draw("");
g2.draw("");
}
}
package patterns.composite.sol3;
public class Test2 {
public static void main(String[] args) {
GroupFigure g2 = new GroupFigure(null, "g2");
GroupFigure g1 = new GroupFigure(g2, "g1");
new RectangleFigure(g1, "f1");
new RectangleFigure(g1, "f2");
new OvalFigure(g2, "f3");
// it is not possible to add g2 to g1, except if the test program is defined in
// the same package (as it is for this example, i.e. the following line could be added).
// g1.addFigure(g2);
g2.draw("");
}
}
package patterns.composite.sol3a;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public abstract class Figure {
public Figure(AbstractGroupFigure parent) {
if(parent != null) parent.addFigure(this);
}
public abstract void draw(String prefix);
public static abstract class AbstractGroupFigure extends Figure {
private final List<Figure> figures = new LinkedList<>();
public AbstractGroupFigure(AbstractGroupFigure parent) {
super(parent);
}
private void addFigure(Figure f) {
figures.add(f);
}
public final List<Figure> getFigures() {
return Collections.unmodifiableList(figures);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment