C#语言中的抽象工厂设计模式

作者:微信公众号:【架构师老卢】
9-19 18:32
114

抽象工厂设计模式是一种创建模式,它提供了一个接口,用于创建相关或依赖对象的族,而无需指定其具体类。在需要创建由通用主题相关但实现不同的对象的情况下,它特别有用。

什么是抽象工厂模式?

Abstract Factory Pattern 允许您创建可以创建其他工厂的超级工厂。这个超级工厂也被称为工厂中的工厂。使用抽象工厂模式的主要优点是它将客户端代码与创建的对象的具体类隔离开来,从而产生更加解耦和可维护的代码。

要点:

  1. 封装一组单独的工厂。
  2. 提供一种无需指定相关对象的具体类即可创建相关对象的方法。
  3. 促进工厂生产的产品之间的一致性。

真实世界的例子:

逐步实施

  1. 定义抽象产品接口。
  2. 创建具体的产品类别。
  3. 定义抽象工厂接口。
  4. 实现具体的工厂类。
  5. 在客户端代码中使用抽象工厂。

第 1 步:定义抽象的 Product Interface

首先,我们为工厂将创建的产品 (设备) 定义接口。

IDevice.cs

public interface IDevice  
{  
    void GetDetails();  
}

第 2 步:为每家公司创建具体的商品类别

接下来,我们创建具体的类,为每种类型的设备和公司实现这些产品接口。

联想设备

public class LenovoMobile : IDevice  
{  
    public void GetDetails()  
    {  
        Console.WriteLine("Lenovo Mobile");  
    }  
}  
  
public class LenovoLaptop : IDevice  
{  
    public void GetDetails()  
    {  
        Console.WriteLine("Lenovo Laptop");  
    }  
}  
  
public class LenovoDesktop : IDevice  
{  
    public void GetDetails()  
    {  
        Console.WriteLine("Lenovo Desktop");  
    }  
}

HP 设备

public class HPMobile : IDevice  
{  
    public void GetDetails()  
    {  
        Console.WriteLine("HP Mobile");  
    }  
}  
  
public class HPLaptop : IDevice  
{  
    public void GetDetails()  
    {  
        Console.WriteLine("HP Laptop");  
    }  
}  
  
public class HPDesktop : IDevice  
{  
    public void GetDetails()  
    {  
        Console.WriteLine("HP Desktop");  
    }  
}

IBM 设备

public class IBMMobile : IDevice  
{  
    public void GetDetails()  
    {  
        Console.WriteLine("IBM Mobile");  
    }  
}  
  
public class IBMLaptop : IDevice  
{  
    public void GetDetails()  
    {  
        Console.WriteLine("IBM Laptop");  
    }  
}  
  
public class IBMDesktop : IDevice  
{  
    public void GetDetails()  
    {  
        Console.WriteLine("IBM Desktop");  
    }  
}

Apple 设备

public class AppleMobile : IDevice  
{  
    public void GetDetails()  
    {  
        Console.WriteLine("Apple Mobile");  
    }  
}  
  
public class AppleLaptop : IDevice  
{  
    public void GetDetails()  
    {  
        Console.WriteLine("Apple Laptop");  
    }  
}  
  
public class AppleDesktop : IDevice  
{  
    public void GetDetails()  
    {  
        Console.WriteLine("Apple Desktop");  
    }  
}

第 3 步:定义抽象工厂接口

我们为抽象工厂定义了一个接口,该接口将创建相关产品。

IDeviceFactory.cs。

public interface IDeviceFactory  
{  
    IDevice CreateMobile();  
    IDevice CreateLaptop();  
    IDevice CreateDesktop();  
}

步骤 4:为每个公司实施 Concrete Factory Classes

我们创建实现抽象工厂接口的具体工厂,并为每个公司创建特定的产品。

LenovoFactory.cs

public class LenovoFactory : IDeviceFactory  
{  
    public IDevice CreateMobile()  
    {  
        return new LenovoMobile();  
    }  
  
    public IDevice CreateLaptop()  
    {  
        return new LenovoLaptop();  
    }  
  
    public IDevice CreateDesktop()  
    {  
        return new LenovoDesktop();  
    }  
}

HPFactory.cs

public class HPFactory : IDeviceFactory  
{  
    public IDevice CreateMobile()  
    {  
        return new HPMobile();  
    }  
  
    public IDevice CreateLaptop()  
    {  
        return new HPLaptop();  
    }  
  
    public IDevice CreateDesktop()  
    {  
        return new HPDesktop();  
    }  
}

IBMFactory.cs

public class IBMFactory : IDeviceFactory  
{  
    public IDevice CreateMobile()  
    {  
        return new IBMMobile();  
    }  
  
    public IDevice CreateLaptop()  
    {  
        return new IBMLaptop();  
    }  
  
    public IDevice CreateDesktop()  
    {  
        return new IBMDesktop();  
    }  
}

AppleFactory.cs

