石家庄最好的网站建设公司,网站开展营销的思路和方法,vx小程序怎么做,深圳 网站建设 公司文章目录 1. 概念1.1 简述1.2 角色1.3 类图 2. 代码示例2.1 设计2.2 代码2.3 类图 1. 概念
1.1 简述
用原型实例指定创建对象的种类#xff0c;并且通过拷贝这些原型创建新的对象
1.2 角色
Prototype#xff08;抽象原型类#xff09;#xff1a;它是声明克隆方法的接口… 文章目录 1. 概念1.1 简述1.2 角色1.3 类图 2. 代码示例2.1 设计2.2 代码2.3 类图 1. 概念
1.1 简述
用原型实例指定创建对象的种类并且通过拷贝这些原型创建新的对象
1.2 角色
Prototype抽象原型类它是声明克隆方法的接口或所有具体原型类的公共父类 它可以是抽象类也可以是接口甚至还可以是具体实现类。 ConcretePrototype具体原型类它实现在抽象原型类中声明的克隆方法在克隆方法中返回自己的一个克隆对象。Client客户类 让一个原型对象克隆自身从而创建一个新的对象只需要直接实例化或通过工厂方法等方式创建一个原型对象再通过调用该对象的克隆方法即可得到多个相同的对象。 1.3 类图 #mermaid-svg-jZRCjMCUxrYCTohx {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-jZRCjMCUxrYCTohx .error-icon{fill:#552222;}#mermaid-svg-jZRCjMCUxrYCTohx .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-jZRCjMCUxrYCTohx .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-jZRCjMCUxrYCTohx .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-jZRCjMCUxrYCTohx .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-jZRCjMCUxrYCTohx .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-jZRCjMCUxrYCTohx .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-jZRCjMCUxrYCTohx .marker{fill:#333333;stroke:#333333;}#mermaid-svg-jZRCjMCUxrYCTohx .marker.cross{stroke:#333333;}#mermaid-svg-jZRCjMCUxrYCTohx svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-jZRCjMCUxrYCTohx g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-jZRCjMCUxrYCTohx g.classGroup text .title{font-weight:bolder;}#mermaid-svg-jZRCjMCUxrYCTohx .nodeLabel,#mermaid-svg-jZRCjMCUxrYCTohx .edgeLabel{color:#131300;}#mermaid-svg-jZRCjMCUxrYCTohx .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-jZRCjMCUxrYCTohx .label text{fill:#131300;}#mermaid-svg-jZRCjMCUxrYCTohx .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-jZRCjMCUxrYCTohx .classTitle{font-weight:bolder;}#mermaid-svg-jZRCjMCUxrYCTohx .node rect,#mermaid-svg-jZRCjMCUxrYCTohx .node circle,#mermaid-svg-jZRCjMCUxrYCTohx .node ellipse,#mermaid-svg-jZRCjMCUxrYCTohx .node polygon,#mermaid-svg-jZRCjMCUxrYCTohx .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-jZRCjMCUxrYCTohx .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-jZRCjMCUxrYCTohx g.clickable{cursor:pointer;}#mermaid-svg-jZRCjMCUxrYCTohx g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-jZRCjMCUxrYCTohx g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-jZRCjMCUxrYCTohx .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-jZRCjMCUxrYCTohx .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-jZRCjMCUxrYCTohx .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-jZRCjMCUxrYCTohx .dashed-line{stroke-dasharray:3;}#mermaid-svg-jZRCjMCUxrYCTohx #compositionStart,#mermaid-svg-jZRCjMCUxrYCTohx .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-jZRCjMCUxrYCTohx #compositionEnd,#mermaid-svg-jZRCjMCUxrYCTohx .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-jZRCjMCUxrYCTohx #dependencyStart,#mermaid-svg-jZRCjMCUxrYCTohx .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-jZRCjMCUxrYCTohx #dependencyStart,#mermaid-svg-jZRCjMCUxrYCTohx .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-jZRCjMCUxrYCTohx #extensionStart,#mermaid-svg-jZRCjMCUxrYCTohx .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-jZRCjMCUxrYCTohx #extensionEnd,#mermaid-svg-jZRCjMCUxrYCTohx .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-jZRCjMCUxrYCTohx #aggregationStart,#mermaid-svg-jZRCjMCUxrYCTohx .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-jZRCjMCUxrYCTohx #aggregationEnd,#mermaid-svg-jZRCjMCUxrYCTohx .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-jZRCjMCUxrYCTohx .edgeTerminals{font-size:11px;}#mermaid-svg-jZRCjMCUxrYCTohx :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Client Prototype Clone() : Prototype ConcretePrototype Clone() : Prototype 2. 代码示例
2.1 设计
创建一个将军类 它有4个方法设置人员信息、设置装备、查询、克隆 调用 实例化一个将军关羽用关羽的克隆方法实例化继承人用继承人的设置人员信息方法修改继承人的信息
2.2 代码
package mainimport fmt// 定义抽象原型
type Prototype interface {Clone() Prototype
}
//定义具体原型
type ConcretePrototype struct {Name stringModelA stringModelB string
}func (c *ConcretePrototype) Clone() Prototype {pN : ConcretePrototype{Name: c.Name,ModelA: c.ModelA,ModelB: c.ModelB,}return pN
}func main() {//实例化一个原型prototype : ConcretePrototype{Name: liubei,ModelA: AAA,ModelB: BBB,}//用原型复制两个实例cloneA : prototype.Clone()cloneB : prototype.Clone()//查看结果fmt.Println(cloneA, cloneB)
}2.3 类图 #mermaid-svg-MElT6D2Ik0BZrfZr {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-MElT6D2Ik0BZrfZr .error-icon{fill:#552222;}#mermaid-svg-MElT6D2Ik0BZrfZr .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-MElT6D2Ik0BZrfZr .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-MElT6D2Ik0BZrfZr .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-MElT6D2Ik0BZrfZr .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-MElT6D2Ik0BZrfZr .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-MElT6D2Ik0BZrfZr .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-MElT6D2Ik0BZrfZr .marker{fill:#333333;stroke:#333333;}#mermaid-svg-MElT6D2Ik0BZrfZr .marker.cross{stroke:#333333;}#mermaid-svg-MElT6D2Ik0BZrfZr svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-MElT6D2Ik0BZrfZr g.classGroup text{fill:#9370DB;fill:#131300;stroke:none;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-MElT6D2Ik0BZrfZr g.classGroup text .title{font-weight:bolder;}#mermaid-svg-MElT6D2Ik0BZrfZr .nodeLabel,#mermaid-svg-MElT6D2Ik0BZrfZr .edgeLabel{color:#131300;}#mermaid-svg-MElT6D2Ik0BZrfZr .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-MElT6D2Ik0BZrfZr .label text{fill:#131300;}#mermaid-svg-MElT6D2Ik0BZrfZr .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-MElT6D2Ik0BZrfZr .classTitle{font-weight:bolder;}#mermaid-svg-MElT6D2Ik0BZrfZr .node rect,#mermaid-svg-MElT6D2Ik0BZrfZr .node circle,#mermaid-svg-MElT6D2Ik0BZrfZr .node ellipse,#mermaid-svg-MElT6D2Ik0BZrfZr .node polygon,#mermaid-svg-MElT6D2Ik0BZrfZr .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-MElT6D2Ik0BZrfZr .divider{stroke:#9370DB;stroke:1;}#mermaid-svg-MElT6D2Ik0BZrfZr g.clickable{cursor:pointer;}#mermaid-svg-MElT6D2Ik0BZrfZr g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-MElT6D2Ik0BZrfZr g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-MElT6D2Ik0BZrfZr .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-MElT6D2Ik0BZrfZr .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-MElT6D2Ik0BZrfZr .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-MElT6D2Ik0BZrfZr .dashed-line{stroke-dasharray:3;}#mermaid-svg-MElT6D2Ik0BZrfZr #compositionStart,#mermaid-svg-MElT6D2Ik0BZrfZr .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-MElT6D2Ik0BZrfZr #compositionEnd,#mermaid-svg-MElT6D2Ik0BZrfZr .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-MElT6D2Ik0BZrfZr #dependencyStart,#mermaid-svg-MElT6D2Ik0BZrfZr .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-MElT6D2Ik0BZrfZr #dependencyStart,#mermaid-svg-MElT6D2Ik0BZrfZr .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-MElT6D2Ik0BZrfZr #extensionStart,#mermaid-svg-MElT6D2Ik0BZrfZr .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-MElT6D2Ik0BZrfZr #extensionEnd,#mermaid-svg-MElT6D2Ik0BZrfZr .extension{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-MElT6D2Ik0BZrfZr #aggregationStart,#mermaid-svg-MElT6D2Ik0BZrfZr .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-MElT6D2Ik0BZrfZr #aggregationEnd,#mermaid-svg-MElT6D2Ik0BZrfZr .aggregation{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-MElT6D2Ik0BZrfZr .edgeTerminals{font-size:11px;}#mermaid-svg-MElT6D2Ik0BZrfZr :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Client Prototype Clone() : Prototype ConcretePrototype String Name String ModelA String ModelB Clone() : Prototype