package model; public abstract class Shape implements Cloneable { protected double area; public abstract void calculateArea(); public double getArea() { return area; } @Override public Object clone() { Object clone = null; try { clone = super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return clone; } @Override public String toString() { return "Shape{" + "area=" + area + '}'; } }
package model; import static java.lang.Math.PI; import static java.lang.Math.pow; public class Circle extends Shape { private double radious; public Circle() { } public Circle(double radious) { this.radious = radious; } @Override public void calculateArea() { area = pow(radious, 2) * PI; } public void setRadious(double radious) { this.radious = radious; } public double getRadious() { return radious; } @Override public String toString() { return super.toString() + " Circle{" + "radious=" + radious + '}'; } }
package model; public class Square extends Shape { private double side; public Square() { } public Square(double side) { this.side = side; } @Override public void calculateArea() { area = side * side; } public double getSide() { return side; } public void setSide(double side) { this.side = side; } @Override public String toString() { return super.toString() + " Square{" + "side=" + side + '}'; } }
package model; public class Rectangle extends Shape { private double height; private double width; public Rectangle() { } public Rectangle(double height, double width) { this.height = height; this.width = width; } @Override public void calculateArea() { area = height * width; } public double getHeight() { return height; } public void setHeight(double height) { this.height = height; } public double getWidth() { return width; } public void setWidth(double width) { this.width = width; } @Override public String toString() { return super.toString() + " Rectangle{" + "height=" + height + ", width=" + width + '}'; } }
package logic; import java.util.Hashtable; import model.*; public enum ShapeCache { INSTANCE; private Hashtable<String, Shape> shapeMap = new Hashtable<String, Shape>(); public Shape getShape(String shapeType) { Shape shape = shapeMap.get(shapeType); if (shapeMap.get(shapeType) != null) { return (Shape) shape.clone(); } try { String packName = Shape.class.getPackage().getName(); shape = (Shape) Class.forName(packName + "." + shapeType).newInstance(); shapeMap.put(shapeType, shape); return (Shape) shape.clone(); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { System.out.println(e.toString()); } return shape; } }
package prototype; import logic.ShapeCache; import model.Circle; import model.Rectangle; import model.Square; public class Prototype { public static void main(String[] args) { ShapeCache cache = ShapeCache.INSTANCE; Circle circle = (Circle) cache.getShape("Circle"); circle.setRadious(3); circle.calculateArea(); System.out.println(circle.toString()); Rectangle rectangle = (Rectangle) cache.getShape("Rectangle"); rectangle.setHeight(2); rectangle.setWidth(3); rectangle.calculateArea(); System.out.println(rectangle.toString()); Square square = (Square) cache.getShape("Square"); square.setSide(3); square.calculateArea(); System.out.println(square.toString()); Circle circle2 = (Circle) cache.getShape("Circle"); System.out.println(circle2.toString()); } }
Nenhum comentário:
Postar um comentário