public class AppleFactory : IDeviceFactory  
{  
    public IDevice CreateMobile()  
    {  
        return new AppleMobile();  
    }  
  
    public IDevice CreateLaptop()  
    {  
        return new AppleLaptop();  
    }  
  
    public IDevice CreateDesktop()  
    {  
        return new AppleDesktop();  
    }  
}

第 5 步:在客户端代码中使用抽象工厂

最后,我们在客户端代码中使用抽象工厂来创建产品,而无需指定它们的具体类。

Program.cs

using System;  
  
class Program  
{  
    static void Main(string[] args)  
    {  
        // Create a Lenovo factory  
        IDeviceFactory lenovoFactory = new LenovoFactory();  
        IDevice lenovoMobile = lenovoFactory.CreateMobile();  
        IDevice lenovoLaptop = lenovoFactory.CreateLaptop();  
        IDevice lenovoDesktop = lenovoFactory.CreateDesktop();  
  
        lenovoMobile.GetDetails();  
        lenovoLaptop.GetDetails();  
        lenovoDesktop.GetDetails();  
  
        // Create an HP factory  
        IDeviceFactory hpFactory = new HPFactory();  
        IDevice hpMobile = hpFactory.CreateMobile();  
        IDevice hpLaptop = hpFactory.CreateLaptop();  
        IDevice hpDesktop = hpFactory.CreateDesktop();  
  
        hpMobile.GetDetails();  
        hpLaptop.GetDetails();  
        hpDesktop.GetDetails();  
  
        // Create an IBM factory  
        IDeviceFactory ibmFactory = new IBMFactory();  
        IDevice ibmMobile = ibmFactory.CreateMobile();  
        IDevice ibmLaptop = ibmFactory.CreateLaptop();  
        IDevice ibmDesktop = ibmFactory.CreateDesktop();  
  
        ibmMobile.GetDetails();  
        ibmLaptop.GetDetails();  
        ibmDesktop.GetDetails();  
  
        // Create an Apple factory  
        IDeviceFactory appleFactory = new AppleFactory();  
        IDevice appleMobile = appleFactory.CreateMobile();  
        IDevice appleLaptop = appleFactory.CreateLaptop();  
        IDevice appleDesktop = appleFactory.CreateDesktop();  
  
        appleMobile.GetDetails();  
        appleLaptop.GetDetails();  
        appleDesktop.GetDetails();  
    }  
}

文件夹结构

项目的文件夹结构应如下所示:

AbstractFactoryExample  
│  
├── IDevice.cs  
├── LenovoMobile.cs  
├── LenovoLaptop.cs  
├── LenovoDesktop.cs  
├── HPMobile.cs  
├── HPLaptop.cs  
├── HPDesktop.cs  
├── IBMMobile.cs  
├── IBMLaptop.cs  
├── IBMDesktop.cs  
├── AppleMobile.cs  
├── AppleLaptop.cs  
├── AppleDesktop.cs  
├── IDeviceFactory.cs  
├── LenovoFactory.cs  
├── HPFactory.cs  
├── IBMFactory.cs  
├── AppleFactory.cs  
└── Program.cs

运行程序

要运行该程序,请执行以下步骤:

  1. 为项目创建一个新目录,例如 .AbstractFactoryExample
  2. 在此目录中,使用上面提供的代码创建文件 , , 。IDevice.csLenovoMobile.csLenovoLaptop.csLenovoDesktop.csHPMobile.csHPLaptop.csHPDesktop.csIBMMobile.csIBMLaptop.csIBMDesktop.csAppleMobile.csAppleLaptop.csAppleDesktop.csIDeviceFactory.csLenovoFactory.csHPFactory.csIBMFactory.csAppleFactory.csProgram.cs
  3. 打开终端或命令提示符并导航到项目目录。
  4. 使用 .NET CLI 创建新的控制台应用程序并添加现有文件:cmd : dotnet new console
  5. 将生成的文件替换为提供的 .Program.csProgram.cs
  6. 运行应用程序:cmd: dotnet run

您应该会看到输出:

Lenovo Mobile  
Lenovo Laptop  
Lenovo Desktop  
HP Mobile  
HP Laptop  
HP Desktop  
IBM Mobile  
IBM Laptop  
IBM Desktop  
Apple Mobile  
Apple Laptop  
Apple Desktop

提示和最佳实践

  1. 一致性: 确保工厂创建的产品一致且相关。
  2. 可扩展性: Abstract Factory 可以轻松引入新的产品系列,而无需更改现有代码。
  3. 解耦: 该模式将客户端代码与具体的产品类分离,使其更易于维护和扩展。
  4. 避免过度使用: 当您有多个产品系列时,请使用 Abstract Factory Pattern。过度使用它可能会导致不必要的复杂性。

抽象工厂模式是一个强大的工具,用于创建相关对象的族,而无需指定其具体类。它促进了产品之间的一致性,并将客户端代码与具体实现分离,使您的代码库更具可维护性和可扩展性。

相关留言评论
昵称:
邮箱:
阅读排行