使用模式工厂获取类型化的对象

时间:2018-09-24 14:34:41

标签: c# interface factory

查看我的代码:

public interface IStructureType
{
    int Longueur { get; set; }
    int Position { get; set; }
    int CompleterCodeBy { get; set; }
}


public abstract class StructureTypeFactory
{
    public abstract IStructureType GetStructureType(string type);
}

public class ConcreteStructureTypeFactory : StructureTypeFactory
{
    public override IStructureType GetStructureType(string type)
    {
       switch(type)
        {
            case "StructureCodeMagasin":
                return new StructureCodeMagasin();

            case "StructureChrono":
                return new StructureChrono();

            case "StructureLotSimple":
                return new StructureLotSimple();

            default:
                throw new ApplicationException("");
        }
    }
}

 public class StructureCodeMagasin : IStructureType
{
    public int Longueur { get ; set; }
    public int Position { get; set; }
    public int CompleterCodeBy { get { return 2; }  set { CompleterCodeBy = value; }  }

    public void GetCodeMagasin()
    {
        //some code
    }
}

我尝试使用Factory模式,但是在此示例中如何访问GetCodeMagasin方法:

public MainWindow()
    {
        InitializeComponent();

        StructureTypeFactory st = new ConcreteStructureTypeFactory();

        var structure = st.GetStructureType("StructureCodeMagasin");

        int longueur = structure.CompleterCodeBy;

    }

我可以访问属性,但不能访问任何方法,我希望结构变量将键入StructureCodeMagasin。

感谢帮助

0 个答案:

没有答案