原型模式又称对象复制模式,浅拷贝的时候,一般采用Object类的clone()方法.
给出如下场景: 人做某种运动.
1.建立抽象类 人
2.建立抽象类 运动
3.建立场景管理类,通过管理类实现场景
java代码如下:
package com.liu.pattern6;public class PRototype { public static void main(String[] args) { // TODO Auto-generated method stub Manager.play(new Man("张三","15"), new Run("跑步")); } }class Manager{ public static void play(Person p,Activity a){ Person p2 = (Person) p.clone(); Activity a2 = (Activity)a.clone(); System.out.println(p2.toString() + a2.toString()); }}abstract class Person implements Cloneable { public Person(String name,String age){ this.name =name; this.age = age; } private String name; private String age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public Object clone(){ try { return super.clone(); } catch (CloneNotSupportedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public abstract String toString();}class Man extends Person{ public Man(String name, String age) { super(name, age); // TODO Auto-generated constructor stub } @Override public String toString() { // TODO Auto-generated method stub return super.getName()+"("+super.getAge()+")"; } }abstract class Activity implements Cloneable{ public Activity(String name){ this.name = name; } private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Object clone(){ try { return super.clone(); } catch (CloneNotSupportedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public abstract String toString();}class Run extends Activity{ public Run(String name) { super(name); // TODO Auto-generated constructor stub } @Override public String toString() { // TODO Auto-generated method stub return "在进行"+super.getName()+"的运动"; } }
c#代码如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace com.liu.pattern6{ class Prototype { static void Main(string[] args) { Manager.play(new Man("张三", "15"), new Run("跑步")); } } class Manager { public static void play(Person p, Activity a){ Person p2 = (Person) p.clone(); Activity a2 = (Activity)a.clone(); System.Console.WriteLine(p2.toString() + a2.toString()); System.Console.ReadKey(); } } abstract class Person { private String _name; private String _age; public String Name{ set{ _name = value; } get{ return _name; } } public String Age{ set{ _age = value; } get{ return _age; } } public Person(String name,String age){ Name =name; Age = age; } public Object clone(){ return base.MemberwiseClone(); } public abstract String toString(); } class Man : Person{ public Man(String name, String age):base(name,age) { } public override String toString() { // TODO Auto-generated method stub return base.Name+"("+base.Age+")"; } } abstract class Activity{ public Activity(String name){ Name = name; } private String _name; public String Name { set { _name = value; } get { return _name; } } public Object clone(){ return base.MemberwiseClone(); } public abstract String toString(); } class Run : Activity{ public Run(String name):base(name) { // TODO Auto-generated constructor stub } public override String toString() { // TODO Auto-generated method stub return "在进行" + base.Name+ "的运动"; } }}
新闻热点
疑难解答