static void RunVisitor() { // 多对多的关系 // Setup structure ObjectStructure o = new ObjectStructure(); o.Attach(new ConcreteElementA()); o.Attach(new ConcreteElementB()); // Create visitor objects ConcreteVisitor1 v1 = new ConcreteVisitor1(); ConcreteVisitor2 v2 = new ConcreteVisitor2(); // Structure accepting visitors o.Accept(v1); o.Accept(v2); }
static void Main(string[] args) { // Setup structure ObjectStructure o = new ObjectStructure(); o.Attach(new ConcreteElementA()); o.Attach(new ConcreteElementB()); // Create visitor objects ConcreteVisitor1 v1 = new ConcreteVisitor1(); ConcreteVisitor2 v2 = new ConcreteVisitor2(); // Structure accepting visitors o.Accept(v1); o.Accept(v2); Console.ReadLine(); }
public static void Main(String[] args) { // //Singleton Pattern // var sing1 = Singleton.Instance(); // var sing2 = Singleton.Instance(); // // Console.WriteLine(sing1 == sing2 ? "Same Object" : "Different Object"); // // sing1.StrName = "Ant"; // sing2.StrName = "Bird"; // Console.WriteLine("sing1: {0}, sing2: {1}", sing1.StrName, sing2.StrName); // // Wait for dispaly // Console.ReadKey(); // // //Template Method // var aA = new ConcreteClassA(); // aA.TemplateMethod(); // var aB = new ConcreteClassB(); // aB.TemplateMethod(); // var aC = new ConcreteClassC(); // aC.TemplateMethod(); // // Wait for dispaly // Console.ReadKey(); // // Composite root = new Composite("root"); // root.Add(new Leaf("Leaf A")); // root.Add(new Leaf("Leaf B")); // // Composite comp = new Composite("Composite X"); // comp.Add(new Leaf("Leaf XA")); // comp.Add(new Leaf("Leaf XB")); // // root.Add(comp); // root.Add(new Leaf("Leaf C")); // // // Add and remove a leaf // Leaf leaf = new Leaf("Leaf D"); // root.Add(leaf); // root.Remove(leaf); // // // Recursively display tree // root.Display(1); // // Wait for dispaly // Console.ReadKey(); // // //AbstractFactory // AbstractFactory factory1 = new ConcreteFactory1(); // Client client1 = new Client(factory1); // client1.Run(); // // // Abstract factory #2 // AbstractFactory factory2 = new ConcreteFactory2(); // Client client2 = new Client(factory2); // client2.Run(); // // Wait for dispaly // Console.ReadKey(); // // //State // Context cA = new Context(new ConcreteStateA()); // Context cB = new Context(new ConcreteStateB()); // // Console.WriteLine("+++++++++++++++++++++++"); // cA.Request(); // Console.WriteLine("+++++++++++++++++++++++"); // cA.Request(); // Console.WriteLine("+++++++++++++++++++++++"); // cA.Request(); // Console.WriteLine("+++++++++++++++++++++++"); // cA.Request(); // Console.WriteLine("+++++++++++++++++++++++"); // cB.Request(); // Console.WriteLine("+++++++++++++++++++++++"); // cB.Request(); // Console.WriteLine("+++++++++++++++++++++++"); // cB.Request(); // Console.WriteLine("+++++++++++++++++++++++"); // cB.Request(); // Console.WriteLine("+++++++++++++++++++++++"); // // Wait for dispaly // Console.ReadKey(); // ConcreateAggregate conAggregate = new ConcreateAggregate(); // conAggregate[0] = "Item A"; // conAggregate[1] = "Item B"; // conAggregate[2] = "Item C"; // conAggregate[3] = "Item D"; // // // Create Iterator and provide aggregate // ConcreteIterator i = new ConcreteIterator(conAggregate); // // Console.WriteLine("Iterating over collection:"); // // object item = i.First(); // while (item != null) // { // Console.WriteLine(item); // item = i.Next(); // } // // // Wait for user // Console.ReadKey(); // Observer pattern // ConcreteSubject concreteSubject = new ConcreteSubject(); // // concreteSubject.Attach(new ConcreteObserver(concreteSubject, "X")); // concreteSubject.Attach(new ConcreteObserver(concreteSubject, "Y")); // concreteSubject.Attach(new ConcreteObserver(concreteSubject, "Z")); // // // Change subject and notify observers // concreteSubject.SubjectState = "ABCD"; // concreteSubject.Notify(); // // // Wait for user // Console.ReadKey(); // Facade facade = new Facade(); // // facade.MethodA(); // facade.MethodB(); // // // Wait for user // Console.ReadKey(); // Abstraction ab = new RefinedAbstraction(); // // // Set implementation and call // ab.Implementor = new ConcreteImplementorA(); // ab.Operation(); // // // Change implemention and call // ab.Implementor = new ConcreteImplementorB(); // ab.Operation(); // // // Wait for user // Console.ReadKey(); // ConcreteComponent c = new ConcreteComponent(); // ConcreteDecoratorA d1 = new ConcreteDecoratorA(); // ConcreteDecoratorB d2 = new ConcreteDecoratorB(); // // // Link decorators // d1.SetComponent(c); // d2.SetComponent(d1); // // d2.Operation(); // // // Wait for user // Console.ReadKey(); // Setup Chain of Responsibility // Handler h1 = new ConcreteHandler1(); // Handler h2 = new ConcreteHandler2(); // Handler h3 = new ConcreteHandler3(); // h1.SetSuccessor(h2); // h2.SetSuccessor(h3); // // // Generate and process request // int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 }; // // foreach (int request in requests) // { // h1.HandleRequest(request); // } // // // Wait for user // Console.ReadKey(); // Create receiver, command, and invoker // Receiver receiver = new Receiver(); // Command command = new ConcreteCommand(receiver); // Invoker invoker = new Invoker(); // // // Set and execute command // invoker.SetCommand(command); // invoker.ExecuteCommand(); // // // Wait for user // Console.ReadKey(); // ContextInterperter context = new ContextInterperter(); // // // Usually a tree // ArrayList list = new ArrayList(); // // // Populate 'abstract syntax tree' // list.Add(new TerminalExpression()); // list.Add(new NonterminalExpression()); // list.Add(new TerminalExpression()); // list.Add(new TerminalExpression()); // // // Interpret // foreach (AbstractExpression exp in list) // { // exp.Interperter(context); // } // // // Wait for user // Console.ReadKey(); // Setup structure ObjectStructure o = new ObjectStructure(); o.Attach(new ConcreteElementA()); o.Attach(new ConcreteElementB()); // Create visitor objects ConcreteVisitor1 v1 = new ConcreteVisitor1(); ConcreteVisitor2 v2 = new ConcreteVisitor2(); // Structure accepting visitors o.Accept(v1); o.Accept(v2); // Wait for user Console.ReadKey(); }
static void Main(string[] args) { //ThreadStart childref = new ThreadStart(CallToChildThread1); //Console.WriteLine("In Main: Creating the Child thread"); //Thread childThread = new Thread(childref); //childThread.Start(); //// 停止主线程一段时间 ////Thread.Sleep(2000); //// 现在中止子线程 //Console.WriteLine("In Main: Aborting the Child thread"); //childThread.Abort(); #region ---------- 遍历模式 ----------- //BookShelf bookShelf = new BookShelf(); //bookShelf.AppendBook(new Book("Around the World in 80 days")); //bookShelf.AppendBook(new Book("Bible")); //bookShelf.AppendBook(new Book("Cinderella")); //bookShelf.AppendBook(new Book("Daddy-Long-Legs")); //Iterator iterator = bookShelf.iterator(); //while(iterator.hasNext()) //{ // Book book = (Book)iterator.Next(); // Console.WriteLine(book.Name); //} #endregion #region ---------- 适配器 ------------- //类适配器 //IPrint print = new PrintBanner("Hello"); //print.printWeak(); //print.printStrong(); //对象适配器 //实例化一个适配器给目标接口 //Target target = new Adapter(); //下面的这些就是客户端可以被识别接口了 //target.GetTemperature(); //target.GetPressure(); //target.GetHumidity(); //target.GetUltraviolet(); #endregion #region ---------- 模板方法 ----------- //AbstractDisplay d1 = new CharDisplay('H'); //AbstractDisplay d2 = new StringDisplay("Hello,World"); //AbstractDisplay d3 = new StringDisplay("你好,中国"); //d1.Display(); //d2.Display(); //d3.Display(); #endregion #region ---------- 简单工厂 ----------- //Operation operFactory = OperationFactory.CreateOperation("+"); //operFactory.Number1 = 100; //operFactory.Number2 = 200; //double result = operFactory.GetResult(); #endregion #region ---------- 策略 --------------- //Context context; //context = new Context(new ConcreteStragegyA()); //context.ContextInterface(); //context = new Context(new ConcreteStragegyB()); //context.ContextInterface(); //context = new Context(new ConcreteStragegyC()); //context.ContextInterface(); #endregion #region ----------- 装饰模式 ---------- //Person xc = new Person("小菜"); //BigTrouser bt = new BigTrouser(); //Tshirts tshirt = new Tshirts(); //bt.Decorate(xc); //tshirt.Decorate(bt); //tshirt.Show(); #endregion #region ---------- 代理模式 ---------- //SchoolGirl xiaoHong = new SchoolGirl { Name = "小红" }; //Proxy proxy = new Proxy(xiaoHong); //proxy.GiveChocolate(); //proxy.GiveDolls(); //proxy.GiveFlowers(); #endregion #region -----------工厂方法模式 ------------- //IFactory operation = new AddFactory(); //MathOperation mathOperate= operation.CreateOperation(); //mathOperate.Number1 = 10; //mathOperate.Number2 = 20; //double result = mathOperate.GetResult(); #endregion #region ---------- 原型模式 ----------------- //Resume a = new Resume("大鸟"); //a.SetPersonalInfo(true, 27); //a.SetWorkExperience("1997-2000", "NBA"); //Resume b = a.Clone() as Resume; //b.SetWorkExperience("1998-2006","CCTV"); //Resume c = a.Clone() as Resume; //c.SetWorkExperience("1999-2004", "华为"); //a.Display(); //b.Display(); //c.Display(); #endregion #region ----------模板方法 ------------------ //Console.WriteLine("学生A的答案:"); //TestPaperA studentA = new TestPaperA(); //studentA.TestQuestion1(); //studentA.TestQuestion2(); //studentA.TestQuestion3(); //Console.WriteLine("学生B的答案:"); //TestPaperB studentB = new TestPaperB(); //studentB.TestQuestion1(); //studentB.TestQuestion2(); //studentB.TestQuestion3(); #endregion #region -----------建造者模式---------------- //Form1 form1 = new Form1(); //form1.ShowDialog(); #endregion #region ----------观察者模式----------------- //Boss lgs = new Boss(); //StockObserver zhj = new StockObserver("张海军", lgs); //NBAObserver zy = new NBAObserver("张严",lgs); //lgs.Update += new EventHandler(zhj.CloseStockMarket); //lgs.Update += new EventHandler(zy.CloseNBADirectSeeding); //lgs.SubjectState = "你们的Boss回来了"; //lgs.Notify(); //Secretary secretary = new Secretary(); //zhj = new StockObserver("张海军", secretary); //zy = new NBAObserver("张严", secretary); //secretary.Update += new EventHandler(zhj.CloseStockMarket); //secretary.Update += new EventHandler(zy.CloseNBADirectSeeding); //secretary.SubjectState = "注意:老板回来了"; //secretary.Notify(); #endregion #region ----------抽象工厂模式--------------- //User user = new User //{ // Name="AAA", // Id=1000001 //}; //Department department = new Department //{ // Id = 1, // Name = "生产部" //}; //IUser userTable= DataAccess.CreateUser(); //IDepartment departmentTable = DataAccess.CreateDepartment(); //userTable.Insert(user); //userTable.GetUser(1); //departmentTable.Insert(department); //departmentTable.GetDepart(1); #endregion #region -----------观察模式------------------ //Work emergencyProject = new Work(); //emergencyProject.Hour = 9; //emergencyProject.WriteProgram(); //emergencyProject.Hour = 10; //emergencyProject.WriteProgram(); //emergencyProject.Hour = 12; //emergencyProject.WriteProgram(); //emergencyProject.Hour = 13; //emergencyProject.WriteProgram(); //emergencyProject.Hour = 14; //emergencyProject.WriteProgram(); //emergencyProject.Hour = 17; //emergencyProject.TaskFinished = false; //emergencyProject.WriteProgram(); //emergencyProject.Hour = 19; //emergencyProject.WriteProgram(); //emergencyProject.Hour = 22; //emergencyProject.WriteProgram(); #endregion #region ----------适配器模式----------------- //Target1 target = new Adapter1(); //target.Request(); //Player b = new Fowards("巴蒂尔"); //b.Attack(); //Player m = new Guards("麦克格雷迪"); //m.Attack(); //Player a = new Center("YaoMing"); //a.Attack(); //a.Defense(); //Player wjzf = new Translator("姚明"); //wjzf.Attack(); //wjzf.Defense(); #endregion #region ----------备忘录模式----------------- //Originator o = new Originator(); //o.State = "On"; //o.Show(); //Caretaker c = new Caretaker(); //c.Memento = o.CreateMemento(); //o.State = "Off"; //o.Show(); //o.SetMemento(c.Memento); //o.Show(); #endregion #region ----------组合模式------------------- //Composite root = new Composite("root"); //root.Add(new Leaf("Leaf A")); //root.Add(new Leaf("Leaf B")); //Composite comp = new Composite("Composite X"); //comp.Add(new Leaf("Leaf XA")); //comp.Add(new Leaf("Leaf XB")); //root.Add(comp); //Composite comp1 = new Composite("Composite XY"); //comp1.Add(new Leaf("Leaf XYA")); //comp1.Add(new Leaf("Leaf XYB")); //comp.Add(comp1); //root.Add(new Leaf("Leaf C")); //Leaf leaf = new Leaf("Leaf D"); //root.Add(leaf); //root.Remove(leaf); //root.Display(1); #endregion #region -----------桥接模式------------------ //Abstraction ab = new RefinedAbstraction(); //ab.SetImplementor(new ConcreteImplementorA()); //ab.Operation(); //ab.SetImplementor(new ConcreteImplementorB()); //ab.Operation(); #endregion #region -----------命令模式------------------ //开店前的准备 //Barbecuer boy = new Barbecuer(); //Command bakeMuttonCmd1 = new BakeMuttonCommand(boy); //Command bakeMuttonCmd2 = new BakeMuttonCommand(boy); //Command bakeChikenWingCmd1 = new BakeChickenWingCommand(boy); //Waiter girl = new Waiter(); ////开门营业 //girl.SetOrder(bakeMuttonCmd1); //girl.SetOrder(bakeMuttonCmd2); //girl.SetOrder(bakeChikenWingCmd1); ////点菜完毕,通知厨房 //girl.Notify(); //Receiver r = new Receiver(); //接收者 //BaseCommand c = new ConcreteCommand(r); //具体命令 //Invoker i = new Invoker(); //命令执行者 //i.SetCommand(c); //关联命令 //i.ExecuteCommand(); //执行命令 #endregion #region -----------职责链模式---------------- //Handler h1 = new ConcreteHandler1(); //Handler h2 = new ConcreteHandler2(); //Handler h3 = new ConcreteHandler3(); ////设置职责链上家和下家 //h1.SetSuccessor(h2); //h2.SetSuccessor(h3); //int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 }; //foreach(int item in requests) //{ // h1.HandleRequest(item); //} //CommManager jinli = new CommManager("金利"); //Majordomo zongjian = new Majordomo("宗剑"); //GeneralManager zhongjingli = new GeneralManager("刘光胜"); //jinli.SetSuperior(zongjian); //zongjian.SetSuperior(zhongjingli); //Request request1 = new Request() //{ // RequestType = "请假", // RequestContent = "海军请假看病", // Number = 1 //}; //jinli.RequestApplication(request1); //Request request2 = new Request() //{ // RequestType = "请假", // RequestContent = "理政请假带小孩", // Number = 4 //}; //jinli.RequestApplication(request2); //Request request3 = new Request() //{ // RequestType = "加薪", // RequestContent = "海军请求加薪", // Number = 500 //}; //jinli.RequestApplication(request3); //Request request4 = new Request() //{ // RequestType = "加薪", // RequestContent = "海军请求加薪", // Number = 1000 //}; //jinli.RequestApplication(request4); #endregion #region ----------中介者模式----------------- //ConcreteMediator cm = new ConcreteMediator(); //ConcreteColleague1 c1 = new ConcreteColleague1(cm); //ConcreteColleague2 c2 = new ConcreteColleague2(cm); //cm.Colleague1 = c1; //cm.Colleague2 = c2; //c1.Send("吃过饭了吗?"); //c2.Send("没有呢,你打算请客?"); //UnitedNationSecurityCouncil unsc = new UnitedNationSecurityCouncil(); //USA usa = new USA(unsc); //Iraq iraq = new Iraq(unsc); //unsc.Colleague1 = usa; //unsc.Colleague2 = iraq; //usa.Declare("我要打你"); //iraq.Declare("来吧,我等着"); #endregion #region -----------享元模式------------------ //int extrinsicstate = 22; //FlyWeightFactory f = new FlyWeightFactory(); //FlyWeight fx = f.GetFlyWeight("X"); //fx.Operation(--extrinsicstate); //FlyWeight fy = f.GetFlyWeight("Y"); //fy.Operation(--extrinsicstate); //FlyWeight fz = f.GetFlyWeight("Z"); //fz.Operation(--extrinsicstate); //FlyWeight uf = new UnSharedConcreteFlyWeight(); //uf.Operation(--extrinsicstate); //WebsiteFactory wf = new WebsiteFactory(); //Website web1 = wf.GetWebsiteCategory("产品展示"); //web1.Use(new WebUser("ZS")); //Website web2 = wf.GetWebsiteCategory("产品展示"); //web2.Use(new WebUser("LS")); //Website web3 = wf.GetWebsiteCategory("产品展示"); //web3.Use(new WebUser("WW")); //Website web4 = wf.GetWebsiteCategory("博客"); //web4.Use(new WebUser("ZT")); //Website web5 = wf.GetWebsiteCategory("博客"); //web5.Use(new WebUser("WM")); //Website web6 = wf.GetWebsiteCategory("博客"); //web6.Use(new WebUser("ZP")); //Console.WriteLine("网站分类总数:"+wf.GetWebsiteCount()); #endregion #region -----------解释器模式---------------- //ExpressionContext context = new ExpressionContext(); //IList<AbstractExpression> list = new List<AbstractExpression>(); //list.Add(new TerminalExpression()); //list.Add(new NominalExpression()); //list.Add(new TerminalExpression()); //list.Add(new TerminalExpression()); //foreach(AbstractExpression exp in list) //{ // exp.Interpret(context); //} //PlayContext playContext = new PlayContext(); //Console.WriteLine("上海滩..."); //playContext.PlayText = "T 500 O 2 E 0.5 G 0.5 A 3 E 0.5 G 0.5 D 3 E 0.5 G 0.5 A 0.5 O 3 C 1 O 2 A 0.5 G 1 C 0.5 A 0.5 O 3 C 1 O 2 A 0.5 G 1 C 0.5 E 0.5 D 3"; //Expression expression = null; //try //{ // while(playContext.PlayText.Length>0) // { // string str = playContext.PlayText.Substring(0, 1); // switch(str) // { // case "O": // expression = new Scale(); // break; // case "T": // expression = new Speed(); // break; // case "C": // case "D": // case "E": // case "F": // case "G": // case "A": // case "B": // case "P": // expression = new Note(); // break; // } // expression.Interpret(playContext); // } //} //catch(System.Exception ex) //{ // Console.WriteLine(ex.Message); //} #endregion #region -----------访问者模式---------------- ObjectStructure os = new ObjectStructure(); os.Attach(new Man()); os.Attach(new Woman()); //成功时的反应 Success success = new Success(); os.Display(success); //失败时的反应 Failure failure = new Failure(); os.Display(failure); //恋爱时的反应 Love love = new Love(); os.Display(love); ObjectStructureNew osn = new ObjectStructureNew(); osn.Attach(new ConcreteElementA()); osn.Attach(new ConcreteElementB()); ConcreteVisitor1 cv1 = new ConcreteVisitor1(); osn.Accept(cv1); ConcreteVisitor2 cv2 = new ConcreteVisitor2(); osn.Accept(cv2); #endregion Console.ReadKey(); }