标签:imp else dfa 形状 java interface rri circle int
Shape.java
public interface Shape { void draw(); }
Rectangel.java
public class Rectangle implements Shape { @Override public void draw() { System.out.println("draw Rectangle"); } }
Circle.java
public class Circle implements Shape { @Override public void draw() { System.out.println("draw Circle"); } }
ShapeFactory.java
public class ShapeFactory { //使用 getShape 方法获取形状类型的对象 public Shape getShape(String shapeType){ if(shapeType == null){ return null; } if(shapeType.equalsIgnoreCase("CIRCLE")){ return new Circle(); } else if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new Rectangle(); }return null; } }
SimpleFactoryDemo.java
public class SimpleFactoryDemo { public static void main(String[] args) { ShapeFactory shapeFactory = new ShapeFactory(); //获取 Circle 的对象,并调用它的 draw 方法 Shape shape1 = shapeFactory.getShape("CIRCLE"); //调用 Circle 的 draw 方法 shape1.draw(); //获取 Rectangle 的对象,并调用它的 draw 方法 Shape shape2 = shapeFactory.getShape("RECTANGLE"); //调用 Rectangle 的 draw 方法 shape2.draw(); } }
Shape.java
public interface Shape { void draw(); }
Circle2D.java
public class Circle2D implements Shape { @Override public void draw() { System.out.println("draw Circle2D"); } }
Circle3D.java
public class Circle3D implements Shape { @Override public void draw() { System.out.println("draw Circle3D"); } }
Rectangle2D.java
public class Rectangle2D implements Shape { @Override public void draw() { System.out.println("draw Rectangle2D"); } }
Rectangle3D.java
public class Rectangle3D implements Shape { @Override public void draw() { System.out.println("draw Rectangle3D"); } }
ShapeFactory.java
public abstract class ShapeFactory { public abstract Shape getShape(String shapeType); }
ShapeFactory2D.java
public class ShapeFactory2D extends ShapeFactory { @Override public Shape getShape(String shapeType) { if(shapeType == null){ return null; } if(shapeType.equalsIgnoreCase("CIRCLE2D")){ return new Circle2D(); } else if(shapeType.equalsIgnoreCase("RECTANGLE2D")){ return new Rectangle2D(); } return null; } }
ShapeFactory3D.java
public class ShapeFactory3D extends ShapeFactory { @Override public Shape getShape(String shapeType) { if(shapeType == null){ return null; } if(shapeType.equalsIgnoreCase("CIRCLE3D")){ return new Circle3D(); } else if(shapeType.equalsIgnoreCase("RECTANGLE3D")){ return new Rectangle3D(); } return null; } }
FactoryMethodDemo.java
public class FactoryMethodDemo { public static void main(String[] args) { ShapeFactory shapeFactory = new ShapeFactory2D(); Shape shape1 = shapeFactory.getShape("CIRCLE2D"); shape1.draw(); Shape shape2 = shapeFactory.getShape("Rectangle2D"); shape2.draw(); shapeFactory = new ShapeFactory3D(); Shape shape3 = shapeFactory.getShape("CIRCLE3D"); shape3.draw(); Shape shape4 = shapeFactory.getShape("Rectangle3D"); shape4.draw(); } }
AbstractFactory.java
public abstract class AbstractFactory { public abstract Shape getShape(String shapeType); public abstract Color getColor(String colorType); }
Graphics2DFactory.java
public class Graphics2DFactory extends AbstractFactory { @Override public Shape getShape(String shapeType) { if(shapeType == null){ return null; } if(shapeType.equalsIgnoreCase("CIRCLE2D")){ return new Circle2D(); } else if(shapeType.equalsIgnoreCase("RECTANGLE2D")){ return new Rectangle2D(); } return null; } @Override public Color getColor(String colorType) { if(colorType == null){ return null; } if(colorType.equalsIgnoreCase("RED")){ return new Red(); } else if(colorType.equalsIgnoreCase("BLUE")){ return new Blue(); } return null; } }
Graphics3DFactory.java
public class Graphics3DFactory extends AbstractFactory { @Override public Shape getShape(String shapeType) { if(shapeType == null){ return null; } if(shapeType.equalsIgnoreCase("CIRCLE3D")){ return new Circle3D(); } else if(shapeType.equalsIgnoreCase("RECTANGLE3D")){ return new Rectangle3D(); } return null; } @Override public Color getColor(String colorType) { if(colorType == null){ return null; } if(colorType.equalsIgnoreCase("RED")){ return new Red(); } else if(colorType.equalsIgnoreCase("BLUE")){ return new Blue(); } return null; } }
AbstractFactoryDemo.java
public class AbstractFactoryDemo { public static void main(String[] args) { AbstractFactory abstractFactory = new Graphics2DFactory(); Shape shape = abstractFactory.getShape("Circle2D"); Color color = abstractFactory.getColor("Red"); shape.draw(); color.fill(); } }
Color.java
public interface Color { void fill(); }
Red.java
public class Red implements Color { @Override public void fill() { System.out.println("Red"); } }
Blue.java
public class Blue implements Color { @Override public void fill() { System.out.println("Blue"); } }
标签:imp else dfa 形状 java interface rri circle int
原文地址:https://www.cnblogs.com/zhihaospace/p/12483527.html