Example #1
0
        static void Main(string[] args)
        {
            Light light = new Light(new Off());

            Console.WriteLine("状态模式:");
            Console.WriteLine("电灯状态:");
            light.PressSwich();
            light.PressSwich();
            light.PressSwich();

            Console.WriteLine();
            Console.WriteLine("访问者模式:");
            //药类型
            Medicine a = new MedicineA("阿莫西林", 19.9, 3);
            Medicine b = new MedicineB("土黄霉素", 5.3, 2);
            //药单(将所需药品添加到药单中)
            Presciption presciption = new Presciption();

            presciption.add(a);
            presciption.add(b);
            Visitor charge           = new Charge("张三");           //划价员访问者
            Visitor workerOfPharmacy = new WorkerOfPharmacy("李四"); //药房工作者访问者

            presciption.accpet(charge);                            //划价
            presciption.accpet(workerOfPharmacy);                  //抓药

            Console.WriteLine();
            Console.WriteLine("观察者模式:");
            //订阅号
            Blog WeiBo = new MyBlog("微博", "发布了一篇新文章");

            // 添加订阅者
            WeiBo.AddObserver(new Subscriber("小张"));
            WeiBo.Update();
            Blog TengXun = new MyBlog("腾讯", "发布了一篇新闻");

            TengXun.AddObserver(new Subscriber("小赵"));
            //更新信息
            TengXun.Update();

            Console.WriteLine();
            Console.WriteLine("原型模式:");
            Resume x = new Resume("靖凡");

            x.SetPersonalInfo("男", "42");
            x.SetWorkExperience("2010--2018", "君和律师事务所");

            Resume y = (Resume)x.Clone();

            y.SetPersonalInfo("男", "34");
            y.SetWorkExperience("2005--2010", "利兹律师事务所");

            Resume z = (Resume)x.Clone();

            z.SetPersonalInfo("男", "31");
            z.SetWorkExperience("1998--2005", "明和律师事务所");

            x.Display(); //x公司
            y.Display(); //xx公司
            z.Display(); //xxx公司

            ConcretePrototype1 p1 = new ConcretePrototype1("124685");
            ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();

            Console.WriteLine("Cloned: {0}", c1.Id);
            ConcretePrototype2 p2 = new ConcretePrototype2("135472");
            ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();

            Console.WriteLine("Cloned: {0}", c2.Id);

            Console.WriteLine();
            Console.WriteLine("单例模式:");
            Singleton singleton1 = Singleton.Instance;

            singleton1.GetShow();
            Singleton singleton2 = Singleton.Instance;

            singleton2.GetShow();
            if (singleton1 == singleton2)
            {
                Console.WriteLine("ok");
            }
            else
            {
                Console.WriteLine("no");
            }
            Console.ReadKey();
        }
Example #2
0
 public override void visitor(MedicineA a)
 {
     Console.WriteLine("划价员:" + this.name + "划价、给药:" + a.GetName() + ",单价:" + a.GetPrice() + ",总价格:" + a.GetSum());
 }
Example #3
0
 public override void visitor(MedicineA a)
 {
     Console.WriteLine("药房工作者:" + this.name + ",抓药:" + a.GetName() + ",数量:" + a.GetCourt());
 }
Example #4
0
 //访问者访问药品A、药品B
 public abstract void visitor(MedicineA a);