1、简单工厂模式
工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。
package factory; public interface Shape { void draw(); } package factory; public class Square implements Shape{ @Override public void draw() { System.out.println("Inside Square:draw() is running"); } } package factory; public class Rectangle implements Shape{ @Override public void draw() { System.out.println("Inside Rectangle:draw() method is running!!!!"); } } package factory; public class Circle implements Shape{ @Override public void draw() { System.out.println("Inside Circle:draw() method is running!!!!"); } } package factory; public class ShapeFactory { public Shape getShape(String type) { if(type == null) { return null; } if(type.equalsIgnoreCase("Rectangle")) { return new Rectangle(); } if(type.equalsIgnoreCase("Square")) { return new Square(); } if(type.equalsIgnoreCase("Circle")) { return new Circle(); } return null; } } package factory; public class ShapeFactoryTest { public static void main(String[] args) { ShapeFactory shape = new ShapeFactory(); Shape rectangle = shape.getShape("Rectangle"); rectangle.draw(); Shape square = shape.getShape("Square"); square.draw(); Shape circle = shape.getShape("Circle"); circle.draw(); } } 结果打印; Inside Rectangle:draw() method is running!!!! Inside Square:draw() is running Inside Circle:draw() method is running!!!!
2、抽象工厂模式
抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。
1 //创建一个形状接口 2 public interface Shape { 3 void draw(); 4 } 5 6 //创建一个形状接口 7 public interface Color { 8 void fill(); 9 } 10 11 public class Circle implements Shape{ 12 13 @Override 14 public void draw() { 15 System.out.println("Inside Circle:画圆"); 16 } 17 18 } 19 20 21 public class Red implements Color{ 22 23 @Override 24 public void fill() { 25 System.out.println("红色"); 26 } 27 28 } 29 30 package absfactory; 31 //颜色工厂 32 public class ColorFactory extends AbstractFactory{ 33 34 public Color getColor(String color) { 35 if(color == null){ 36 return null; 37 } 38 if(color.equalsIgnoreCase("RED")){ 39 return new Red(); 40 } else if(color.equalsIgnoreCase("GREEN")){ 41 return new Green(); 42 } else if(color.equalsIgnoreCase("BLUE")){ 43 return new Blue(); 44 } 45 return null; 46 } 47 48 public Shape getShape(String shape) { 49 return null; 50 } 51 52 } 53 54 //形状工厂 55 package absfactory; 56 57 public class ShapeFactory extends AbstractFactory { 58 59 60 public Color getColor(String color) { 61 // TODO Auto-generated method stub 62 return null; 63 } 64 65 public Shape getShape(String shapeType) { 66 if(shapeType == null){ 67 return null; 68 } 69 if(shapeType.equalsIgnoreCase("CIRCLE")){ 70 return new Circle(); 71 } else if(shapeType.equalsIgnoreCase("RECTANGLE")){ 72 return new Rectangle(); 73 } else if(shapeType.equalsIgnoreCase("SQUARE")){ 74 return new Square(); 75 } 76 return null; 77 } 78 79 } 80 81 package absfactory; 82 83 //为Color和Shape对象创建抽象类来获取工厂 84 public abstract class AbstractFactory { 85 public abstract Color getColor(String color); 86 public abstract Shape getShape(String shape); 87 } 88 89 //创建一个工厂创造生成器类,通过传递形状或颜色信息来获取工厂 90 //可以认为是工厂的工厂 91 public class FactoryProducer { 92 public static AbstractFactory getFactory(String code) { 93 if(code.equalsIgnoreCase("shape")) { 94 return new ShapeFactory(); 95 }else if(code.equalsIgnoreCase("color")) { 96 return new ColorFactory(); 97 } 98 return null; 99 } 100 } 101 102 package absfactory; 103 //测试 104 public class AbstractFactoryTest { 105 public static void main(String[] args) { 106 //获取形状工厂 107 AbstractFactory shapeFactory = FactoryProducer.getFactory("shape"); 108 //获取形状为Circle的对象 109 Shape circle = shapeFactory.getShape("circle"); 110 //调用draw()方法 111 circle.draw(); 112 113 //获取颜色工厂 114 AbstractFactory colorFactory = FactoryProducer.getFactory("color"); 115 //获取颜色为红色的对象 116 Color red = colorFactory.getColor("red"); 117 red.fill(); 118 } 119 } 120 121 打印输出: 122 Inside Circle:画圆 123 红色