原型模式
Last updated
Last updated
package prototype
// 原型对象需要实现的接口
type Cloneable interface {
Clone() Cloneable
}
type User struct {
name string
age int
}
func (u User) Clone() Cloneable {
return u
}package prototype
import (
"fmt"
"strconv"
"testing"
)
func Test(t *testing.T) {
user := User{}
user.name = "init"
usermap := make(map[string]User)
for i := 1; i < 10; i++ {
u := user.Clone().(User)
u.name = strconv.Itoa(i)
u.age = i
usermap[strconv.Itoa(i)] = u
}
fmt.Println(user)
fmt.Println(usermap)
}package prototype
//Cloneable 是原型对象需要实现的接口
type Cloneable interface {
Clone() Cloneable
}
type PrototypeManager struct {
prototypes map[string]Cloneable
}
func NewPrototypeManager() *PrototypeManager {
return &PrototypeManager{
prototypes: make(map[string]Cloneable),
}
}
func (p *PrototypeManager) Get(name string) Cloneable {
return p.prototypes[name]
}
func (p *PrototypeManager) Set(name string, prototype Cloneable) {
p.prototypes[name] = prototype
}package prototype
import "testing"
var manager *PrototypeManager
type Type1 struct {
name string
}
func (t *Type1) Clone() Cloneable {
tc := *t
return &tc
}
type Type2 struct {
name string
}
func (t *Type2) Clone() Cloneable {
tc := *t
return &tc
}
func TestClone(t *testing.T) {
t1 := manager.Get("t1")
t2 := t1.Clone()
if t1 == t2 {
t.Fatal("error! get clone not working")
}
}
func TestCloneFromManager(t *testing.T) {
c := manager.Get("t1").Clone()
t1 := c.(*Type1)
if t1.name != "type1" {
t.Fatal("error")
}
}
func init() {
manager = NewPrototypeManager()
t1 := &Type1{
name: "type1",
}
manager.Set("t1", t1)
}package tech.selinux.design.pattern.creational.prototype;
public class Mail implements Cloneable {
private String name;
private String emailAddress;
private String content;
public Mail() {
System.out.println("Mail Class Constructor");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "Mail{"
+ "name='"
+ name
+ '\''
+ ", emailAddress='"
+ emailAddress
+ '\''
+ ", content='"
+ content
+ '\''
+ '}'
+ super.toString();
}
@Override
protected Object clone() throws CloneNotSupportedException {
System.out.println("clone mail object");
return super.clone();
}
}package tech.selinux.design.pattern.creational.prototype;
import java.text.MessageFormat;
public class MailUtil {
public static void sendMail(Mail mail) {
String outputContent = "向{0}用户,邮件地址:{1},邮件内容:{2}发送邮件成功";
System.out.println(
MessageFormat.format(
outputContent, mail.getName(), mail.getEmailAddress(), mail.getContent()));
}
public static void saveOriginMailRecord(Mail mail) {
System.out.println("存储originMail记录,originMail:" + mail.getContent());
}
}package tech.selinux.design.pattern.creational.prototype;
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
Mail mail = new Mail();
mail.setContent("初始化模板");
System.out.println("初始化mail:" + mail);
for (int i = 0; i < 10; i++) {
Mail mailTemp = (Mail) mail.clone();
mailTemp.setName("姓名" + i);
mailTemp.setEmailAddress("姓名" + i + "@selinux.tech");
mailTemp.setContent("恭喜您,此次中奖了");
MailUtil.sendMail(mailTemp);
System.out.println("克隆的mailTemp:" + mailTemp);
}
MailUtil.saveOriginMailRecord(mail);
}
}package tech.selinux.design.pattern.creational.prototype.clone;
import java.util.Date;
public class User implements Cloneable {
private String name;
private Date birthday;
public User(String name, Date birthday) {
this.name = name;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
protected Object clone() throws CloneNotSupportedException {
User user = (User) super.clone();
// 深克隆
user.birthday = (Date) user.birthday.clone();
return user;
}
@Override
public String toString() {
return "User{" + "name='" + name + '\'' + ", birthday=" + birthday + '}' + super.toString();
}
}package tech.selinux.design.pattern.creational.prototype.clone;
import java.util.Date;
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
Date birthday = new Date(0L);
User user = new User("佩奇", birthday);
User user1 = (User) user.clone();
System.out.println(user);
System.out.println(user1);
user.getBirthday().setTime(666666666666L);
System.out.println(user);
System.out.println(user1);
}
}package tech.selinux.design.pattern.creational.prototype.abstractprototype;
public abstract class A implements Cloneable {
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}package tech.selinux.design.pattern.creational.prototype.abstractprototype;
public class B extends A {
public static void main(String[] args) throws CloneNotSupportedException {
B b = new B();
b.clone();
}
}