File tree Expand file tree Collapse file tree 6 files changed +94
-0
lines changed
src/com/design_pattern/structural/composition Expand file tree Collapse file tree 6 files changed +94
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 1+ package com .design_pattern .structural .composition ;
2+
3+ public interface Shape {
4+
5+ public void draw (String fillColor );
6+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments