练习|原型模式

本文探讨了设计模式中的原型模式原理,通过C++代码示例详细解释了如何使用拷贝构造函数实现对象的克隆,展示了如何快速创建多个相同实例的过程。

原型模式:当需要创建多个参数相同的实例时,只需要创建一个,其他的调用clone()即可。clone():

创建当前对象的浅表副本。方法创建一个新对象,然后将当前对象的非静态字段复制到该新对象。若当前对象是值类型,则逐位复制;若为引用或指针,则复制引用或指针而不复制对象。——《大话设计模式》

用C++实现模型模式可以直接用拷贝构造函数

#include <iostream>
#include <string>
using namespace std;

class Resume
{
public:
	Resume(): _name("??"), _exper("??") { }
	Resume(const string& name, const string& exper): _name(name), _exper(exper) { }
	Resume(const Resume& r) { _name = r._name; _exper = r._exper; }
	string get_name() const { return _name; }
	string get_exper() const { return _exper; }
private:
	string _name, _exper;
};

int main()
{
	Resume r1, r2, r3;
	r1 = Resume("irene", "none");
	r2 = Resume(r1);
	r3 = Resume(r1);
	cout << "the experience of " << r1.get_name() << " is " << r1.get_exper() << endl;
	cout << "the experience of " << r2.get_name() << " is " << r2.get_exper() << endl;
	cout << "the experience of " << r3.get_name() << " is " << r3.get_exper() << endl;
	return 0;
}

输出:

the experience of irene is none
the experience of irene is none
the experience of irene is none

### Java原型模式实验示例及相关资料 #### 什么是原型模式原型模式是一种创建型设计模式,允许通过复制现有对象来创建新对象,而无需深入了解其具体类。这种方式可以显著提高性能并简化复杂对象的创建过程[^3]。 在Java中实现原型模式通常涉及以下几个方面: 1. 定义一个接口 `Cloneable` 并重写方法 `clone()`。 2. 使用深拷贝或浅拷贝技术完成对象克隆操作。 以下是基于“头哥”教程风格的一个简单示例: ```java // 抽象产品角色:定义 clone 方法 public abstract class Product implements Cloneable { private String name; public Product(String name) { this.name = name; } public String getName() { return name; } @Override public Object clone() throws CloneNotSupportedException { System.out.println("Cloning product: " + this.getName()); return super.clone(); } } // 具体产品A public class ConcreteProductA extends Product { public ConcreteProductA(String name) { super(name); } } // 具体产品B public class ConcreteProductB extends Product { public ConcreteProductB(String name) { super(name); } } ``` 下面是一个简单的测试程序展示如何使用这些类: ```java public class PrototypeTest { public static void main(String[] args) throws CloneNotSupportedException { // 创建原始对象 Product originalA = new ConcreteProductA("Prototype A"); Product clonedA = (Product) originalA.clone(); Product originalB = new ConcreteProductB("Prototype B"); Product clonedB = (Product) originalB.clone(); // 输出验证 System.out.println("Original A Name: " + originalA.getName()); System.out.println("Cloned A Name: " + clonedA.getName()); System.out.println("Original B Name: " + originalB.getName()); System.out.println("Cloned B Name: " + clonedB.getName()); } } ``` 此代码展示了如何利用原型模式快速生成相同类型的实例[^4]。 #### 关于‘头哥’的教学资源 虽然未直接提及具体的“头哥”教程内容,但在许多面向初学者的设计模式讲解视频或文档中,“头哥”的课程常以其清晰易懂的特点受到广泛欢迎。这类材料一般会提供详细的理论解释以及实际编码练习指导[^5]。 如果希望深入学习包括但不限于原型模式在内的各种经典设计模式应用技巧,推荐查阅由该讲师制作的相关系列讲座或者书籍版本。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值