区总工会网站建设流程,南京建设网站费用,怎么恶意点击对手竞价,百度做网站不给FTP密码抽象工厂是设计模块中创建型模式的一种#xff0c;它比起工厂方法模式来说#xff0c;更加具有一般性#xff0c;在本模式中会引入一个产品族的概念#xff0c;就是说,在本模式中抽象产品会有多个#xff0c;然后用抽象工厂去调用它们#xff0c;具体怎么去调用由具体工厂…抽象工厂是设计模块中创建型模式的一种它比起工厂方法模式来说更加具有一般性在本模式中会引入一个产品族的概念就是说,在本模式中抽象产品会有多个然后用抽象工厂去调用它们具体怎么去调用由具体工厂来实现。 看下列代码主要实现动态生成按钮和弹出窗口的功能弹出窗口可以根据浏览器的类型去调用适合 自己的方法按钮也可以根据传入的样式和类型去自动生成按钮。 class Program { static void Main(string[] args) { RunEnvironment runnew RunEnvironment (new RedFactory()); Console.WriteLine(run.Button.ToHtml(ButtonType.Button,name1)); } } #region WindowOpen的抽象产品和具体产品 public abstract class WindowOpen { protected WindowOpen(string title) { Title title; } protected string Title { get; set; } public abstract string ToHtml(string url); } public class IeWindowOpen : WindowOpen { public IeWindowOpen() : base(IE) { } public override string ToHtml(string url) { return string.Format(scriptWindow.open(,{0},), url); } } public class FireFoxWindowOpen : WindowOpen { public FireFoxWindowOpen() : base(火狐) { } public override string ToHtml(string url) { return string.Format(scriptWindow.open(,{0},), url); } } #endregion #region Button的抽象产品和具体产品 /// summary /// 按钮类型 /// /summary public enum ButtonType { Submit, Button, Reset, } public abstract class Button { protected Button(string className) { ClassName className; } protected string ClassName { get; set; } public abstract string ToHtml(ButtonType buttonType, string id); } public class RedButton : Button { public RedButton() : base(Redbtn) { } public override string ToHtml(ButtonType buttonType, string id) { return string.Format(input id{2} name{2} type{0} class{1} /, Enum.GetName(typeof(ButtonType), buttonType), ClassName, id); } } public class GreenButton : Button { public GreenButton() : base(GreenBtn) { } public override string ToHtml(ButtonType buttonType, string id) { return string.Format(input id{2} name{2} type{0} class{1} /, Enum.GetName(typeof(ButtonType), buttonType), ClassName, id); } } #endregion #region 抽象工厂和具体工厂 public abstract class Factory { public abstract WindowOpen CreateWindowOpen(); public abstract Button CreateButton(); } public class GreenFactory : Factory { public override Button CreateButton() { return new GreenButton(); } public override WindowOpen CreateWindowOpen() { return new IeWindowOpen(); } } public class RedFactory : Factory { public override Button CreateButton() { return new RedButton(); } public override WindowOpen CreateWindowOpen() { return new IeWindowOpen(); } } #endregion #region 应用环境 public class RunEnvironment { public WindowOpen WindowOpen { get; set; } public Button Button { get; set; } public RunEnvironment(Factory factory) { this.WindowOpen factory.CreateWindowOpen(); this.Button factory.CreateButton(); } } #endregion 看到上面代码后如果我们想为按钮加一个黄色的样式我要示从Button类派生一个子类去实现 黄色样式功能就可以了而不需要修改已有的代码这也很好的符合的“开闭原则OCP”转载于:https://www.cnblogs.com/lori/archive/2011/08/23/2150335.html