public AbstractFactoryCilent() { AbstractFactory nanChangFactory = new NanChangFactory(); YaBo nanChangYabo = nanChangFactory.CreateYabo(); nanChangYabo.Print(); YaJia nanChangYajia = nanChangFactory.CreateYaJia(); nanChangYajia.Print(); AbstractFactory shangHaiFactory = new ShangHaiFactory(); YaBo shangHaiYabo = shangHaiFactory.CreateYabo(); shangHaiYabo.Print(); YaJia shangHaiYajia = shangHaiFactory.CreateYaJia(); shangHaiYajia.Print(); //新增湖南 AbstractFactory huNanFactory = new HuNanFactory(); YaBo huNanYabo = huNanFactory.CreateYabo(); huNanYabo.Print(); YaJia huNanYajia = huNanFactory.CreateYaJia(); huNanYajia.Print(); }
public void CreateYaJiaTest() { // 南昌工厂实例 var factory = new NanChangFactory(); // 生产南昌口味的鸭架 var yajia = factory.CreateYaJia(); // 验证 Assert.IsNotNull(yajia); Assert.AreEqual(typeof(NanChangYaJia), yajia.GetType()); }
public void CreateYaBoTest() { // 南昌工厂实例 var factory = new NanChangFactory(); // 生产南昌口味的鸭脖 var yabo = factory.CreateYaBo(); // 验证 Assert.IsNotNull(yabo); Assert.AreEqual(typeof(NanChangYaBo), yabo.GetType()); }
static void Main(string[] args) { //抽象工厂模式:提供一个创建产品的接口来负责创建相关或依赖的对象,而不具体明确指定具体类 //抽象工厂对于系列产品的变化支持 “开放——封闭”原则(指的是要求系统对扩展开放,对修改封闭),扩展起来非常简便, //但是,抽象工厂对于添加新产品这种情况就不支持”开放——封闭 “原则,这也是抽象工厂的缺点所在, //如果需要添加新产品,此时就必须去修改抽象工厂的接口,这样就涉及到抽象工厂类的以及所有子类的改变,这样也就违背了“开发——封闭”原则。 //抽象工厂,抽象了工厂,方便了开设分厂,连锁店之类的,但是不方便添加新产品。 ///// 扮演抽象工厂的角色 ///// 创建连接数据库时所需要的对象集合, ///// 这个对象集合包括有 DbConnection对象(这个是抽象产品类,如绝味例子中的YaBo类)、DbCommand类、DbDataAdapter类,针对不同的具体工厂都需要实现该抽象类中方法, //public abstract class DbProviderFactory //{ //// 提供了创建具体产品的接口方法 // protected DbProviderFactory(); // public virtual DbCommand CreateCommand(); // public virtual DbCommandBuilder CreateCommandBuilder(); // public virtual DbConnection CreateConnection(); // public virtual DbConnectionStringBuilder CreateConnectionStringBuilder(); // public virtual DbDataAdapter CreateDataAdapter(); // public virtual DbDataSourceEnumerator CreateDataSourceEnumerator(); // public virtual DbParameter CreateParameter(); // public virtual CodeAccessPermission CreatePermission(PermissionState state); //} //下面以绝味鸭脖连锁店为例子演示下抽象工厂模式 //因为每个地方的喜欢的口味不一样,有些地方喜欢辣点的,有些地方喜欢吃不辣点 //客户端调用 // 南昌工厂制作南昌的鸭脖和鸭架 JueWeiAbstractFactory nanChangFactory = new NanChangFactory(); DuckNeck nanChangDuckNeck = nanChangFactory.CreateDuckNeck(); nanChangDuckNeck.Print(); DuckSwings nanChangDuckSwings = nanChangFactory.CreateDuckSwings(); nanChangDuckSwings.Print(); // 上海工厂制作上海的鸭脖和鸭架 JueWeiAbstractFactory shangHaiFactory = new ShangHaiFactory(); shangHaiFactory.CreateDuckNeck().Print(); shangHaiFactory.CreateDuckSwings().Print(); Console.Read(); }
public void AbstartFactoryTest() { // 南昌工厂制作南昌的鸭脖和鸭架 AbstractFactory nanChangFactory = new NanChangFactory(); Assert.Fail("南昌的鸭脖", nanChangFactory.CreateYaBo().Print()); Assert.Fail("南昌的鸭架", nanChangFactory.CreateYaJia().Print()); // 上海工厂制作上海的鸭脖和鸭架 AbstractFactory shangHaiFactory = new ShangHaiFactory(); Assert.Fail("上海的鸭脖", shangHaiFactory.CreateYaBo().Print()); Assert.Fail("上海的鸭架", shangHaiFactory.CreateYaJia().Print()); }
public void AbstractFactoryTest() { { AbstractFactory nanChangFactory = new NanChangFactory(); YaBo nanChangYabo = nanChangFactory.CreateYaBo(); nanChangYabo.Print(); YaJia nanChangYajia = nanChangFactory.CreateYaJia(); nanChangYajia.Print(); // 上海工厂制作上海的鸭脖和鸭架 AbstractFactory shangHaiFactory = new ShangHaiFactory(); shangHaiFactory.CreateYaBo().Print(); shangHaiFactory.CreateYaJia().Print(); } }
public void Test() { AbstractFactory nc = new NanChangFactory(); YaBo ncyb = nc.CreateYaBo(); ncyb.Print(); YaJia ncyj = nc.CreateYaJia(); ncyj.Print(); AbstractFactory sh = new ShangHaiFactory(); sh.CreateYaBo(); sh.CreateYaJia(); Console.ReadLine(); }
public void NanChangFactoryTest01() { // 初始化工厂实例 var factory = new NanChangFactory(); // 制作鸭脖 var yabo = factory.CreateYaBo(); yabo.PutInto(); // 制作鸭架 var yajia = factory.CreateYaJia(); yajia.PutInto(); Assert.AreEqual("南昌口味的鸭脖", yabo.Dish); Assert.AreEqual("南昌口味的鸭架", yajia.Dish); }
static void Main(string[] args) { // 南昌工厂制作南昌的鸭脖和鸭架 AbstractFactory nanChangFactory = new NanChangFactory(); YaBo nanChangYabo = nanChangFactory.CreateYaBo(); nanChangYabo.Print(); YaJia nanChangYajia = nanChangFactory.CreateYaJia(); nanChangYajia.Print(); // 上海工厂制作上海的鸭脖和鸭架 AbstractFactory shangHaiFactory = new ShangHaiFactory(); shangHaiFactory.CreateYaBo().Print(); shangHaiFactory.CreateYaJia().Print(); Console.Read(); }
private void button4_Click(object sender, EventArgs e) { // 南昌工厂制作南昌的鸭脖和鸭架 AbstractFactory nanChangFactory = new NanChangFactory(); YaBo nanChangYabo = nanChangFactory.CreateYaBo(); nanChangYabo.Print(); YaJia nanChangYajia = nanChangFactory.CreateYaJia(); nanChangYajia.Print(); // 上海工厂制作上海的鸭脖和鸭架 AbstractFactory shangHaiFactory = new ShangHaiFactory(); shangHaiFactory.CreateYaBo().Print(); shangHaiFactory.CreateYaJia().Print(); Console.Read(); }
static void Main(string[] args) { Console.WriteLine("南昌工厂创建及产品生产。"); AbstractFactory nanChangFactory = new NanChangFactory(); YaBo nanChangYabo = nanChangFactory.CreateYaBo(); nanChangYabo.Print(); YaJia nanChangYajia = nanChangFactory.CreateYaJia(); nanChangYajia.Print(); Console.WriteLine("————————————"); Console.WriteLine("山海工厂创建及产品生产。"); AbstractFactory shangHaiFactory = new ShangHaiFactory(); shangHaiFactory.CreateYaBo().Print(); shangHaiFactory.CreateYaJia().Print(); Console.Read(); }
static void Main(string[] args) { AbstractFactory shangHaiFactory = new ShangHaiFactory(); Console.WriteLine("上海工厂做的产品:"); shangHaiFactory.CreateYaBo().Print(); shangHaiFactory.CreateYaJia().Print(); AbstractFactory nanChangFactory = new NanChangFactory(); Console.WriteLine("南昌工厂做的产品:"); nanChangFactory.CreateYaBo().Print(); nanChangFactory.CreateYaJia().Print(); Console.WriteLine("新开设了河南工厂,制作:"); AbstractFactory heNanFactory = new HeNanFactory(); heNanFactory.CreateYaBo().Print(); heNanFactory.CreateYaJia().Print(); Console.ReadKey(); }
public static void Main(string[] args) { //单例模式 Console.WriteLine("---------Singleton----------"); var s1 = Singleton.GetSingleton(); var s2 = Singleton.GetSingleton(); Console.WriteLine(s1.GetHashCode()); Console.WriteLine(s2.GetHashCode()); //简单工厂模式 Console.WriteLine("---------SimpleFactory----------"); var f1 = SimpleFoodFactory.CreateFood("TomaoScrambledEggs"); f1.Print(); var f2 = SimpleFoodFactory.CreateFood("ShreddedPorkWithPotatoes"); f2.Print(); //工厂模式 Console.WriteLine("---------Factory----------"); Creator shreddedPorkWithPotatoesFactory = new ShreddedPorkWithPotatoesFactory(); Creator tomatoScrambledEggsFactory = new TomatoScrambledEggsFactory(); Creator minceMeatEggplantFactor = new MincedMeatEggplantFactory(); var tomatoScrambleEggs = tomatoScrambledEggsFactory.CreateFoodFactory(); tomatoScrambleEggs.Print(); var shreddedPorkWithPotatoes = shreddedPorkWithPotatoesFactory.CreateFoodFactory(); shreddedPorkWithPotatoes.Print(); var minceMeatEggplant = minceMeatEggplantFactor.CreateFoodFactory(); minceMeatEggplant.Print(); //抽象工厂模式 Console.WriteLine("---------AbstractFactory----------"); AbstractFactory nanChangFactory = new NanChangFactory(); nanChangFactory.CreateYaBo().Print(); nanChangFactory.CreateYaJia().Print(); AbstractFactory shangHaiFactory = new ShangHaiFactory(); shangHaiFactory.CreateYaBo().Print(); shangHaiFactory.CreateYaJia().Print(); AbstractFactory huNanFactory = new HuNanFactory(); huNanFactory.CreateYaBo().Print(); huNanFactory.CreateYaJia().Print(); //建造者模式 Console.WriteLine("---------Builder_Patterns----------"); var director = new Director(); Builder b1 = new BuildFirst(); Builder b2 = new BuildSecond(); director.Construct(b1); var c1 = b1.GetComputer(); c1.Show(); director.Construct(b2); var c2 = b2.GetComputer(); c2.Show(); //原型模式 Console.WriteLine("---------Prototype_Patterns----------"); MonkeyKingPrototype prototype = new ConcretePrototype("pipixiong"); MonkeyKingPrototype cloneMonkeyKingPrototype = prototype.Clone() as ConcretePrototype; Console.WriteLine("1:" + cloneMonkeyKingPrototype?.Id); MonkeyKingPrototype clonePrototype = prototype.Clone() as ConcretePrototype; Console.WriteLine("2:" + cloneMonkeyKingPrototype?.Id); //适配器模式 Console.WriteLine("---------Adapter_Patterns----------"); ThreeHole threeHole = new PowerAdapter(); threeHole.Request(); //桥接模式 Console.WriteLine("---------Bridge_Patterns----------"); RemoteControl remoteControl = new ConcreteRemote(); remoteControl.Implementor = new XiaoMi(); remoteControl.On(); remoteControl.Off(); remoteControl.SetChannel(); remoteControl.Implementor = new Samsung(); remoteControl.On(); remoteControl.Off(); remoteControl.SetChannel(); //装饰者模式 Console.WriteLine("---------Decorator_Patterns----------"); Phone phone = new ApplePhone(); Decorator stickerDecorator = new Sticker(phone); stickerDecorator.Print(); Console.WriteLine("---------"); Decorator accessoriesDecorator = new Accessories(phone); accessoriesDecorator.Print(); Console.WriteLine("---------"); var sticker = new Sticker(phone); var accessories = new Accessories(sticker); accessories.Print(); //组合模式 Console.WriteLine("---------Composite_Patterns----------"); var complexGraphics = new ComplexGraphics("一个复杂图形和两条线段组成的复杂图形"); complexGraphics.Add(new Line("线A")); var complexGraphicsCG = new ComplexGraphics("一个圆和一条线组成的复杂图形"); complexGraphicsCG.Add(new Circle("圆")); complexGraphicsCG.Add(new Line("线B")); complexGraphics.Add(complexGraphicsCG); var line = new Line("线C"); complexGraphics.Add(line); Console.WriteLine("复杂图形的绘制如下:"); Console.WriteLine("---------------------"); complexGraphics.Draw(); Console.WriteLine("复杂图形绘制完成"); Console.WriteLine("---------------------"); complexGraphics.Remove(line); Console.WriteLine("移除线段C后,复杂图形的绘制如下:"); Console.WriteLine("---------------------"); complexGraphics.Draw(); Console.WriteLine("复杂图形绘制完成"); Console.WriteLine("---------------------"); //外观模式 Console.WriteLine("---------Facade_Patterns----------"); var facade = new RegistrationFacade(); if (facade.RegisterCourse("设计模式", "pomelo")) { Console.WriteLine("选课成功"); } else { Console.WriteLine("选课失败"); } //享元模式 Console.WriteLine("---------Flyweight_Patterns----------"); var externalstate = 10; var factory = new FlyweightFactory(); var flyweightA = factory.GetfFlyweight("A"); flyweightA?.Operation(externalstate); var flyweightB = factory.GetfFlyweight("B"); flyweightB?.Operation(externalstate); var flyweightC = factory.GetfFlyweight("C"); flyweightC?.Operation(externalstate); var flyweightD = factory.GetfFlyweight("D"); if (flyweightD != null) { flyweightD.Operation(externalstate); } else { Console.WriteLine("驻留池中不存在字符串D"); // 这时候就需要创建一个对象并放入驻留池中 var d = new ConcreteFlyweight("D"); factory.Flyweights.Add("D", d); } //代理模式 Console.WriteLine("---------Proxy_Patterns----------"); Person proxy = new Friend(); proxy.BuyProduct(); //模板方法 Console.WriteLine("---------Template_Patterns----------"); var spinach = new Spinach(); spinach.cookVegetable(); //命令模式 Console.WriteLine("---------Command_Patterns----------"); var receiver = new Receiver(); Command command = new ConcreteCommand(receiver); var invoke = new Invoke(command); invoke.ExecuteCommand(); //迭代器模式 Console.WriteLine("---------Iterator_Patterns----------"); IListCollection list = new ConcreteList(); var terator = list.GetITerator(); while (terator.MoveNext()) { var i = (int)terator.GetCurrent(); Console.WriteLine(i.ToString()); terator.Next(); } //观察者模式 Console.WriteLine("---------Observer_Patterns----------"); TenXun tenXun = new TenXunGame("LOL", "游戏需要更新"); tenXun.AddObserver(new Subscriber("pomelo")); tenXun.AddObserver(new Subscriber("yuuko")); tenXun.Update(); //中介者模式 Console.WriteLine("---------Mediator_Patterns----------"); AbstractCardPartner a = new ParterA(); AbstractCardPartner b = new ParterB(); a.MoneyCount = 20; b.MoneyCount = 20; AbstractMediator mediator = new MediatorPater(a, b); a.ChangCount(5, mediator); Console.WriteLine("A现在的钱是:{0}", a.MoneyCount); Console.WriteLine("B现在的钱是:{0}", b.MoneyCount); b.ChangCount(10, mediator); Console.WriteLine("A现在的钱是:{0}", a.MoneyCount); Console.WriteLine("B现在的钱是:{0}", b.MoneyCount); //状态者模式 Console.WriteLine("---------state_Patterns----------"); var account = new Account("pomelo"); account.Deposit(1000); account.Deposit(200); account.Deposit(600); account.PayInterest(); account.Withdraw(2000); account.Withdraw(500); //策略者模式 Console.WriteLine("---------stragety_Patterns----------"); var operation = new InterestOperation(new PersonalTaxStrategy()); Console.WriteLine("个人支付的税为:{0}", operation.GetTax(5000.00)); operation = new InterestOperation(new EnterpriseTaxStrategy()); Console.WriteLine("企业支付的税为:{0}", operation.GetTax(50000.00)); //责任链模式 Console.WriteLine("---------Responsibility_Patterns----------"); var purchaseRequest = new PurchaseRequest(4000, "phone"); var softPurchaseRequest = new PurchaseRequest(10000, "vs"); var computeRequest = new PurchaseRequest(40000, "computer"); Approver manager = new Manager("pomelo"); Approver vp = new VicePresident("yuuko"); Approver pre = new President("jesse"); manager.NextApprover = vp; vp.NextApprover = pre; manager.ProcessRequest(purchaseRequest); manager.ProcessRequest(softPurchaseRequest); manager.ProcessRequest(computeRequest); //访问者模式 Console.WriteLine("---------Vistor_Patterns----------"); var objectStructure = new ObjectStructure(); foreach (Element e in objectStructure.Elements) { e.Accept(new ConcreteVistor()); } //备忘录模式 Console.WriteLine("---------Memento_Patterns----------"); var persons = new List <ContactPerson> { new ContactPerson { Name = "Pomelo", MobileNum = "123445" }, new ContactPerson { Name = "Yuuko", MobileNum = "234565" }, new ContactPerson { Name = "Jeese ", MobileNum = "231455" } }; var mobileOwner = new MobileOwner(persons); mobileOwner.Show(); var caretaker = new Caretaker(); caretaker.ContactMementoDic.Add(DateTime.Now.ToString(CultureInfo.InvariantCulture), mobileOwner.CreateContactMemento()); Console.WriteLine("----移除最后一个联系人--------"); mobileOwner.ContactPersons.RemoveAt(2); mobileOwner.Show(); Thread.Sleep(1000); caretaker.ContactMementoDic.Add(DateTime.Now.ToString(CultureInfo.InvariantCulture), mobileOwner.CreateContactMemento()); Console.WriteLine("-------恢复联系人列表,请从以下列表选择恢复的日期------"); var keyCollection = caretaker.ContactMementoDic.Keys; foreach (var k in keyCollection) { Console.WriteLine("Key = {0}", k); } while (true) { Console.Write("请输入数字,按窗口的关闭键退出:"); var index = -1; try { index = int.Parse(Console.ReadLine()); } catch { Console.WriteLine("输入的格式错误"); continue; } if (index < keyCollection.Count && caretaker.ContactMementoDic.TryGetValue(keyCollection.ElementAt(index), out var contactMentor)) { mobileOwner.RestoreMemento(contactMentor); mobileOwner.Show(); } else { Console.WriteLine("输入的索引大于集合长度!"); } } }