Factory Pattern

Relying on the abstract.

Problem

Cicle c = new Cicle(origin, r);
c.Draw();
public void Main(){
	Function(new Circle());
	Function(new Square());
}

public void Function(IShape s){
	s.Draw();
}

IShape

IShape : Factory

IShape : Factory

Exapmle : Various Database

Application (Employee)

Exapmle : Various Database

Exapmle : Test

Exapmle : Test

Exapmle : Test

public interface Database
{
    void Store(string key, string data);
    string Retrieve(string key);
}

public class DatabaseImpl : Database
{
    public void Store(string key, string data)
    {
        // Do Real DB connection
    }

    public string Retrieve(string key)
    {
        // Do Action
        return "someData";
    }
}

public class Payroll
{
    public Database DB { get; set; }
    public void DoSometing()
    {
        DB.Store("key1", "some data");
        var data = DB.Retrieve("key1");
    }
}
[TestFixture]
public class PayrollTest : Database
{
    private string testVariable = String.Empty;
    private Payroll payroll;

    public void Store(string key, string data)
    {
        // Do Test Action
        testVariable = data;
    }

    public string Retrieve(string key)
    {
        // Do Test Action
        return testVariable;
    }

    [Test]
    public void TestReadWrite()
    {
        payroll = new Payroll();
        payroll.DB = this;
        payroll.DoSometing();
        Assert.AreEqual(testVariable, "some data");
    }
}

Exapmle : Test

Exapmle : X-band Data Checker

Exapmle : X-band Data Checker

// Controller

dataProcessingChain = DataProcessingChainFactory.CreateAndReturn(parameters.OperationMode, parameters.DataSource);
dataProcessingChain.SetParameters(parameters, report);
dataProcessingChain.Start();
// Factory
public static class DataProcessingChainFactory
{
    public static IDataProcessingChain CreateAndReturn(OperationMode operationMode, DataSource dataSource)
    {
        IDataProcessingChain selectedChain = null;

        if (dataSource == DataSource.SSR)
        {
            switch (operationMode)
            {
                case OperationMode.INTIGRETY_CHECK_TF:
                    selectedChain = new ChainTfIntegrityCheck();
                    break;
                case OperationMode.DUMP_TRANSFERFRAME:
                    selectedChain = new ChainTfDecryption();
                    break;
                case OperationMode.DUMP_SOURCEPACKET:
                    selectedChain = new ChainSpDump();
                    break;
                case OperationMode.INTIGRETY_CHECK_SP:
                    selectedChain = new ChainSpIntegrityCheck();
                    break;
                case OperationMode.RAW_TO_IMAGE:
                case OperationMode.SP_TO_IMAGE:
                    selectedChain = new ChainSpToImage();
                    break;
                case OperationMode.PATTERN_CHECK:
                    selectedChain = new ChainPatternCheck();
                    break;
                case OperationMode.DUMP_LINEDATA:
                    selectedChain = new ChainLineDataDump();
                    break;
            }
        }
        else if (dataSource == DataSource.FPA)
        {
            switch (operationMode)
            {
                case OperationMode.RAW_TO_IMAGE:
                    selectedChain = new ChainFpaImageDump();
                    break;
                case OperationMode.PATTERN_CHECK:
                    selectedChain = new ChainFpaPatternCheck();
                    break;
                case OperationMode.REAL_TIME_VIEW:
                    selectedChain = new ChainFpaRealTimeView();
                    break;
                case OperationMode.RAW_FILE_VIEW:
                    selectedChain = new ChainFpaDumpView();
                    break;
            }
        }

        return selectedChain;
    }
}

Adventage

  • Relying on the abstract.

  • replaceable object

Cost

  • Complicated

End.

Factory Pattern

By Jeong Keun Park

Factory Pattern

  • 125