lundi 23 septembre 2019

Factory pattern desgin c#

I got project to do and I need to create program that creates file for specific embosser machine (machine that creates plastic bank card for example). I have 3 embosser machine and all of them creates different file. Every file contains card number, last and first name, address, etc. Difference between files is that they contains specific characters for delimiter.

I want to implement this project with factory pattern.

For simplicity let say that I have these embossers: EmbosserA, EmbosserB and EmbosserC.

This is how I created files in my project, and I need advice is this good way.

public enum eType
{ 
     A,
     B,
     C
}

public interface IEmbosser
{
     eType EmbosserType {get;}
     List<EmbosserData> GetEmbosserData();
     void CreateEmosserFile(string path);
}

public class EmbosserA : IEmbosser
{
     public eType EmbosserType => eType.A;
     public List<EmbosserData> GetEmbosserData()
     {
       //Implementation
     }

     public void CreateEmbosserFile(string path)
     {
       //Implementation
     }
}

public class EmbosserB : IEmboser { //... }

public class EmbosserC : IEmboser { //... }

And factory creator

public class EmbosserCreator
{
    public static IEmbosser GetEmbosser(eType EmbosserType)
    {

        switch (EmbosserType)
        {
            case eType.A:
                 return new EmbosserA();
            case eType.B:
                return new EmbosserB();
            case eType.C:
                return new EmbosserC();
            default:
                throw new Exception("...");
        }
    }
}

For data I thought to create abstract class EmbosserData which inherit class EmbosserAData, EmbosserBData, EmbosserCData.

For example:

public abstract class EmbosserData
{
     public string cardNumber {get; set;}
     public string address {get; set;}
     //...
}

public class EmbosserA : EmbosserData
{
     public string IDNumber {get; set;}
     //...
}

public class EmbosserB : EmbosserData
{
     public string Phone {get; set;}
     //...
}

public class EmbosserC : EmbosserData
{
     //other fields
}

My question, is this good approach for my project? If you have any advice for other implementation feel free to write.

Aucun commentaire:

Enregistrer un commentaire