Java设计模式之抽象工厂模式

Java设计模式之抽象工厂模式

抽象工厂模式是对简单工厂模式的改进,将工厂抽象成两层,抽象工厂和具体实现类工厂,将单个工厂类变成工厂簇,有利于代码的维护和扩展。抽象工厂模式主要用于有多于一个的产品族,而系统只需要消费一个产品。

一、实现抽象工厂UML类图

抽象工厂UML类图

二、实现抽象工厂模式的步骤

(1):创建一个Clothing接口和Color接口
(2):创建实现Clothing和Color的实体类
(3):创建一个AbstractFactory抽象工厂类
(4):创建实现AbstractFactory工厂的实体类ClothingFactory和ColorFactory
(5):创建一个工厂创造器FactoryProducer

(1)创建一个Clothing接口和Color接口

/**
 * @author yly
 * @ClassName Clothing
 * @Date 2020/2/17 19:03
 * @Version 1.0
 **/
public interface Clothing {
    public void make();
}
/**
 * @author yly
 * @ClassName Color
 * @Date 2020/2/8 11:57
 * @Version 1.0
 **/
public interface Color {
    void fill();
}

(2)创建实现Clothing和Color的实体类

/**
 * @author yly
 * @ClassName shirt
 * @Date 2020/2/17 19:05
 * @Version 1.0
 **/
public class Shirt implements Clothing {
    @Override
    public void make() {
        System.out.println("衬衫");
    }
}
/**
 * @author yly
 * @ClassName Shorts
 * @Date 2020/2/17 19:06
 * @Version 1.0
 **/
public class Shorts implements Clothing {
    @Override
    public void make() {
        System.out.println("短裤");
    }
}
/**
 * @author yly
 * @ClassName skirt
 * @Date 2020/2/17 19:04
 * @Version 1.0
 **/
public class Skirt implements Clothing {
    @Override
    public void make() {
        System.out.println("裙子");
    }
}
/**
 * @author yly
 * @ClassName Red
 * @Date 2020/2/8 11:58
 * @Version 1.0
 **/
public class Red implements Color {
    @Override
    public void fill() {
        System.out.println("红色");
    }
}

/**
 * @author yly
 * @ClassName Green
 * @Date 2020/2/8 11:58
 * @Version 1.0
 **/
public class Green implements Color {
    @Override
    public void fill() {
        System.out.println("绿色");
    }
}
/**
 * @author yly
 * @ClassName Blue
 * @Date 2020/2/8 11:59
 * @Version 1.0
 **/
public class Blue implements Color {
    @Override
    public void fill() {
        System.out.println("蓝色");
    }
}

(3)创建一个AbstractFactory抽象工厂类

/**
 * @author yly
 * @ClassName AbstractFactory
 * @Date 2020/2/8 11:59
 * @Version 1.0
 **/
public abstract class AbstractFactory {

    /**
     * 获取颜色
     * @param color
     * @return
     */
    public abstract Color getColor(String color);

    /**
     * 获取裙子
     * @param clothing
     * @return
     */
    public abstract Clothing getClothing(String clothing);
}

(4)创建实现AbstractFactory工厂的实体类ClothingFactory和ColorFactory

/**
 * @author yly
 * @ClassName ClothingFactory
 * @Date 2020/2/17 19:07
 * @Version 1.0
 **/
public class ClothingFactory extends AbstractFactory {

    @Override
    public Color getColor(String color) {
        return null;
    }
    
    @Override
    public Clothing getClothing(String clothing) {
        if (clothing==null){
            return null;
        }
        if (clothing.equalsIgnoreCase("Shirt")) {
            return new Shirt();
        } else if (clothing.equalsIgnoreCase("Shorts")) {
            return new Shorts();
        } else if (clothing.equalsIgnoreCase("Skirt")) {
            return new Skirt();
        }
        return null;
    }
}
/**
 * @author yly
 * @ClassName ColorFactory
 * @Date 2020/2/8 12:05
 * @Version 1.0
 **/
public class ColorFactory extends AbstractFactory {
    @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;
    }

    @Override
    public Clothing getClothing(String clothing) {
        return null;
    }
}

(5)创建一个工厂创造器FactoryProducer

/**
 * @author yly
 * @ClassName FactoryProducer
 * @Date 2020/2/8 12:07
 * @Version 1.0
 **/
public class FactoryProducer {

    public static AbstractFactory getFactory(String choice){
   		 if (choice == null) {
            return null;
	     }
		 if("COLOR".equalsIgnoreCase(choice)){
            return new ColorFactory();
        }else if ("Clothing".equalsIgnoreCase(choice)){
            return new ClothingFactory();
        }
        return null;
    }
}

(6)使用 FactoryProducer 来获取 AbstractFactory。

/**
 * @author yly
 * @ClassName AbstractFactoryPatternDemo
 * @Date 2020/2/8 12:07
 * @Version 1.0
 **/
public class AbstractFactoryPatternDemo {
   public static void main(String[] args) {
      //获取颜色工厂
      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();
      //获取服饰工厂
      AbstractFactory clothing = FactoryProducer.getFactory("Clothing");
      //获取服饰为Shirt的对象
      Clothing shirt = clothing.getClothing("Shirt");
      //调用Clothing的make方法
      shirt.make();
      //获取服饰为Shorts的对象
      Clothing shorts = clothing.getClothing("Shorts");
      //调用Clothing的make方法
      shorts.make();
      //获取服饰为Skirt的对象
      Clothing skirt = clothing.getClothing("Skirt");
      //调用Clothing的make方法
      skirt.make();
   }
}

(7)执行结果

红色
绿色
蓝色
衬衫
短裤
裙子

优点:一个产品族中多个对象被设计在一起工作,抽象工厂模式可以始终只使用同一个产品族中的对象
缺点:产品族扩展困难

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值