`

设计模式----原型模式(prototype pattern)

 
阅读更多
AdvTemplate.java代码如下:
package com.designPattern.prototype;

public class AdvTemplate {

	private String advSubject = "title";
	
	private String advContext = "context";

	public String getAdvSubject() {
		return advSubject;
	}

	public String getAdvContext() {
		return advContext;
	}

}



Mail.java代码如下:
 package com.designPattern.prototype;

/**
 * 
 * @author yangjianzhou
 * 使用原型模式时需要注意的几个问题:
 * 1.对象拷贝时,类的构造函数是不会被执行的
 * 2.浅拷贝与深拷贝的问题
 * 3.clone与final的关系
 */
public class Mail implements Cloneable{

	private String receiver ;
	
	private String subject;
	
	private String appellation;
	
	private String context;
	
	private String tail;
	
	public Mail(AdvTemplate advTemplate){
		this.context = advTemplate.getAdvContext();
		this.subject = advTemplate.getAdvSubject();
	}
	
	@Override
	public Mail clone(){
		Mail mail = null;
		try{
			mail = (Mail)super.clone();
		}catch(CloneNotSupportedException e){
			e.printStackTrace();
		}
		
		return mail;
	}

	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 getAppellation() {
		return appellation;
	}

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

	public String getContext() {
		return context;
	}

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

	public String getTail() {
		return tail;
	}

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



Client.java代码如下:
package com.designPattern.prototype;

import java.util.Random;

public class Client {
	
	private static int MAX_COUNT = 6;
	
	public static void main(String[] args) {
		int i=0;
		Mail mail = new Mail(new AdvTemplate());
		mail.setTail("XX银行版权所有");
		while(i<MAX_COUNT){
			Mail cloneMail = mail.clone();
			cloneMail.setAppellation(getRandString(5)+"先生(女士)");
			cloneMail.setReceiver(getRandString(5)+"@"+getRandString(8)+".com");
			sendMail(cloneMail);
			i++;
		}
	}
	
	public static void sendMail(Mail mail){
		System.out.println("标题:"+mail.getSubject()+"\t收件人:"+mail.getReceiver()+"\t发送成功");
	}
	
	public static String getRandString(int maxLength){
		String source = "abcdefghijklmnopqrstuvwxyz";
		
		StringBuffer sb = new StringBuffer();
		Random rand = new Random();
		for(int i=0;i<maxLength;i++){
			sb.append(source.charAt(rand.nextInt(source.length())));
		}
		
		return sb.toString();
	}

}



运行结果如下:
标题:title	收件人:xuzya@sooosxsu.com	发送成功
标题:title	收件人:areqo@krcmvkva.com	发送成功
标题:title	收件人:wyeyy@mlocbsqt.com	发送成功
标题:title	收件人:ezdkj@fnrmbsnp.com	发送成功
标题:title	收件人:nmuuz@vsvhxgxi.com	发送成功
标题:title	收件人:yrgqu@ggomwgtj.com	发送成功

分享到:
评论

相关推荐

    Prototype Pattern原型模式

    原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

    design-pattern-java.pdf

    (二) 确保对象的唯一性——单例模式 (三) 确保对象的唯一性——单例模式 (四) 确保对象的唯一性——单例模式 (五) 原型模式-Prototype Pattern 对象的克隆——原型模式(一) 对象的克隆——原型模式(二) ...

    原型模式 Prototype Pattern

    原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

    设计模式面面观(10):桥接模式(Bridge Pattern)-结构型模式

    (100%) 设计模式面面观(5):抽象工厂模式(AbstractFactory)-创建型模式 (100%) 设计模式面面观(6):生成器模式(Builder)-创建型模式 (100%) 设计模式面面观(7):原型模式(Prototype)-...

    原型模式(Prototype Pattern)原理图

    原型模式是一种创建型设计模式,它通过复制一个现有的对象来创建新的对象,而不是通过调用构造函数的方式。这种方式可以在运行时动态地创建和修改对象,而不需要知道具体的创建细节 。 原型模式的基本概念包括以下...

    design-pattern-test:设计模式学习

    design-pattern-test 设计模式学习练习 模式可以分为三大类:创建型模式(Creational Patterns)、结构型模式(Structural Patterns)、行为型模式(Behavioral Patterns) 序号 模式&描述 包括 1 创建型模式---...

    用Java实现23种设计模式

    用Java实现23种设计模式 1. 创建型模式 工厂模式(Factory Pattern) 抽象工厂模式(Abstract Factory Pattern) 单例模式(Singleton Pattern) 建造者模式(Builder Pattern) 原型模式(Prototype Pattern)...

    [创建型模式]设计模之原型模式(Prototype Pattern)

    NULL 博文链接:https://jacky-dai.iteye.com/blog/2295379

    C#设计模式.PDF

    C#设计模式(9)-Prototype Pattern 70 一、 原型(Prototype)模式 70 二、 Prototype模式的结构: 71 三、 程序举例: 71 四、 带Prototype Manager的原型模式 73 五、 浅拷贝与深拷贝 77 六、 Prototype模式的...

    .NET设计模式(6):原型模式(PrototypePattern)

    .NET设计模式(6):原型模式(PrototypePattern)

    33种JAVA设计模式DEMO

    原型模式(Prototype Pattern) 2 结构型模式 这些设计模式关注类和对象的组合。继承的概念被用来组合接口和定义组合对象获得新功能的方式。 适配器模式(Adapter Pattern) 桥接模式(Bridge Pattern) 过滤器模式...

    设计模式_原型模式.zip

    原型模式(Prototype Pattern)是用于创建重复的对象,同时又能保证性能。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。 这种模式是实现了一个原型接口,该接口用于创建当前对象的克隆。当...

    C++设计模式(Design Pattern)范例源代码

    23种设计模式(Design Pattern)的C++实现范例,包括下面列出的各种模式,代码包含较详细注释。另外附上“设计模式迷你手册.chm” 供参考。 注:项目在 VS2008 下使用。 创建型: 抽象工厂模式(Abstract Factory) ...

    C#版 24种设计模式

    适配器模式(Adapter Pattern) 提供者模式(Provider Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 原型模式(Prototype Pattern) 责任链模式(Chain of Responsibility Pattern) 中介者模式...

    C#设计模式大全

    C#设计模式(9)-Prototype Pattern 一、 原型(Prototype)模式 二、 Prototype模式的结构: 三、 程序举例: 四、 带Prototype Manager的原型模式 五、 浅拷贝与深拷贝 六、 Prototype模式的优点与缺点 C#...

    设计模式PPT

     原型模式(Prototype Pattern)  单例模式(Singleton Pattern) 结构型模式用来处理类或者对象的组合,主要包含以下7种设计模式:  适配器模式(Adapter Pattern)  桥接模式(Bridge Pattern)  组合...

    单例模式源码java-DesignPattern:在个人自学阶段的23种设计模式代码的全部实现,全部使用Java编写,其中还包括各个设计模式在

    在个人自学阶段的23种设计模式代码的全部实现,全部使用Java编写,其中还包括各个设计模式在源码中的使用,每种设计模式都举了一个简单的小例子来进行实现,并加以注释 包名解释 一、DesignPattern 1.1 创建型模式 ...

    uu-design-pattern:23种设计模式案例

    23种设计模式演示代码文件结构图gof23 |- creational(创建型模式) |- simplefactory 简单工厂模式案例 |- factorymethod 工厂方法模式案例 |- abstractfactory 抽象工厂模式案例 |- builder 建造者模式案例 |- ...

    java设计模式源码-DesignPattern:设计模式(Java实现源码)

    原型模式(prototypePattern) 适配器模式(adapterPattern) 桥接模式(bridgePattern) 过滤器模式(filterPattern) 组合模式(compositePattern) 装饰器模式(decoratorPattern) 外观模式(facadePattern) 享元模式...

    C#设计模式_设计模式_C#_

    原型模式(Prototype)结构型: 6. 适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式...

Global site tag (gtag.js) - Google Analytics