抽象工厂设计模式- c#

本文关键字:设计模式 工厂 抽象 | 更新日期: 2023-09-27 18:15:15

我遵循了工厂设计模式:

http://dotnet.dzone.com/articles/design-patterns-c-factory

我很明白这个例子。

也可以做同样的例子吗?

抽象工厂设计模式 ?

我想了解抽象工厂设计模式的基本概念。

谢谢

抽象工厂设计模式- c#

常见的构造是创建数据访问组件的抽象工厂,您可以为数据库供应商或另一个存储库系统实现特定的每个工厂。

using System;
namespace DZoneArticles.FactoryDesignPattern
{
    public abstract class EmployeeFactory
    {
         public abstract Employee Create();
    }
    public class DBAFactory : EmployeeFactory
    {
         public override Employee Create() { return new DBA(); }
    }
    public class ManagerFactory : EmployeeFactory
    {
         public override Employee Create() { return new Manager(); }
    }
}

抽象工厂——不是同一个例子,但是很容易理解。

改编自"Applied Java Patterns":

 public interface IEmployee
 {
 }
 public class Developer : IEmployee
 {
 }
 public class Tester : IEmployee
 {
 }
 public interface IManager
 {
 }
 public class HRManager : IManager
 {
 }
 public class ProjectManager : IManager
 {
 }
 public interface IFactory()
 {
      IEmployee CreateEmployee();
      IManager CreateManager();
 }
 public class ConcreteFactory1 : IFactory
 {
      public IEmployee CreateEmployee()
      {
           return new Developer();
      }
      public IManager CreateManager()
      {
           return new HRManager();
      }
 }
 public class ConcreteFactory2 : IFactory
 {
      public IEmployee CreateEmployee()
      {
           return new Tester();
      }
      public IManager CreateManager()
      {
           return new ProjectManager();
      }
 }

抽象工厂设计模式在c#中的简单示例:

using System;
using System.IO;
enum TYPE_OPERATION
{
    Design,
    Work
}
//Abstract Factory
abstract class AbstractFactory
{
    public static AbstractFactory PrepareOperation(TYPE_OPERATION type_operation)
    {
        switch (type_operation)
        {
            case TYPE_OPERATION.Design:
                return new DesignTeam();
            case TYPE_OPERATION.Work:
                return new WorkTeam();
            default:
                throw new NotImplementedException();
        }
    }
    public abstract void PerformOperation();
}
class DesignTeam : AbstractFactory
{
    public override void PerformOperation()
    {
        Designers m = new Designers();
        m.Designing();
    }
}
class WorkTeam : AbstractFactory
{
    public override void PerformOperation()
    {
        Workers k = new Workers();
        k.Working();
    }
}
// The concrete class
class Designers
{
    public void Designing()
    {
        Console.WriteLine("The Design Team has completed its work!");
    }
}
// The concrete class
class Workers 
{
    public  void Working()
    {
        Console.WriteLine("The Work Team has finished its work!");
    }
}
public class Client
{   
    public static void Main(String[] args)
    {
        AbstractFactory factory = AbstractFactory.PrepareOperation(TYPE_OPERATION.Design);
        factory.PerformOperation();
        factory = AbstractFactory.PrepareOperation(TYPE_OPERATION.Work);
        factory.PerformOperation();
        Console.ReadLine();
    }
 }
/* output:
 The Design Team has completed its work!
 The Work Team has finished its work!
*/