关于设计模式之前就很想学习,但是发现自己功底不够深厚,学习起来有一定的难度,最近准备拿出来重新系统的学习一下。。。关于设计模式自我感觉网上大多讲的繁文缛节,还不如直接看代码来的实在。其次在学习的过程中我觉得设计模式就是一种思想。用的多了自然就得心应手了。前期也不用考虑那么多,比如优缺点啊什么的,只需要知道核心思想是什么,主要用在哪些场景下面即可。下面说说我的理解吧:
简单工厂模式:
从表面上理解就是一种工厂,那么工厂是干什么的?加工零部件吧。。比如我们这就是一个车胎的工厂,可以满足各种车胎的生产。ok..接下来,不同的汽车厂商需要什么类型的车胎交给工厂去做就好,而不用管车胎工厂是怎么做的。这就是工厂模式。是不是感觉很简单吧。下面展示一个案例:或许看完就能豁然开朗吧!
步骤 1
创建一个接口:
Shape.java
1 |
public interface Shape { void draw(); } |
步骤 2
创建实现接口的实体类。
Rectangle.java
1 2 3 4 5 6 7 |
public class Rectangle implements Shape { @Override public void draw() { System.out.println("Inside Rectangle::draw() method."); } } |
Square.java
1 2 3 4 5 6 7 |
public class Square implements Shape { @Override public void draw() { System.out.println("Inside Square::draw() method."); } } |
Circle.java
1 2 3 4 5 6 7 |
public class Circle implements Shape { @Override public void draw() { System.out.println("Inside Circle::draw() method."); } } |
步骤 3
创建一个工厂,生成基于给定信息的实体类的对象。
ShapeFactory.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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(); } else if(shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } return null; } } |
步骤 4
使用该工厂,通过传递类型信息来获取实体类的对象。
FactoryPatternDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class FactoryPatternDemo { 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(); //获取 Square 的对象,并调用它的 draw 方法 Shape shape3 = shapeFactory.getShape("SQUARE"); //调用 Square 的 draw 方法 shape3.draw(); } } |
步骤 5
执行程序,输出结果:
1 2 3 |
Inside Circle::draw() method. Inside Rectangle::draw() method. Inside Square::draw() method. |
抽象工厂
那什么是抽象工厂模式,说白了就是我这个生产车胎的工厂,不仅要生产车胎,我还要生产车轮,好了,这样一弄,简单工厂不能满足组我们的要求了.玛尼,不能用那就不用了。人们又想出了另一种解决方案。那我一个工厂建造两个生产设备吧。我一个用来生产车胎,一个用来生产车轮,不就好了。对应设计模式里面的创建了两个接口~
案例:
步骤 1
为形状创建一个接口。
Shape.java
1 |
public interface Shape { void draw(); } |
步骤 2
创建实现接口的实体类。
Rectangle.java
1 2 3 4 5 6 7 |
public class Rectangle implements Shape { @Override public void draw() { System.out.println("Inside Rectangle::draw() method."); } } |
Square.java
1 2 3 4 5 6 7 |
public class Square implements Shape { @Override public void draw() { System.out.println("Inside Square::draw() method."); } } |
Circle.java
1 2 3 4 5 6 7 |
public class Circle implements Shape { @Override public void draw() { System.out.println("Inside Circle::draw() method."); } } |
步骤 3
为颜色创建一个接口。
Color.java
1 2 3 |
public interface Color { void fill(); } |
步骤4
创建实现接口的实体类。
Red.java
1 2 3 4 5 6 7 |
public class Red implements Color { @Override public void fill() { System.out.println("Inside Red::fill() method."); } } |
Green.java
1 2 3 4 5 6 7 |
public class Green implements Color { @Override public void fill() { System.out.println("Inside Green::fill() method."); } } |
Blue.java
1 2 3 4 5 6 7 |
public class Blue implements Color { @Override public void fill() { System.out.println("Inside Blue::fill() method."); } } |
步骤 5
为 Color 和 Shape 对象创建抽象类来获取工厂。
AbstractFactory.java
1 2 3 4 |
public abstract class AbstractFactory { public abstract Color getColor(String color); public abstract Shape getShape(String shape) ; } |
步骤 6
创建扩展了 AbstractFactory 的工厂类,基于给定的信息生成实体类的对象。
ShapeFactory.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class ShapeFactory extends AbstractFactory { @Override 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(); } else if(shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } return null; } @Override public Color getColor(String color) { return null; } } |
ColorFactory.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class ColorFactory extends AbstractFactory { @Override public Shape getShape(String shapeType){ return null; } @Override public Color getColor(String color) { if(color == null){ return null; } if(color.equalsIgnoreCase("RED")){ return new Red(); } else if(color.equalsIgnoreCase("GREEN")){ return new Green(); } else if(color.equalsIgnoreCase("BLUE")){ return new Blue(); } return null; } } |
步骤 7
创建一个工厂创造器/生成器类,通过传递形状或颜色信息来获取工厂。
FactoryProducer.java
1 2 3 4 5 6 7 8 9 10 |
public class FactoryProducer { public static AbstractFactory getFactory(String choice){ if(choice.equalsIgnoreCase("SHAPE")){ return new ShapeFactory(); } else if(choice.equalsIgnoreCase("COLOR")){ return new ColorFactory(); } return null; } } |
步骤 8
使用 FactoryProducer 来获取 AbstractFactory,通过传递类型信息来获取实体类的对象。
AbstractFactoryPatternDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
public class AbstractFactoryPatternDemo { public static void main(String[] args) { //获取形状工厂 AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE"); //获取形状为 Circle 的对象 Shape shape1 = shapeFactory.getShape("CIRCLE"); //调用 Circle 的 draw 方法 shape1.draw(); //获取形状为 Rectangle 的对象 Shape shape2 = shapeFactory.getShape("RECTANGLE"); //调用 Rectangle 的 draw 方法 shape2.draw(); //获取形状为 Square 的对象 Shape shape3 = shapeFactory.getShape("SQUARE"); //调用 Square 的 draw 方法 shape3.draw(); //获取颜色工厂 AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR"); //获取颜色为 Red 的对象 Color color1 = colorFactory.getColor("RED"); //调用 Red 的 fill 方法 color1.fill(); //获取颜色为 Green 的对象 Color color2 = colorFactory.getColor("Green"); //调用 Green 的 fill 方法 color2.fill(); //获取颜色为 Blue 的对象 Color color3 = colorFactory.getColor("BLUE"); //调用 Blue 的 fill 方法 color3.fill(); } } |
步骤 9
执行程序,输出结果:
1 2 3 4 5 6 |
Inside Circle::draw() method. Inside Rectangle::draw() method. Inside Square::draw() method. Inside Red::fill() method. Inside Green::fill() method. Inside Blue::fill() method. |