Skip to content

Commit ef9f015

Browse files
committed
Composition Design Pattern
1 parent 5ad8d6a commit ef9f015

File tree

6 files changed

+94
-0
lines changed

6 files changed

+94
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.design_pattern.structural.composition;
2+
3+
public class Circle implements Shape{
4+
5+
@Override
6+
public void draw(String fillColor) {
7+
// TODO Auto-generated method stub
8+
System.out.println("Circle fill color "+fillColor);
9+
}
10+
11+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.design_pattern.structural.composition;
2+
3+
public class CompositionPatternDesign {
4+
5+
public static void main(String[] arg){
6+
7+
8+
Shape triangeShape = new Triangle();
9+
Shape squireShape = new Square();
10+
Shape circleShape = new Circle();
11+
12+
Drawing drawing = new Drawing();
13+
drawing.add(circleShape);
14+
drawing.add(squireShape);
15+
drawing.add(triangeShape);
16+
drawing.draw("red");
17+
18+
drawing.clear();
19+
drawing.add(circleShape);
20+
drawing.add(squireShape);
21+
drawing.draw("green");
22+
23+
}
24+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.design_pattern.structural.composition;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Drawing {
7+
8+
List<Shape> shapes = new ArrayList<Shape>();
9+
10+
public void draw(String fillColor) {
11+
// TODO Auto-generated method stub
12+
for (Shape s: shapes) {
13+
s.draw(fillColor);
14+
}
15+
}
16+
17+
public void add(Shape s){
18+
shapes.add(s);
19+
}
20+
21+
public void delete(Shape s) {
22+
// TODO Auto-generated method stub
23+
shapes.remove(s);
24+
}
25+
26+
public void clear() {
27+
// TODO Auto-generated method stub
28+
System.out.println("Clearing all the shapes from drawing");
29+
shapes.clear();
30+
}
31+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.design_pattern.structural.composition;
2+
3+
public interface Shape {
4+
5+
public void draw(String fillColor);
6+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.design_pattern.structural.composition;
2+
3+
public class Square implements Shape{
4+
5+
@Override
6+
public void draw(String fillColor) {
7+
// TODO Auto-generated method stub
8+
System.out.println("Square fill color "+fillColor);
9+
}
10+
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.design_pattern.structural.composition;
2+
3+
public class Triangle implements Shape{
4+
5+
@Override
6+
public void draw(String fillColor) {
7+
// TODO Auto-generated method stub
8+
System.out.println("Triangle fill color "+fillColor);
9+
}
10+
11+
}

0 commit comments

Comments
 (0)