예제 #1
0
        static void Main(string[] args)
        {
            //定义原型管理器
            NoodleManager noodleManager = new NoodleManager();

            //客户要求下面三碗面
            Noodle beefNoodle = (Noodle)noodleManager["牛肉拉面"].Clone();
            //Noodle beefNoodle=(Noodle)noodleManager["牛肉拉面"].DeepClone();
            Noodle muttonNoodle  = (Noodle)noodleManager["羊肉拉面"].Clone();
            Noodle beefCutNoodle = (Noodle)noodleManager["牛肉刀削面"].Clone();

            //修改克隆对象中的引用对象的属性,验证它是浅拷贝还是深拷贝
            beefNoodle.TbName = " 克隆对象已改名";

            //显示原始对象的NoodelName和TbName
            Console.WriteLine(noodleManager["牛肉拉面"].NoodleName
                              + noodleManager["牛肉拉面"].TbName + "\n");
            //显示克隆对象的NoodleName和TbName
            Console.WriteLine(beefNoodle.NoodleName + beefNoodle.TbName + "\n");


            // 将新的产品加入原型管理器,以备以后克隆时使用,
            // 下面是定义了一种新的面条-羊肉刀削面,
            // 并把它添加到面条管理器中,如果以后再有客户点这个面,直接克隆即可。
            noodleManager["羊肉刀削面"] = new CutNoodle("羊肉刀削面");

            //克隆一碗羊肉刀削面
            Noodle muttonCutNoodle = (Noodle)noodleManager["羊肉刀削面"].Clone();

            Console.WriteLine(noodleManager["羊肉刀削面"].NoodleName + "\n");
            Console.WriteLine(muttonCutNoodle.NoodleName + "\n");
            Console.ReadLine();
        }
예제 #2
0
 public NoodleManager()
 {
     //初始化时加入三种基本原型
     noodle = new PullNoodle("牛肉拉面");
     noodleHt.Add("牛肉拉面", noodle);
     noodle = new PullNoodle("羊肉拉面");
     noodleHt.Add("羊肉拉面", noodle);
     noodle = new CutNoodle("牛肉刀削面");
     noodleHt.Add("牛肉刀削面", noodle);
 }