static void Main(string[] args) { #region Singleton Console.WriteLine("Singleton"); Console.WriteLine(""); DBConnectClassic serviceInstance1 = DBConnectClassic.GetInstance(); Console.WriteLine(serviceInstance1.GetCreationCount()); DBConnectClassic serviceInstance2 = DBConnectClassic.GetInstance(); Console.WriteLine(serviceInstance2.GetCreationCount()); DBConnectClassic1 serviceInstance3 = DBConnectClassic1.Instance; Console.WriteLine(serviceInstance3.GetCreationCount()); DBConnectClassic1 serviceInstance4 = DBConnectClassic1.Instance; Console.WriteLine(serviceInstance4.GetCreationCount()); DBConnectClassic2 serviceInstance5 = DBConnectClassic2.Instance2; Console.WriteLine(serviceInstance5.GetCreationCount()); DBConnectClassic2 serviceInstance6 = DBConnectClassic2.Instance2; Console.WriteLine(serviceInstance6.GetCreationCount()); Console.WriteLine(serviceInstance1 == serviceInstance2); Console.WriteLine(serviceInstance3 == serviceInstance4); Console.WriteLine(serviceInstance5 == serviceInstance6); Console.WriteLine(""); #endregion #region Factory Console.WriteLine("Factory"); Console.WriteLine(""); var a = AnimalFactory.Create(Animal.Dog); Console.WriteLine("Classe:" + a.GetType().Name); Console.WriteLine(""); #endregion #region AbstractFactory Console.WriteLine("DesignPatterns"); Console.WriteLine(""); Console.WriteLine("AbstractFactory"); ContinentFactory africa = new AfricaFactory(); AnimalWorld world = new AnimalWorld(africa); world.Animais(); Console.WriteLine(""); ContinentFactory america = new AmericaFactory(); //BRAZIL world = new AnimalWorld(america); world.Animais(); Console.WriteLine(""); #endregion #region Builder Console.WriteLine("Builder"); Console.WriteLine(""); BuildingsBuilder builders; Build builder = new Build(); builders = new ComercialBuilder(); builder.Construct(builders); builders.Building.Show(); Console.WriteLine(""); builders = new SchoolBuilder(); builder.Construct(builders); builders.Building.Show(); Console.WriteLine(""); #endregion #region Prototype Console.WriteLine("Prototype"); Console.WriteLine(""); Document d1 = new Document("test.doc"); Document c1 = (Document)d1.Clone(); Console.WriteLine($"d1 - original { d1.ShowName()}"); Console.WriteLine($"c1 - clone { c1.ShowName()}"); Console.WriteLine(""); #endregion #region Adapter Console.WriteLine("Adapter"); Console.WriteLine(""); Sockets s1pin = new Sockets("1Pin"); s1pin.Display(); Console.WriteLine(""); SocketType s2pin = new SocketType("2Pin"); s2pin.Display(); Console.WriteLine(""); SocketType s3pin = new SocketType("3Pin"); s3pin.Display(); Console.WriteLine(""); SocketType s3pinY = new SocketType("3PinY"); s3pinY.Display(); Console.WriteLine(""); #endregion #region Bridge Console.WriteLine("Bridge"); Console.WriteLine(""); string url = "www.facebook.com"; Sites site = new Sites("Firefox", url); site.Navegate(); Console.WriteLine(""); site = new Sites("Edge", url); site.Navegate(); Console.WriteLine(""); site = new Sites("Chrome", url); site.Navegate(); Console.WriteLine(""); #endregion #region Composite Console.WriteLine("Composite"); Console.WriteLine(""); Woman mother = new Woman(); var father = new Man(); var kid1 = new Man(); var kid2 = new Woman(); mother.AddFiliation(kid1); mother.AddFiliation(kid2); mother.Display(); Console.WriteLine(""); father.Display(); Console.WriteLine(""); #endregion #region Decorator Console.WriteLine("Decorator"); Console.WriteLine(""); Burguer burger = new Burguer("BBQ simples"); Bread bread = new Bread("Frances", 1, 1); Cheese cheese = new Cheese("Prato", 0.400, 2); Salada salada = new Salada("Alface", 1, 2); Steak hamburguer = new Steak("160g", 1, 2); Sauce sauce = new Sauce("BBQ", 1, 2); burger.SetIngredients(bread); burger.SetIngredients(cheese); burger.SetIngredients(salada); burger.SetIngredients(hamburguer); burger.SetIngredients(sauce); burger.MakeFood(); Console.WriteLine(""); Pizza pizza = new Pizza("Calabreza"); Batter batter = new Batter("Media", 1, 2); cheese = new Cheese("Mussarela", 0.500, 2); sauce = new Sauce("Tomatoe", 0.500, 2); Specialities specialities = new Specialities("Calabreza", 1, 3); pizza.SetIngredients(batter); pizza.SetIngredients(cheese); pizza.SetIngredients(sauce); pizza.SetIngredients(specialities); pizza.MakeFood(); Console.WriteLine(""); Massa massa = new Massa("Bolonhesa"); Pasta pasta = new Pasta("Spaguetti", 1, 3); cheese = new Cheese("Parmesão", 0.500, 2); sauce = new Sauce("Tomatoe", 0.500, 2); specialities = new Specialities("Carne Moida", 1, 3); massa.SetIngredients(pasta); massa.SetIngredients(cheese); massa.SetIngredients(sauce); massa.SetIngredients(specialities); massa.MakeFood(); Console.WriteLine(""); #endregion #region Facade Console.WriteLine("Facade"); Console.WriteLine(""); Facade facade = new Facade(); facade.Buy(); Console.WriteLine(""); #endregion #region FlyWeight Console.WriteLine("FlyWeight"); Console.WriteLine(""); Mover move = new Mover(); move.MoveTo("A.pdf", @"\\Vp1\c\Docs\"); Console.WriteLine(""); #endregion #region Proxy Console.WriteLine("Proxy"); Console.WriteLine(""); FinancialRFBR financialRFBR = new FinancialRFBR(); financialRFBR.PayLegal(19333.88, 15, "10000-1101010-1010201010"); Console.WriteLine(""); financialRFBR.PayPhysical(150.55, 10, "10000-1101010"); Console.WriteLine(""); #endregion #region ChainResponsability Console.WriteLine("ChainResponsability"); Console.WriteLine(""); GameTable gt = new GameTable(); Players p1 = new Players("Andre"); Players p2 = new Players("Bruna"); Players p3 = new Players("Cesar"); Players p4 = new Players("Danilo"); Players p5 = new Players("Ester"); Players p6 = new Players("Fabiana"); gt.AddPlayer(p1); gt.AddPlayer(p5); gt.AddPlayer(p3); gt.AddPlayer(p6); gt.AddPlayer(p4); gt.AddPlayer(p2); int NumberOfPlays = 5; gt.PlayGame(NumberOfPlays); Console.WriteLine(""); #endregion #region Strategy Console.WriteLine("Strategy"); Console.WriteLine(""); OrdenedArray vector = new OrdenedArray(); vector.Add("1"); vector.Add("3"); vector.Add("4"); vector.Add("7"); vector.Add("9"); vector.SetOrderStrategy(new NormalOrder()); vector.Order(); Console.WriteLine(""); vector.SetOrderStrategy(new ReverseOrder()); vector.Order(); Console.WriteLine(""); #endregion Console.WriteLine("Oh Glória!!"); Console.ReadLine(); }
static void Main(string[] args) { #region 简单工厂 var add = Factory.GetOperat("1"); var sub = Factory.GetOperat("2"); #endregion Console.WriteLine("------------------------------"); #region 单例模式 Singleton.GetInstance(); var s1 = Singleton1.Instance; var s2 = Singleton2.Instance; #endregion Console.WriteLine("------------------------------"); #region 抽象工程模式 // ef 打开和关闭 AbstractFactory efFactory = new EfFactory(); efFactory.CreateOpen().Print(); efFactory.CreateClose().Print(); // dapper 打开和关闭 AbstractFactory dapperFactory = new DapperFactory(); dapperFactory.CreateOpen().Print(); dapperFactory.CreateClose().Print(); #endregion Console.WriteLine("------------------------------"); #region 建造者模式 var director = new Director(); var saiyanBuilder = new SaiyanBuilder(); var naimBuilder = new NaimBuilder(); director.Construct(saiyanBuilder); // 组装赛亚人 var saiyanPerson = saiyanBuilder.GetPerson(); saiyanPerson.Show(); // 组装那美克人 director.Construct(naimBuilder); var naimPerson = naimBuilder.GetPerson(); naimPerson.Show(); #endregion Console.WriteLine("------------------------------"); #region 原型模式 var mingren1 = new MingrenPrototype(); var mingren2 = mingren1.Clone() as MingrenPrototype; //mingren1 负责攻击 mingren1.Attack(); //mingren2 负责保护 mingren2?.Protect(); #endregion Console.WriteLine("------------------------------"); #region 适配器模式 //类的适配器模式 var baiduMap = new BaiduAdapter(); baiduMap.Gen(); //对象的适配器模式 var baiduMap1 = new BaiduAdapter1(); baiduMap1.Gen(); #endregion Console.WriteLine("------------------------------"); #region 桥接模式 DbControlAbstract dbControlAbstract = new DbControl(); // Sql Server dbControlAbstract.Db = new SqlServerDb(); dbControlAbstract.Open(); dbControlAbstract.Add(); dbControlAbstract.Close(); // MySql dbControlAbstract.Db = new MySqlDb(); dbControlAbstract.Open(); dbControlAbstract.Add(); dbControlAbstract.Close(); #endregion Console.WriteLine("------------------------------"); #region 装饰者模式 // SqlServerDbHelper DbHelper dbHelper = new SqlServerDbHelper(); // check Decorator decorator = new CheckDecorator(dbHelper); decorator.Add(); #endregion Console.WriteLine("------------------------------"); #region 组合模式 //透明式 Car car = new Motorcycle(); car.Travel(); car.Two(new SuvCar()); car.Ten(new SuvCar()); car = new SuvCar(); car.Travel(); car.Two(new SuvCar()); car.Ten(new SuvCar()); //安全式 Car1 car1 = new Motorcycle1(); car1.Travel(); Car1 bus = new Bus(); bus.Travel(); ((FourCar)bus).Two(new Bus()); ((FourCar)bus).Ten(new Bus()); #endregion Console.WriteLine("------------------------------"); #region 外观模式 var facade = new Facade(); facade.Buy(); #endregion Console.WriteLine("------------------------------"); #region 享元模式 foreach (var item in new[] { "a", "b", "c", "a", "b" }) { var flyweight = FlyweightFactory.GetFlyweight(item); flyweight.Operation(1); } Console.WriteLine(FlyweightFactory.DicFlyweight.Count); #endregion Console.WriteLine("------------------------------"); #region 代理模式 var proxy = new Proxy(); proxy.Do("上网"); #endregion Console.WriteLine("------------------------------"); #region 模板方法模式 var chain = new ChinaBank(); chain.Get(); var shBank = new ShanghaiBank(); shBank.Get(); #endregion Console.WriteLine("------------------------------"); #region 命令模式 var r = new Receiver1(); var c = new CommandImp(r); var i = new Invoke(c); i.ExecuteCommand(); #endregion Console.WriteLine("------------------------------"); #region 迭代器模式 ICollection list = new Collection(); var iterator = list.GetIterator(); while (iterator.MoveNext()) { var j = (int)iterator.GetCurrent(); Console.WriteLine(j.ToString()); iterator.Next(); } #endregion Console.WriteLine("------------------------------"); #region 观察者模式 HousingDeveloper hd = new WuhanHousingDeveloper(); hd.Add(new NoHouseObserver("A")); hd.Add(new NoHouseObserver("B")); hd.Add(new NoHouseObserver("C")); hd.Add(new HasHouseObserver("D")); hd.Add(new HasHouseObserver("E")); hd.Add(new HasHouseObserver("F")); hd.Notify(); #endregion Console.WriteLine("------------------------------"); #region 中介者模式 var buyer = new Buyer(); var seller = new Seller(); var mediator = new MediatorImp(buyer, seller); buyer.MoneyChange(5, mediator); seller.MoneyChange(10, mediator); #endregion Console.WriteLine("------------------------------"); #region 状态模式 var user = new User("admin"); for (var j = 0; j < 10; j++) { user.Recharge(20 * j); } #endregion Console.WriteLine("------------------------------"); #region 策略模式 var context = new NotifyContext(new EmailStragety()); context.Send("新的消息"); context.Stragety = new SmsStragety(); context.Send("第二条消息"); #endregion Console.WriteLine("------------------------------"); #region 责任链模式 var leaveRequest1 = new LeaveRequest("张三", 1); var leaveRequest2 = new LeaveRequest("李四", 4); var leaveRequest3 = new LeaveRequest("王五", 7); var leaveRequest4 = new LeaveRequest("赵六", 11); Approver projectManager = new ProjectManager("项目管理者"); Approver departManager = new DepartManager("部门管理者"); Approver ceo = new Ceo("CEO"); // 设置责任链 projectManager.NextApprover = departManager; departManager.NextApprover = ceo; // 处理请求 projectManager.ProcessRequest(leaveRequest1); projectManager.ProcessRequest(leaveRequest2); projectManager.ProcessRequest(leaveRequest3); projectManager.ProcessRequest(leaveRequest4); #endregion Console.WriteLine("------------------------------"); #region 访问者模式 Visitor visitor = new VisitorImp(); var app = new AppStructure(visitor); FinancialManagement financialManagement = new Bank(); financialManagement.SaveMoney(); app.Process(financialManagement); financialManagement = new Yeb(); financialManagement.SaveMoney(); app.Process(financialManagement); financialManagement = new Jj(); financialManagement.SaveMoney(); app.Process(financialManagement); #endregion Console.WriteLine("------------------------------"); #region 备忘录模式 var persons = new List <ContactPerson>() { new ContactPerson { Name = "A", MobileNumber = "13533332222" }, new ContactPerson { Name = "B", MobileNumber = "13966554433" }, new ContactPerson { Name = "C", MobileNumber = "13198765544" } }; //手机名单发起人 var mobileOriginator = new MobileBackOriginator(persons); mobileOriginator.Show(); // 创建备忘录并保存备忘录对象 var manager = new MementoManager { ContactPersonMemento = mobileOriginator.CreateMemento() }; // 更改发起人联系人列表 Console.WriteLine("----移除最后一个联系人--------"); mobileOriginator.ContactPersonList.RemoveAt(2); mobileOriginator.Show(); // 恢复到原始状态 Console.WriteLine("-------恢复联系人列表------"); mobileOriginator.RestoreMemento(manager.ContactPersonMemento); mobileOriginator.Show(); #endregion #region 解释器模式 Console.WriteLine("------------------------------"); const string roman = "六千四百五十二"; var interpreterContext = new InterpreterContext(roman); var tree = new ArrayList { new GeExpression(), new ShiExpression(), new BaiExpression(), new QianExpression() }; foreach (Expression exp in tree) { exp.Interpreter(interpreterContext); } Console.Write(interpreterContext.Data); #endregion Console.ReadKey(); }