设计模式(七)——原型模式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Dream_Ryoma/article/details/82985196

原型模式的核心就是java中的Cloneable接口。

示例如下:建设银行国庆期间给所有用户发送信用卡消费抽奖信息。

package PrototypePattern;
/**
 * 邮件实体
 */
public class Mail{

    //收件人
    private String receiver;
    
    //主题
    private String subject;
    
    //内容
    private String context;
    
    //称谓
    private String appellation;
    
    //邮件尾部
    private String tail;
    
    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }

    public String getAppellation() {
        return appellation;
    }

    public void setAppellation(String appellation) {
        this.appellation = appellation;
    }

    public String getTail() {
        return tail;
    }

    public void setTail(String tail) {
        this.tail = tail;
    }
    
}
package PrototypePattern;

import java.util.Random;

public class RunDemo {

    private static int MAX_COUNT = 5;//实际情况中这个数应该是很大很大的

    public static void main(String[] args) throws CloneNotSupportedException {
        int i = 0;
        Mail mail = new Mail();
        mail.setSubject("建设银行国庆信用卡抽奖活动");
        mail.setContext("国庆抽奖活动通知:刷卡消费超过999,就可以享受抽奖资格,拿限量礼品");
        mail.setTail("建设银行版权所有!");
        while (i < MAX_COUNT) {
            mail.setAppellation(getRandStr(5) + "先生/女士");
            mail.setReceiver(getRandStr(5) + "@" + getRandStr(8) + ".com");
            sendMail(mail);
            i++;
        }
    }

    public static void sendMail(Mail mail) {
        System.out.println("标题:" + mail.getSubject() + "\t收件人:" + mail.getReceiver() + "\t....发送成功!");
    }

    public static String getRandStr(int length) {
        String source = "abcdefghijklmnopqrskuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        StringBuffer sb = new StringBuffer();
        Random rand = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(source.charAt(rand.nextInt(source.length())));
        }
        return sb.toString();
    }

}

该示例中邮件的组成标签很少,而且每个标签的内容很短。而实际情况中,邮件的组成标签远不止这么几个,而且每个标签的内容会长很多,邮件中的某些标签的内容也是要从数据库中查取的(耗时),所以发送一封邮件会很耗时。那怎么办呢?可能有人会观察到,该示例是单线程的,可以改成多线程。没错,但是改成多线程又会有线程安全,一封邮件还没有发送,另一封邮件就会把收件人和称谓改掉。好,解决方法是用线程安全。本节采用原型模式来解决这个发邮件的问题。

邮件实体继承Cloneable接口,重写clone方法。

package PrototypePattern;

/**
 * 邮件实体
 */
public class Mail implements Cloneable {

    // 收件人
    private String receiver;

    // 主题
    private String subject;

    // 内容
    private String context;

    // 称谓
    private String appellation;

    // 邮件尾部
    private String tail;

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }

    public String getAppellation() {
        return appellation;
    }

    public void setAppellation(String appellation) {
        this.appellation = appellation;
    }

    public String getTail() {
        return tail;
    }

    public void setTail(String tail) {
        this.tail = tail;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        Mail mail = null;
        try {
            mail = (Mail) super.clone();
        } catch (CloneNotSupportedException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return mail;
    }

}
package PrototypePattern;

import java.util.Random;

public class RunDemo {

    private static int MAX_COUNT = 5;// 实际情况中这个数应该是很大很大的

    public static void main(String[] args) throws CloneNotSupportedException {
        int i = 0;
        Mail mail = new Mail();
        mail.setSubject("建设银行国庆信用卡抽奖活动");
        mail.setContext("国庆抽奖活动通知:刷卡消费超过999,就可以享受抽奖资格,拿限量礼品");
        mail.setTail("建设银行版权所有!");
        while (i < MAX_COUNT) {
            /* ************************************* */
            /* *********** 克隆原有对象(大,复杂)********* */
            /* ************************************* */
            Mail mailClone = (Mail) mail.clone();
            mailClone.setAppellation(getRandStr(5) + "先生/女士");
            mailClone.setReceiver(getRandStr(5) + "@" + getRandStr(8) + ".com");
            sendMail(mailClone);
            i++;
        }
    }

    public static void sendMail(Mail mail) {
        System.out.println("标题:" + mail.getSubject() + "\t收件人:" + mail.getReceiver() + "\t....发送成功!");
    }

    public static String getRandStr(int length) {
        String source = "abcdefghijklmnopqrskuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        StringBuffer sb = new StringBuffer();
        Random rand = new Random();
        for (int i = 0; i < length; i++) {
            sb.append(source.charAt(rand.nextInt(source.length())));
        }
        return sb.toString();
    }

}

java中的clone方法有很多注意事项,详情参见 java克隆注意事项 。

原型模式的使用场景:(1)类初始化需要消耗非常多的资源,包括数据、硬件等(2)new一个对象需要很繁琐的数据准备或者访问权限(3)一个对象需要提供给其他对象访问,并且各个调用者可能都需要修改其值

参考文档:设计模式|菜鸟教程,《设计模式之禅》

上一篇:设计模式(六)——建造者模式

猜你喜欢

转载自blog.csdn.net/Dream_Ryoma/article/details/82985196