同简单工厂模式和工厂模式相比,工厂模式只提供一个产品等级结构而抽象工厂模式提供的的产品等级结构≥2。搜索关于抽象工厂模式的解释,基本上都是指对具有相同主题的(单个/独)工厂进行封装。何为相同主题,这里就涉及到对产品簇与产品等级结构的理解,具体请参看如下图所示:
一、示例展示:
通过学习及总结,以下是我做的一个抽象工厂模式的示例:
1. 定义抽象工厂角色
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
//Abstract Factory public abstract class AbstractFactory { public abstract Handphone CreateHandphone(); public abstract Laptop CreateLaptop(); }
2. 定义具体工厂角色
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
//Concrete Factory public class LenovoFactory : AbstractFactory { public override Handphone CreateHandphone() { return new LenovoHandphone(); } public override Laptop CreateLaptop() { return new LenovoLaptop(); } } //Concrete Factory public class AppleFactory : AbstractFactory { public override Handphone CreateHandphone() { return new AppleHandphone(); } public override Laptop CreateLaptop() { return new AppleLaptop(); } }
3. 定义抽象产品角色:Handphone
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
//Abstract Product role(抽象产品) public abstract class Handphone { abstract public void HandPhoneRun(); }
4. 定义抽象产品角色:Laptop
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public abstract class Laptop { abstract public void LaptopRun(); }
5. 定义具体产品角色:Lenovo品牌的Handphone和Laptop
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
//Concrete product(具体产品角色) public class LenovoHandphone : Handphone { public override void HandPhoneRun() { Console.WriteLine("Lenovo handphone is running,you can dail to someone now!"); } } //Concrete product(具体产品角色) public class LenovoLaptop : Laptop { public override void LaptopRun() { Console.WriteLine("Lenovo laptop is running,you can start to work now!"); } }
6. 定义具体产品角色:Apple品牌的Handphone和Laptop
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
//Concrete product(具体产品角色) class AppleHandphone : Handphone { public override void HandPhoneRun() { Console.WriteLine("Apple handphone is running,you can dail to someone now!"); } } //Concrete product(具体产品角色) class AppleLaptop : Laptop { public override void LaptopRun() { Console.WriteLine("Apple laptop is running,you can start to work now!"); } }
7. 创建客户端调用
方式1:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
static void Main(string[] args) { AbstractFactory lenovoFactory = new LenovoFactory(); AbstractFactory appleFactory = new AppleFactory(); Handphone lenovoHandphone = lenovoFactory.CreateHandphone(); Laptop lenovoLaptop = lenovoFactory.CreateLaptop(); Handphone appleHandphone = appleFactory.CreateHandphone(); Laptop appleLaptop = appleFactory.CreateLaptop(); lenovoHandphone.HandPhoneRun(); lenovoLaptop.LaptopRun(); appleHandphone.HandPhoneRun(); appleLaptop.LaptopRun(); Console.ReadLine(); }
方式2:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
class ElectronicProduct { private Handphone hp; private Laptop lt; public ElectronicProduct(AbstractFactory factory) { lt = factory.CreateLaptop(); hp = factory.CreateHandphone(); } public void LaptopRun() { lt.LaptopRun(); } public void HandphoneRun() { hp.HandPhoneRun(); } } class Program { static void Main(string[] args) { AbstractFactory factory1 = new LenovoFactory(); ElectronicProduct ep1 = new ElectronicProduct(factory1); ep1.HandphoneRun(); ep1.LaptopRun(); AbstractFactory factory2 = new AppleFactory(); ElectronicProduct ep2 = new ElectronicProduct(factory2); ep2.HandphoneRun(); ep2.LaptopRun(); Console.ReadLine(); } }
二、抽象工厂模式设计理念:
抽象工厂模式涉及到4个角色:抽象工厂,具体工厂,抽象产品,具体产品。在抽象工厂的声明过程中,已经定义了两个可以生产的产品的抽象方法。在透过对两种产品的分别抽象,同时根据产品簇及产品等级结构的定义,在具体的实现类中,分别对每种产品进行了详细实现。
三、角色及关系: