Android设计模式之工厂方法模式

工厂方法模式

1.定义:

定义一个用于创建对象的接口,让子类决定实例化哪个类。

2.使用场景:

在任何需要生成复杂对象的地方,都可以使用工厂方法模式。复杂对象适合使用工厂模式,用new就可以完成创建的对象无需使用工厂模式。

3.UML图

4.详解:

工厂方法模式,分为普通工厂、多个工厂、静态工厂三种,下面将一一介绍。
首先需要定义一个通用的产品类接口(这里没有按ISender写法),如下:

public interface Sender {
        void send();
    }

接着需要具体的产品实现类,如下:

    public static class MailSender implements Sender {
        @Override
        public void send() {
            System.out.println("this is mail sender!");
        }
    }

    public static class QQSender implements Sender {
        @Override
        public void send() {
            System.out.println("this is qq sender!");
        }
    }
4.1普通工厂模式

就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建

public static class SendFactory {
        public Sender create(String type) {
            if ("mail".equals(type)) {
                return new MailSender();
            } else if ("qq".equals(type)) {
                return new QQSender();
            } else {
                System.out.println("type is not match");
                return null;
            }
        }
    }
4.2多个工厂模式

是对普通工厂方法模式的改进,在普通工厂方法模式中,如果传递的字符串出错,则不能正确创建对象,而多个工厂方法模式是提供多个工厂方法,分别创建对象。

public static class MultiFactory {
        public MailSender createMail() {
            return new MailSender();
        }

        public QQSender createQQ() {
            return new QQSender();
        }
    }
4.3静态工厂模式

将多个工厂方法模式里的方法置为静态的,不需要创建实例,直接调用即可。

public static class StaticFactory {
        public static MailSender createMail() {
            return new MailSender();
        }

        public static QQSender createQQ() {
            return new QQSender();
        }
    }
4.4测试方法:
public static void main(String[] args) {
        SendFactory sendFactory = new SendFactory();
        sendFactory.create("qq").send();
        sendFactory.create("mail").send();
        sendFactory.create("bmw");

        System.out.println("============below is multi factory================");
        MultiFactory multiFactory = new MultiFactory();
        multiFactory.createMail().send();
        multiFactory.createQQ().send();

        System.out.println("=============below is static factory===============");
        StaticFactory.createMail().send();
        StaticFactory.createQQ().send();

        /**
         * 输出结果:
         this is qq sender!
         this is mail sender!
         type is not match
         ============below is multi factory================
         this is mail sender!
         this is qq sender!
         =============below is static factory===============
         this is mail sender!
         this is qq sender!
         */
    }
}

5.代码托管地址

工厂方法模式

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 设计模式汇总 一、基础知识 1. 设计模式概述 定义:设计模式(Design Pattern)是一套被反复使用、多...
    MinoyJet阅读 3,970评论 1 15
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,765评论 18 399
  • 一、设计模式的分类 总体来说设计模式分为三大类: 创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者...
    RamboLI阅读 772评论 0 1
  • 看了这位哥们关于rac的文章,有所启发然后上代码: RACCommand 是什么 1. 定义 RACCommand...
    踏云小子阅读 3,609评论 0 6
  • MIDI文件属于二进制文件,这种文件一般都有如下基本结构: 文件头+数据描述 文件头一般包括文件的类型,因为Mid...
    钱鑫_9771阅读 1,499评论 0 2