构造器模式 BuilderPattern

类图

构造器模式 BuilderPattern_第1张图片

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(); } } }

调用代码

你可能感兴趣的:(String,Class)