2011-07-28 48 views
3

我創建了一個工廠類,以簡化不污染目標類的構造函數創建另一個類的對象(我認爲這就是工廠是)創建在C#一個簡單的工廠類

這是我在這一刻。

public class QFactory 
{ 
    public Quiz.Question q(string text, string buttonname) 
    { 
     Quiz.Question question = new Quiz.Question(); 
     question.QuestionText = text; 
     question.QuestionImage = buttonname; 

     return question; 
    } 

    public Quiz.Answer a(string answerText, bool answerRight = false) 
    { 
     Quiz.Answer answer = new Quiz.Answer(); 
     answer.text = answerText; 
     answer.correct = answerRight; 
     return answer; 
    } 
} 

爲了使用它來創建Quiz.Question和Quiz.Answer對象,我必須按以下方式使用它。

Quiz.Question q = (new QFactory()).q("What is a tomato?","But_01_Idle"); 

Quiz.Answer a = (new QFactory()).a("fruit",true); 
Quiz.Answer b = (new QFactory()).a("vegetable"); 
Quiz.Answer c = (new QFactory()).a("animal"); 

q.Answers = new List<Quiz.Answer>{a,b,c}; // add a,b,c answer to the question 

我該如何更改QFactory類,以便它的使用是這樣(singletons?)?

Quiz.Question q = QFactory.q("what is a Tomato?","But_01_Idle"); 
... 

回答

9

使該方法在工廠靜態

+0

謝謝,我試圖讓這個類只是靜態的。 –

+0

靜態類是一個只有靜態方法和字段的類,所以你可以(也應該)在靜態方法中標記靜態類。 –

+0

謝謝,我希望我正確使用工廠。 –

0

馬克方法Q()和()爲static,類QFactory應該是靜態的,以及因爲所有的方法都是static

2

充分利用aq方法static。它們沒有引用QFactory類中的任何實例變量,因此它們可以是static

由於整個QFactory類缺少成員,因此您可以使整個類static,阻止它的任何實例化。靜態類更多地成爲一組方法,而不是通常意義上的類,這正是這種情況。將該類標記爲靜態會更清楚地表明這不是一個可以攜帶狀態的對象。

2

這裏是C#中工廠設計模式實現的簡單示例。這可能對你有幫助

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace Factory 
{ 
    public abstract class DbServer 
    { 
     //method need to be implemented by inherited classes 
     public abstract string GetDbServerName(); 
    } 
    public class MsSqlServerDb : DbServer 
    { 
     //overridden method 
     public override string GetDbServerName() 
     { 
      return "MS Sql Server"; 
     } 
    } 
    public class OracleDb : DbServer 
    { 
     //overridden method 
     public override string GetDbServerName() 
     { 
      return "Oracle Database Server"; 
     } 
    } 
    //factory class that will be used to create objects 
    public static class DatabaseFactory 
    { 
     //return the required object 
     public static DbServer GetDb(string DbName) 
     { 
      if (DbName == "Ms Sql Server") 
      { 
       return new MsSqlServerDb(); 
      } 
      if (DbName == "Oracle Database Server") 
      { 
       return new OracleDb(); 
      } 
      return new MsSqlServerDb(); 

     } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      //get the ms sql server object 
      DbServer dbServer = DatabaseFactory.GetDb("Ms Sql Server"); 
      //return ms sql server 
      string dbName = dbServer.GetDbServerName(); 
      //print the name on output window 
      Console.WriteLine("Server Name : " + dbName); 
      //get the oracle database server object 
      dbServer = DatabaseFactory.GetDb("Oracle Database Server"); 
      //return oracle server name 
      dbName = dbServer.GetDbServerName(); 
      //print the name to output window 
      Console.WriteLine("Server Name : " + dbName); 
      Console.Read(); 
     } 
    } 
}