zoukankan      html  css  js  c++  java
  • 简单工厂模式

    普通做法:

          

    package com.zhen.build_template.simple_factory.general;
    
    /**
     * @author zhen
     * @Date 2019/5/28 11:13
     */
    public interface Sender {
        public void send();
    }
    
    package com.zhen.build_template.simple_factory.general;
    
    /**
     * @author zhen
     * @Date 2019/5/28 11:13
     */
    public class MailSender implements Sender{
        @Override
        public void send() {
            System.out.println("this is mailSender");
        }
    }
    
    package com.zhen.build_template.simple_factory.general;
    
    /**
     * @author zhen
     * @Date 2019/5/28 11:14
     */
    public class SmsSender implements Sender {
        @Override
        public void send() {
            System.out.println("this is SmsSender");
        }
    }
    
    package com.zhen.build_template.simple_factory.general;
    
    /**
     * @author zhen
     * @Date 2019/5/28 11:15
     */
    public class SendFactory {
        public Sender getSenderInstance(String type){
            if ("mail".equals(type)){
                return new MailSender();
            }else if("sms".equals(type)){
                return new SmsSender();
            }else{
                System.out.println("请输入正确的类型");
                return null;
            }
        }
    }
    
    package com.zhen.build_template.simple_factory.general;
    
    /**
     * @author zhen
     * @Date 2019/5/28 11:17
     */
    public class FactoryTest {
        public static void main(String[] args) {
            SendFactory factory = new SendFactory();
            Sender sender = factory.getSenderInstance("sms");
            sender.send();
        }
    }
    普通简单工厂

    普通简单工厂有一个问题:

         如果传入的类型是错误的,则得不到对象

    多方法类型做法:

    package com.zhen.build_template.simple_factory.many_method;
    
    
    /**
     * @author zhen
     * @Date 2019/5/28 11:15
     */
    public class SendFactory {
        public Sender getSmsSenderInstance(){
                return new SmsSender();
        }
    
        public Sender getMailSenderInstance(){
            return new MailSender();
        }
    }
    
    package com.zhen.build_template.simple_factory.many_method;
    
    /**
     * @author zhen
     * @Date 2019/5/28 11:17
     */
    public class FactoryTest {
        public static void main(String[] args) {
            SendFactory factory = new SendFactory();
            Sender sender = factory.getMailSenderInstance();
            sender.send();
        }
    }
    
    package com.zhen.build_template.simple_factory.many_method;
    
    /**
     * @author zhen
     * @Date 2019/5/28 11:13
     */
    public interface Sender {
        public void send();
    }
    
    package com.zhen.build_template.simple_factory.many_method;
    
    /**
     * @author zhen
     * @Date 2019/5/28 11:13
     */
    public class MailSender implements Sender {
        @Override
        public void send() {
            System.out.println("this is mailSender");
        }
    }
    
    package com.zhen.build_template.simple_factory.many_method;
    
    /**
     * @author zhen
     * @Date 2019/5/28 11:14
     */
    public class SmsSender implements Sender {
        @Override
        public void send() {
            System.out.println("this is SmsSender");
        }
    }
    多方法类型简单工厂

     使用多个方法解决了传入错误类型得不到对象的情况,但是每次创造对象都依赖工厂实例

    多静态方法做法:

    package com.zhen.build_template.simple_factory.many_static_method;
    
    /**
     * @author zhen
     * @Date 2019/5/28 11:13
     */
    public interface Sender {
        public void send();
    }
    
    package com.zhen.build_template.simple_factory.many_static_method;
    
    /**
     * @author zhen
     * @Date 2019/5/28 11:13
     */
    public class MailSender implements Sender {
        @Override
        public void send() {
            System.out.println("this is mailSender");
        }
    }
    
    package com.zhen.build_template.simple_factory.many_static_method;
    
    /**
     * @author zhen
     * @Date 2019/5/28 11:14
     */
    public class SmsSender implements Sender {
        @Override
        public void send() {
            System.out.println("this is SmsSender");
        }
    }
    
    package com.zhen.build_template.simple_factory.many_static_method;
    
    
    /**
     * @author zhen
     * @Date 2019/5/28 11:15
     */
    public class SendFactory {
        public static Sender getSmsSenderInstance(){
                return new SmsSender();
        }
    
        public static Sender getMailSenderInstance(){
            return new MailSender();
        }
    }
    
    
    package com.zhen.build_template.simple_factory.many_static_method;
    
    /**
     * @author zhen
     * @Date 2019/5/28 11:17
     */
    public class FactoryTest {
        public static void main(String[] args) {
            Sender sender = SendFactory.getMailSenderInstance();
            sender.send();
        }
    }
    多静态方法型简单工厂

      使用的静态方法方式解决了依赖工厂实例的问题,一般简单工厂都采用第三种。

  • 相关阅读:
    NoSQL数据库 Couchbase Server
    百度推广账户搭建思路
    禅道发邮件配置
    ASP 500错误解决方法
    MYSQL无法连接,提示10055错误尝试解决
    制作不随浏览器滚动的DIV-带关闭按钮
    [CSS3] :nth-child的用法
    [JS] 四角度旋转特效
    [JS] 瀑布流加载
    [CSS3] 二级下拉导航
  • 原文地址:https://www.cnblogs.com/aigeileshei/p/10942194.html
Copyright © 2011-2022 走看看