In: Computer Science
Implement a Composite Design Pattern for the code below that creates a family tree
MAIN:
public class Main {
public static void main(String[] args) {
/* Let's create a family tree (for instance like one used on
genealogy sites).
For the sake of simplicity, assume an individual can have at most
two children.
If an individual has 1-2 children, they are considered a "tree". If
an individual
does not have children, they are considered a "person".
With that in mind, let's populate a family tree with some data.
*/
Person p1 = new Person(1);
Person p2 = new Person(2);
Person p3 = new Person(3);
Person p4 = new Person(4);
Tree t1 = new Tree(p1, 1);
Tree t2 = new Tree(p2, p3, 2);
Tree t3 = new Tree(t1, p4, 3);
Tree t4 = new Tree(t3, t2, 4);
t4.print();
}
}
PERSON:
public class Person {
String name;
public Person(int num) {
name = "person" + num;
}
public void print() {
System.out.println(name);
}
}
TREE:
public class Tree {
private String name;
private Tree tree1;
private Tree tree2;
private Person person1;
private Person person2;
public Tree(Person p1, int num) {
tree1 = null;
tree2 = null;
person1 = p1;
person2 = null;
name = "tree" + num;
}
public Tree(Tree t1, int num) {
tree1 = t1;
tree2 = null;
person1 = null;
person2 = null;
name = "tree" + num;
}
public Tree(Tree t1, Tree t2, int num){
tree1 = t1;
tree2 = t2;
person1 = null;
person2 = null;
name = "tree" + num;
}
public Tree(Tree t1, Person p2, int num){
tree1 = t1;
tree2 = null;
person1 = null;
person2 = p2;
name = "tree" + num;
}
public Tree(Person p1, Person p2, int num){
tree1 = null;
tree2 = null;
person1 = p1;
person2 = p2;
name = "tree" + num;
}
public void print() {
System.out.println(name);
if (tree1 != null) {
tree1.print();
}
if (tree2 != null) {
tree2.print();
}
if (person1 != null) {
person1.print();
}
if (person2 != null) {
person2.print();
}
}
}
For composite Design Pattern Icreated On Interface SuperPrint and declared one method inside it that is print()
Then implements Person class from SuperPrint Interface and override the print() to Person class.
Same way Tree calss also implements from the SuperPrint interface and override the print().
In composite design pattern basically we are Creating one Interface and declaring all common methods inside in it. If any Class wants to Implement the common methods then that class should implements from the Interface and can override the common methods. In this way mutliple class can implements from the same Inerface to write implementation logic.
Below is the code
SuperPrint.java
public interface SuperPrint {
void print();
}
Person.java
public class Person implements SuperPrint{
String name;
public
Person(int num) {
name = "person" + num;
}
//
Interface implemented method
public void print() {
System.out.println(name);
}
}
Tree.java
public class Tree implements SuperPrint{
private String name;
private Tree tree1;
private Tree tree2;
private Person person1;
private Person person2;
public Tree(Person p1, int num) {
tree1 = null;
tree2 = null;
person1 = p1;
person2 = null;
name = "tree" + num;
}
public Tree(Tree t1, int num) {
tree1 = t1;
tree2 = null;
person1 = null;
person2 = null;
name = "tree" + num;
}
public Tree(Tree t1, Tree t2, int num) {
tree1 = t1;
tree2 = t2;
person1 = null;
person2 = null;
name = "tree" + num;
}
public Tree(Tree t1, Person p2, int num) {
tree1 = t1;
tree2 = null;
person1 = null;
person2 = p2;
name = "tree" + num;
}
public Tree(Person p1, Person p2, int num) {
tree1 = null;
tree2 = null;
person1 = p1;
person2 = p2;
name = "tree" + num;
}
// Interface implemented
method
public void print() {
System.out.println(name);
if (tree1 != null) {
tree1.print();
}
if (tree2 != null) {
tree2.print();
}
if (person1 != null) {
person1.print();
}
if (person2 != null) {
person2.print();
}
}
}
In the following way also we can write the Main class after implementing Person and Tree class from SuperPrint Interface.
Main.java
public class Main {
public static void main(String[] args) {
/* Let's create a family tree (for
instance like one used on genealogy sites).
For the sake of simplicity, assume
an individual can have at most two children.
If an individual has 1-2 children,
they are considered a "tree". If an individual
does not have children, they are
considered a "person".
With that in mind, let's populate a
family tree with some data. */
SuperPrint p1 = new
Person(1);
SuperPrint p2 = new
Person(2);
SuperPrint p3 = new
Person(3);
SuperPrint p4 = new Person(4);
SuperPrint t1 = new
Tree((Person)p1, 1);
SuperPrint t2 = new
Tree((Person)p2, (Person)p3, 2);
SuperPrint t3 = new Tree((Tree)t1,
(Person)p4, 3);
SuperPrint t4 = new
Tree((Tree)t3,(Tree) t2, 4);
t4.print();
}
}