构造器模式 BuilderPattern

构建理想型 NiceGirl 类实例
本文介绍了一个使用 C# 编写的 NiceGirl 类及构建器模式的应用案例。该类包含多个属性,如姓名、身高、体重等,并通过 NiceGirlBuilder 类进行实例化。文章展示了如何设置这些属性的具体值并打印出 NiceGirl 对象的完整信息。

using System; public class NiceGirl { private string _Name; private string _Tall; private string _SkinColor; private string _HairColor; private string _Weight; private string _Colthes; private string _Shoes; private string _Bag; public NiceGirl() { } public string Name { get { return _Name; } set { _Name = value; } } public string Tall { get { return _Tall; } set { _Tall = value; } } public string SkinColor { get { return _SkinColor; } set { _SkinColor = value; } } public string HairColor { get { return _HairColor; } set { _HairColor = value; } } public string Weight { get { return _Weight; } set { _Weight = value; } } public string Colthes { get { return _Colthes; } set { _Colthes = value; } } public string Shoes { get { return _Shoes; } set { _Shoes = value; } } public string Bag { get { return _Bag; } set { _Bag = value; } } public override string ToString() { string decorationList = string.Empty; decorationList += "Name:" + this.Name + "/n" + "Tall:" + this.Tall + "/n" + "SkinColor:" + this.SkinColor + "/n" + "HairColor:" + this.HairColor + "/n" + "Weight:" + this.Weight + "/n" + "Clothes:" + this.Colthes + "/n" + "Shoes:" + this.Shoes + "/n" + "Bag:" + this.Bag; return decorationList; } }

待构造的对象

using System; public class NiceGirlBuilder { private NiceGirl _NiceGirl; public NiceGirlBuilder() { } public void createNiceGirl() { _NiceGirl = new NiceGirl(); _NiceGirl.Name = "露西"; _NiceGirl.Tall = "170CM"; _NiceGirl.Weight = "55KG"; _NiceGirl.SkinColor = "奶白色"; _NiceGirl.HairColor = "酒红色"; _NiceGirl.Shoes = "水晶色的低跟鞋"; _NiceGirl.Bag = "牛仔帆布袋"; } public NiceGirl getNiceGirl() { return _NiceGirl; } }

构造器

using System; using System.Collections.Generic; using System.Text; namespace BuilderPattern2 { class Program { static void Main(string[] args) { NiceGirlBuilder ngb = new NiceGirlBuilder(); ngb.createNiceGirl(); NiceGirl ng = ngb.getNiceGirl(); Console.WriteLine(ng.ToString()); Console.ReadLine(); } } }

调用代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值