2011-11-21 107 views

回答

1

Abstract factory - 不一样的例子,但很容易理解。

+0

我需要使用我提供的相同示例的抽象工厂设计模式,以便我能更好地理解它。您可以修改代码/扩展代码。 – user1054625

+0

我知道界面保持不变。而这些类也是。我们可以让Programmer和SeniorProgrammer属于DEVELOPER和NetworkDBA类,而SQLServerDBA属于DBA类。现在我认为我们可以在此基础上创建工厂类。 – user1054625

1

通用构造是创建数据访问组件的抽象工厂,并且您为数据库供应商或其他存储库系统实现了每个特定工厂。从 “应用的Java模式”

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(); } 
    } 
} 
1

适应:

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(); 
     } 
} 
1

抽象工厂设计模式在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! 
*/