创建工厂模式

时间:2015-03-20 13:42:27

标签: c# oop

我试图使用工厂模式并创建了三个接口。

ITest1

public interface ITest1: ITestX
{
    void Write(string name);
}

ITest2

public interface ITest2: ITestX
{
    void Read(string name);
}

ITestX

public interface ITestX
{

}

接口ITest1和ITest1在A和B类中实现:

public class A: ITest1 
{
    public void Write(string name)
    {
        Console.WriteLine(name);
    }
}

public class B: ITest2
{
    public void Read(string name)
    {
        Console.WriteLine(name);
    }
}

对于工厂,我的界面如下所示:

public interface IFactory
{
    ITestX GetClass(ClassType cType);
    ITestX GetClass(int cType);
}

班级 ClassType

public class ClassT
{
    public int Type { get; set; }

    public static class ClassType
    {
        public const int AType = 0;
        public const int BType = 1;
    }
}

实现工厂接口的Factory类:

public class Factory : IFactory
    {
        public ITestX GetClass(ClassType cType)
        {
            return GetNetwork(networkType.Type);
        }

        public ITestX GetClass(int cType)
        {
            switch (cType)
            {
                case ClassT.ClassType.AType:
                    return new A();
                case ClassT.ClassType.BType:
                    return new B();
                default:
                    throw new NotImplementedException();
            }
        }
}

在我的测试方法中,我尝试使用工厂:

Factory factory = new Factory();
var typeClass = factory.GetClass(new ClassT{ Type = ClassT.ClassType.AType }); 

但是,我无法使用ReadWrite方法,因为它们在typeClass中不可见。发生了什么事?为什么我无法访问它们?

3 个答案:

答案 0 :(得分:2)

因为Factory.GetClass返回ITextX,并且此接口没有声明该方法。

您可以尝试将该对象强制转换为所需的接口,然后您将有机会调用相应的方法。

<强>更新 您可以在wiki page上看到示例实现。

答案 1 :(得分:1)

示例:

public interface ICanRead
{
    string Read();
}

public interface ICanWrite
{
    void Write(string name);
}

public interface ICanReadAndWrite : ICanRead, ICanWrite {}


class ConsoleReadWriter : ICanReadAndWrite
{

    public string Read()
    {
        return Console.ReadLine();
    }

    public void Write(string name)
    {
        Console.WriteLine(name);
    }
}

public interface IReadWriterFactory
{
    ICanReadAndWrite GetClass();
}

public class ConsoleReadWriterFactory : IReadWriterFactory
{
    public ICanReadAndWrite GetClass()
    {
        return new ConsoleReadWriter();
    }
}

为其他类的ReadWrite创建一个实现IReadWriterFactory接口的类。

答案 2 :(得分:0)

我认为您需要的是:

class Program
{
    static void Main(string[] args)
    {
        ReadWriter myReadWriter = new ReadWriter(new ConsoleReaderWriterFactory());
        string test = myReadWriter.Read();
        myReadWriter.Write("this is abstract factory power");
    }
}

public class ConsoleWriter : ICanWrite
{
    public void Write(string name)
    {
        Console.WriteLine(name);
    }
}

public class ConsoleReader : ICanRead
{
    public string Read()
    {
        return Console.ReadLine();
    }
}

public interface ICanWrite
{
    void Write(string name);
}

public interface ICanReadAndWrite : ICanRead, ICanWrite { }

public interface ICanRead
{
    string Read();
}

public class ReadWriter : ICanReadAndWrite
{
    private ICanRead reader;
    private ICanWrite writer;

    public string Read()
    {
        return reader.Read();
    }

    public void Write(string name)
    {
        writer.Write(name);
    }

    public ReadWriter(IReaderWriterFactory factory)
    {
        reader = factory.CreateReader();
        writer = factory.CreateWriter();
    }
}

public interface IReaderWriterFactory
{
    ICanRead CreateReader();
    ICanWrite CreateWriter();
}

public class ConsoleReaderWriterFactory : IReaderWriterFactory
{
    public ICanRead CreateReader()
    {
        return new ConsoleReader();
    }

    public ICanWrite CreateWriter()
    {
        return new ConsoleWriter();
    }
}