工厂模式
工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。
好处:
- 用户只需要知道名称就能,不用关注如何创建对象
- 扩展性高
- 屏蔽产品的具体实现,调用者只关心产品的接口
劣处
- 传递工厂不知道的字符串时,会报错,
- 每次增加一个产品时,都需要增加一个具体类和对象实现工厂,使得系统中类的个数成倍增加
简单实例
有一个汽车工厂,生产两种汽车,bench和bmo(随便起的名字),用户通过工厂指定需要生产的汽车型号
及有名Car接口,其中声明一个 getName()方法
bench和bmo类实现Car接口
- Car
package com.qcby.designpattern;
public interface Car {
String printName();
}
- Bench 和 bmo
package com.qcby.designpattern;
public class Bench implements Car{
@Override
public String printName() {
System.out.println("奔驰");
return "Bench";
}
}
package com.qcby.designpattern;
public class bmo implements Car{
@Override
public String printName() {
System.out.println("BMO");
return "bmo";
}
}
- CarFactory
package com.qcby.designpattern;
public class CarFactory {
public static Car createCar(String carname){
switch (carname){
case "Bench":
return new Bench();
case "bmo":
return new bmo();
default:
return null;
}
}
}
- 测试
package com.qcby.designpattern;
public class Main {
public static void main(String[] args) {
Car bench = CarFactory.createCar("Bench");
bench.printName();
}
}
抽象工厂模式
抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。
总的来说就是工厂的工厂
优点:当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。
缺点:产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码
简单实例
创建 Shape 和 Color 接口和实现这些接口的实体类。
创建抽象工厂类 AbstractFactory
定义工厂类 ShapeFactory 和 ColorFactory,这两个工厂类都是扩展了 AbstractFactory
创建一个工厂创造器/生成器类 FactoryProducer。
AbstractFactoryPatternDemo 类使用 FactoryProducer 来获取 AbstractFactory 对象。它将向 AbstractFactory 传递形状信息 Shape,以便获取它所需对象的类型。同时它还向 AbstractFactory 传递颜色信息 Color,以便获取它所需对象的类型。
shape
//接口
package com.qcby.designpattern.factoryTest.shape;
public interface Shape {
void draw();
}
//具体实现类
package com.qcby.designpattern.factoryTest.shape;
import com.qcby.designpattern.factoryTest.shape.Shape;
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
package com.qcby.designpattern.factoryTest.shape;
import com.qcby.designpattern.factoryTest.shape.Shape;
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
package com.qcby.designpattern.factoryTest.shape;
import com.qcby.designpattern.factoryTest.shape.Shape;
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
color
package com.qcby.designpattern.factoryTest.color;
public interface Color {
void fill();
}
package com.qcby.designpattern.factoryTest.color;
import com.qcby.designpattern.factoryTest.color.Color;
public class Blue implements Color {
@Override
public void fill() {
System.out.println("Inside Blue::fill() method.");
}
}
package com.qcby.designpattern.factoryTest.color;
import com.qcby.designpattern.factoryTest.color.Color;
public class Green implements Color {
@Override
public void fill() {
System.out.println("Inside Green::fill() method.");
}
}
package com.qcby.designpattern.factoryTest.color;
import com.qcby.designpattern.factoryTest.color.Color;
public class Red implements Color {
@Override
public void fill() {
System.out.println("Inside Red::fill() method.");
}
}
抽象工厂类
package com.qcby.designpattern.factoryTest.factory;
import com.qcby.designpattern.factoryTest.color.Color;
import com.qcby.designpattern.factoryTest.shape.Shape;
public abstract class AbstractFactory {
public abstract Color getColor(String color);
public abstract Shape getShape(String shape);
}
两个工厂
//Color工厂
package com.qcby.designpattern.factoryTest.factory;
import com.qcby.designpattern.factoryTest.color.Blue;
import com.qcby.designpattern.factoryTest.color.Color;
import com.qcby.designpattern.factoryTest.color.Green;
import com.qcby.designpattern.factoryTest.color.Red;
import com.qcby.designpattern.factoryTest.shape.Shape;
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;
}
}
//shape工厂
package com.qcby.designpattern.factoryTest.factory;
import com.qcby.designpattern.factoryTest.color.Color;
import com.qcby.designpattern.factoryTest.shape.Circle;
import com.qcby.designpattern.factoryTest.shape.Rectangle;
import com.qcby.designpattern.factoryTest.shape.Shape;
import com.qcby.designpattern.factoryTest.shape.Square;
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;
}
}
生成器类
package com.qcby.designpattern.factoryTest;
import com.qcby.designpattern.factoryTest.factory.AbstractFactory;
import com.qcby.designpattern.factoryTest.factory.ColorFactory;
import com.qcby.designpattern.factoryTest.factory.ShapeFactory;
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;
}
}
测试类
package com.qcby.designpattern.factoryTest;
import com.qcby.designpattern.factoryTest.color.Color;
import com.qcby.designpattern.factoryTest.factory.AbstractFactory;
import com.qcby.designpattern.factoryTest.shape.Shape;
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();
}
}