Joseph Le Brech
Joseph Le Brech

Reputation: 6653

Creating a simple factory class in C#

I'm creating a factory class to simplify creating objects of another class without polluting the constructors of the target class (I think that's what factories are for)

This is what I have at the moment.

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;
    }
}

In order to use it to create Quiz.Question and Quiz.Answer objects, I have to use it in the following way.

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

How would I change the QFactory class so that its usage is like this instead (singletons?)?

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

Upvotes: 4

Views: 16509

Answers (4)

aamir sajjad
aamir sajjad

Reputation: 3039

here is simple example of factory design pattern implementation in C#. this may be help full to you

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

Upvotes: 4

Anders Abel
Anders Abel

Reputation: 69260

Make the a and q methods static. They are not referencing any instance variables in the QFactory class so they can be static.

Since the whole QFactory class lacks members you can make the entire class static, preventing any instantiation of it. A static class more becomes a grouping of methods than a class in the usual meaning, which is exactly the case here. Marking the class as static makes it clearer that this is not an object which can carry state.

Upvotes: 2

sll
sll

Reputation: 62494

Mark methods q() and a() as static, class QFactory should be static as well because all methods are static

Upvotes: 0

Anders Forsgren
Anders Forsgren

Reputation: 11101

Make the method static in the factory

Upvotes: 11

Related Questions