File tree Expand file tree Collapse file tree
DesignPatterns/Prototype/Prototype Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Text ;
5+ using System . Threading . Tasks ;
6+
7+ namespace Prototype
8+ {
9+ class DeepRestrationInfo
10+ {
11+ private string name ;
12+ private string birthday ;
13+ private string school ;
14+ private string id ;
15+ private Nationality nationality ;
16+
17+ public string Name
18+ {
19+ get
20+ {
21+ return name ;
22+ }
23+
24+ set
25+ {
26+ name = value ;
27+ }
28+ }
29+
30+ public string Birthday
31+ {
32+ get
33+ {
34+ return birthday ;
35+ }
36+
37+ set
38+ {
39+ birthday = value ;
40+ }
41+ }
42+
43+ public string School
44+ {
45+ get
46+ {
47+ return school ;
48+ }
49+
50+ set
51+ {
52+ school = value ;
53+ }
54+ }
55+
56+ public string Id
57+ {
58+ get
59+ {
60+ return id ;
61+ }
62+
63+ set
64+ {
65+ id = value ;
66+ }
67+ }
68+
69+ //含参构造函数,供普通创建过程使用
70+ public DeepRestrationInfo ( string name )
71+ {
72+ this . name = name ;
73+ nationality = new Nationality ( ) ;
74+ }
75+
76+ //供克隆方法使用的私有构造函数
77+ private DeepRestrationInfo ( Nationality nation )
78+ {
79+ this . nationality = nation . Clone ( ) as Nationality ;
80+ }
81+
82+ //展示报名信息
83+ public void Show ( )
84+ {
85+ Console . WriteLine ( "姓名:" + name ) ;
86+ Console . WriteLine ( "出生年月:" + birthday ) ;
87+ Console . WriteLine ( "毕业院校:" + school ) ;
88+ Console . WriteLine ( "身份证号:" + id ) ;
89+ Console . WriteLine ( "国籍:" + nationality . Nation ) ;
90+ }
91+
92+ public void SetNation ( string nation )
93+ {
94+ nationality . Nation = nation ;
95+ }
96+ public object Clone ( )
97+ {
98+ return MemberwiseClone ( ) ;
99+ }
100+
101+ public object DeepClone ( )
102+ {
103+ DeepRestrationInfo obj = new DeepRestrationInfo ( this . nationality ) ;
104+ obj . name = this . name ;
105+ obj . birthday = this . birthday ;
106+ obj . id = this . id ;
107+ obj . school = this . school ;
108+ return obj ;
109+ }
110+ }
111+ }
Original file line number Diff line number Diff line change 1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Text ;
5+ using System . Threading . Tasks ;
6+
7+ namespace Prototype
8+ {
9+ class Nationality : ICloneable
10+ {
11+ private String nation ;
12+
13+ public string Nation
14+ {
15+ get
16+ {
17+ return nation ;
18+ }
19+
20+ set
21+ {
22+ nation = value ;
23+ }
24+ }
25+
26+ public object Clone ( )
27+ {
28+ return this . MemberwiseClone ( ) ;
29+ }
30+ }
31+ }
Original file line number Diff line number Diff line change @@ -32,7 +32,24 @@ static void Main(string[] args)
3232 Console . WriteLine ( "报名一级厨师" ) ;
3333 RestrationInfo restartion2 = restrationInfo1 . Clone ( ) as RestrationInfo ;
3434 restartion2 . Show ( ) ;
35+ //原型模式举例说明End
3536
37+
38+ //原型模式举例说明2
39+ Console . WriteLine ( "报名面点师" ) ;
40+ DeepRestrationInfo deepRestrationInfo1 = new DeepRestrationInfo ( "小面" ) ;
41+ deepRestrationInfo1 . Birthday = "1992-2-2" ;
42+ deepRestrationInfo1 . School = "清华大学" ;
43+ deepRestrationInfo1 . Id = "001" ;
44+ deepRestrationInfo1 . SetNation ( "美国" ) ;
45+
46+ //报名厨师
47+ Console . WriteLine ( "报名一级厨师" ) ;
48+ DeepRestrationInfo deepRestartion2 = deepRestrationInfo1 . DeepClone ( ) as DeepRestrationInfo ;
49+ deepRestartion2 . SetNation ( "中国" ) ;
50+ deepRestrationInfo1 . Show ( ) ;
51+ deepRestartion2 . Show ( ) ;
52+ //原型模式举例说明End
3653 }
3754 }
3855
Original file line number Diff line number Diff line change 4343 <Reference Include =" System.Xml" />
4444 </ItemGroup >
4545 <ItemGroup >
46+ <Compile Include =" DeepRestrationInfo.cs" />
47+ <Compile Include =" Nationality.cs" />
4648 <Compile Include =" Program.cs" />
4749 <Compile Include =" Properties\AssemblyInfo.cs" />
4850 <Compile Include =" Prototype.cs" />
You can’t perform that action at this time.
0 commit comments