13 August 2016

Composite Design Pattern in Java


Composite pattern in UML.
import java.util.List;
import java.util.ArrayList;
 
//Component
interface Component {
    public void show();
}
 
//Composite
class Composite implements Component {
 
    private List<Component> childComponents = new ArrayList<Component>();
 
    public void add(Component component) {
     childComponents.add(component);
    }
 
    public void remove(Component component) {
     childComponents.remove(component);
    }
 
 @Override
 public void show() {
  for (Component component : childComponents) {
         component.show();
        }
 }
}
 
//leaf
class Leaf implements Component {
 String name;
 public Leaf(String s){
  name = s;
 }
    public void show() {
        System.out.println(name);
    }
}
 
 
public class CompositeTest {
 
    public static void main(String[] args) {
        Leaf leaf1 = new Leaf("1");
        Leaf leaf2 = new Leaf("2");
        Leaf leaf3 = new Leaf("3");
        Leaf leaf4 = new Leaf("4");
        Leaf leaf5 = new Leaf("5");
 
        Composite composite1 = new Composite();
        composite1.add(leaf1);
        composite1.add(leaf2);
 
        Composite composite2 = new Composite();        
        composite2.add(leaf3);
        composite2.add(leaf4);
        composite2.add(leaf5);
 
        composite1.add(composite2);
        composite1.show();
    }
}
Output: 
1
2
3
4
5

/** "Component" */
interface Shape{

    //Draw the color.
    public void draw(String fillColor);
}

/** "Composite" */
import java.util.List;
import java.util.ArrayList;
public class Drawing implements Shape{

 //collection of Shapes
 private List<Shape> shapes = new ArrayList<Shape>();
 
 @Override
 public void draw(String fillColor) {
  for(Shape sh : shapes)
  {
   sh.draw(fillColor);
  }
 }
 
 //adding shape to drawing
 public void add(Shape s){
  this.shapes.add(s);
 }
 
 //removing shape from drawing
 public void remove(Shape s){
  shapes.remove(s);
 }
 
 //removing all the shapes
 public void clear(){
  System.out.println("Clearing all the shapes from drawing");
  this.shapes.clear();
 }
}

/** "Triangle" */
public class Triangle implements Shape {

 @Override
 public void draw(String fillColor) {
  System.out.println("Drawing Triangle with color "+fillColor);
 }

}
/** "Circle" */
public class Circle implements Shape {

 @Override
 public void draw(String fillColor) {
  System.out.println("Drawing Circle with color "+fillColor);
 }

}


/** Client */
public class Program {

 public static void main(String[] args) {
  //Initialize four Triangle & Circle
  Shape tri = new Triangle();
  Shape tri1 = new Triangle();
  Shape cir = new Circle();
  //Initialize 1 composite drawing
  Drawing drawing = new Drawing();
  //Composes the drawing
  drawing.add(tri1);
  drawing.add(tri1);
  drawing.add(cir);
  
  drawing.draw("Red");
  
  drawing.clear();
  
  drawing.add(tri);
  drawing.add(cir);
  drawing.draw("Green");
 }

}
Output:
Drawing Triangle with color Red
Drawing Triangle with color Red
Drawing Circle with color Red
Clearing all the shapes from drawing
Drawing Triangle with color Green
Drawing Circle with color Green

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang