首页 > 代码库 > 设计模式之抽象工厂模式

设计模式之抽象工厂模式

---恢复内容开始---

软件设计模式中的抽象工厂模式是基于普通工厂模式的工厂模式。该模式的设计目标是为了在产品生产的规模达到一定数量级的时候使用普通的工厂模式生让生产整体的框架变得笨重,及涉及到多维度的开发的时候。

一、接口类

  1. IFactory.java
    package interpackage;
    
    /**
     * Created by Administrator on 2016/10/26.
     */
    
    import produce.ChapterProduce;
    import produce.MeduimProduce;
    import produce.SupperProduce;
    
    /**
     * 定义此接口作为工厂的抽象封装好工厂所具备的抽象方法
     * 包括生产SupperProduce、MeduimProduce、ChapterProduce三种类型的产品,并分别返回
     */
    
    public interface IFactory {
    
    
        public ChapterProduce produceChapter();
    
        public MeduimProduce produceMedium();
    
        public SupperProduce produceSupper();
    
        public String showFactory();
    
    
    }

     

  2. IProduce.java
    package interpackage;
    
    /**
     * Created by Administrator on 2016/10/26.
     */
    
    /**
    
    展示产品等级类型的接口
     */
    public interface IProduce {
    
        public String showCode();
    }

    二、工厂类DellFactory.java

    package factory;
    
    import interpackage.IFactory;
    import produce.ChapterProduce;
    import produce.MeduimProduce;
    import produce.SupperProduce;
    
    /**
     * Created by Administrator on 2016/10/26.
     */
    public class DellFatory implements IFactory {
        @Override
        public ChapterProduce produceChapter() {
            return new ChapterProduce();
        }
    
        @Override
        public MeduimProduce produceMedium() {
            return new MeduimProduce();
        }
    
        @Override
        public SupperProduce produceSupper() {
            return new SupperProduce();
        }
    
        //展示工厂的品牌
        @Override
        public String  showFactory(){
            return "Thanks for enjoy DellFactory";
        }
    
    }

    HP工厂类HPFactory.java

    package factory;
    
    import interpackage.IFactory;
    import produce.ChapterProduce;
    import produce.MeduimProduce;
    import produce.SupperProduce;
    
    /**
     * Created by Administrator on 2016/10/26.
     */
    public class HPFactory implements IFactory {
        @Override
        public ChapterProduce produceChapter() {
            return new ChapterProduce();
        }
    
        @Override
        public MeduimProduce produceMedium() {
            return new MeduimProduce();
        }
    
        @Override
        public SupperProduce produceSupper() {
            return new SupperProduce();
        }
    
    
        //展示工厂的品牌
        @Override
        public String  showFactory(){
            return "Thanks for enjoy HPFactory";
        }
    }

    产品类

  3. ChapterProduce.java
    package produce;
    
    import interpackage.IProduce;
    
    /**
     * Created by Administrator on 2016/10/26.
     */
    public class ChapterProduce implements IProduce {
    
    
        @Override
        public String showCode() {
            return "The produce code is Chapter";
        }
    }

    MeduimProduce.java

    package produce;
    
    import interpackage.IProduce;
    
    /**
     * Created by Administrator on 2016/10/26.
     */
    public class MeduimProduce implements IProduce {
    
    
        @Override
        public String showCode() {
            return "The produce code is Meduim";
        }
    }

    SupperProduce.java

    package produce;
    
    import interpackage.IProduce;
    
    /**
     * Created by Administrator on 2016/10/26.
     */
    
    /**
     定义实现了产品接口的类作为等级为Supper的类
     */
    public class SupperProduce implements IProduce {
        @Override
        public String showCode() {
            return "The produce code is Supper";
        }
    }

    测试类(Test.java)

    package test;
    
    import factory.DellFatory;
    import factory.HPFactory;
    import interpackage.IFactory;
    
    /**
     * Created by Administrator on 2016/10/26.
     */
    public class ProduceTest {
    
        public static void main(String args[]){
    
            //将抽象工厂实例化为Dell工厂
    
            IFactory dell=new DellFatory();
    
            //将抽象工厂实例化为HP工厂
            IFactory hp=new HPFactory();
    
    
            //输出产品品牌和产品等级
    
            System.out.println(dell.showFactory()+"\t"+dell.produceChapter().showCode());
            System.out.println(hp.showFactory()+"\t"+hp.produceChapter().showCode());
    
        }
    
    }

     

---恢复内容结束---

设计模式之抽象工厂模式