抽象工厂是设计模块中创建型模式的一种,它比起工厂方法模式来说,更加具有一般性,在本模式中会引入一个产品族的概念,就是说,在本模式中抽象产品会有多个,然后用抽象工厂去调用它们,具体怎么去调用由具体工厂来实现。
看下列代码,主要实现动态生成按钮和弹出窗口的功能,弹出窗口可以根据浏览器的类型去调用适合
自己的方法,按钮也可以根据传入的样式和类型去自动生成按钮。
 class Program     { static void Main(string[] args)
        { RunEnvironment run=new 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("<script>Window.open('','{0}','')", url);
}
}
public class FireFoxWindowOpen : WindowOpen
    {         public FireFoxWindowOpen() : base("火狐")
        { }
public override string ToHtml(string url)
        { return string.Format("<script>Window.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)”