Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a simple factory class in C#

Tags:

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");
...
like image 411
Joseph Le Brech Avatar asked Dec 03 '22 07:12

Joseph Le Brech


2 Answers

Make the method static in the factory

like image 179
Anders Forsgren Avatar answered Dec 19 '22 22:12

Anders Forsgren


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();
        }
    }
}
like image 22
aamir sajjad Avatar answered Dec 20 '22 00:12

aamir sajjad