Composite pattern Implementation

Give a read from here and here about composite pattern, you will know much about it, here I’m with implementation.


package com.rokon.pattern;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public abstract class AbstractShape {
private final static Logger LOGGER = Logger.getLogger(AbstractShape.class
.getName());

private List<AbstractShape> shapeList = new ArrayList<AbstractShape>();

public void draw(Graphics g) {
for (AbstractShape item : shapeList) {
item.draw(g);
}
}

public void add(AbstractShape shape) {
if (shape != null) {
shapeList.add(shape);
} else {
LOGGER.log(Level.INFO, "null object passed,can't add");
}
}

public void remove(AbstractShape shape) {
if (shape != null) {
shapeList.remove(shape);
} else {
LOGGER.log(Level.INFO, "null object passed, can't remove");
}
}
}

Line


package com.rokon.pattern;

import java.awt.Graphics;
import java.awt.Point;

public class Line extends AbstractShape {
private Point startPoint, endPoint;

public Line(Point startPoint, Point endPoint) {
super();
this.startPoint = startPoint;
this.endPoint = endPoint;
}

@Override
public void draw(Graphics g) {
g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
}
}

Circle

package com.rokon.pattern;

import java.awt.Graphics;
import java.awt.Point;

public class Circle extends AbstractShape{
private Point top;
private int width;
private int height;

public Circle(Point top, int width, int hegiht) {
this.top = top;
this.width = width;
this.height = hegiht;
}
@Override
public void draw(Graphics g) {
g.drawOval(top.x, top.y, width, height);
}
}

Rectangle

package com.rokon.pattern;

import java.awt.Point;

public class Ractangle extends AbstractShape{
private Point topLeft;
private Point topRight;
private Point bottomLeft;
private Point bottomRight;

public Ractangle(int x, int y, int width, int height) {
setPoint(new Point(x,y), height, width);
makeRactangle();
}
public Ractangle(Point startPoint, int width, int height){
setPoint(startPoint, height, width);
makeRactangle();
}

private void makeRactangle(){
Line a = new Line(topLeft, topRight);
Line b = new Line(topLeft, bottomLeft);
Line c = new Line(bottomLeft, bottomRight);
Line d = new Line(topRight, bottomRight);

add(a);
add(b);
add(c);
add(d);
}

public void setPoint(Point startPoint, int height, int width){
this.topLeft = startPoint;
this.topRight = new Point((topLeft.x+width), topLeft.y);
this.bottomLeft = new Point(topLeft.x, (topLeft.y+height));
this.bottomRight = new Point(topLeft.x+width, topLeft.y+height);
}
}

package com.rokon.pattern;

import java.awt.Point;

public class ShapeOne extends AbstractShape{
public ShapeOne() {
Ractangle one = new Ractangle(new Point(250,50), 150, 100);
Ractangle two = new Ractangle(new Point(125,200), 150, 100);
Ractangle three = new Ractangle(new Point(50,385), 150, 100);
Ractangle four = new Ractangle(new Point(250,385), 150, 100);
Ractangle five = new Ractangle(new Point(450,385), 150, 100);

Line lineOne = new Line(new Point(330,150), new Point(330,385));
Line lineTwo = new Line(new Point(275,250), new Point(330,250));
Line lineThree = new Line(new Point(125,335), new Point(525,335));
Line lineFour = new Line(new Point(125,335), new Point(125,385));
Line lineFive = new Line(new Point(525,335), new Point(525,385));

add(one);
add(two);
add(three);
add(four);
add(five);

add(lineOne);
add(lineTwo);
add(lineThree);
add(lineFour);
add(lineFive);
}
}

package com.rokon.pattern;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;

public class RShape extends JComponent {

private AbstractShape shape;

public RShape(AbstractShape shape) {
super();
this.shape = shape;
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, 650, 650);

g2d.setStroke(new BasicStroke(2));
g2d.setColor(Color.GREEN);

shape.draw(g2d);

g2d.setColor(Color.RED);
}
}

package com.rokon.pattern;

import java.awt.Dimension;
import java.awt.Point;

import javax.swing.JFrame;

public class ShapeOneApp extends JFrame {

public ShapeOneApp() {
super("Composite Pattern Implementation");
setSize(new Dimension(650, 650));
setLocationRelativeTo(null);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {
ShapeOne one = new ShapeOne();
RShape component = new RShape(one);

ShapeOneApp application = new ShapeOneApp();
application.getContentPane().add(component);
application.setVisible(true);
}
}

 

Leave a comment