Reputation: 2393
I'm trying to use the factory pattern and have created three interfaces.
ITest1
public interface ITest1: ITestX
{
void Write(string name);
}
ITest2
public interface ITest2: ITestX
{
void Read(string name);
}
and ITestX
public interface ITestX
{
}
The interfaces ITest1 and ITest1 are implemented in classes A and 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);
}
}
As for the factory, my interface looks like this:
public interface IFactory
{
ITestX GetClass(ClassType cType);
ITestX GetClass(int cType);
}
The class ClassType:
public class ClassT
{
public int Type { get; set; }
public static class ClassType
{
public const int AType = 0;
public const int BType = 1;
}
}
The Factory class which implements the factory interface:
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();
}
}
}
In my test method, I try to use the factory:
Factory factory = new Factory();
var typeClass = factory.GetClass(new ClassT{ Type = ClassT.ClassType.AType });
However, I can't use the Read
or Write
methods because they are not visible in typeClass
. What's going on? Why can't I access them?
Upvotes: 1
Views: 159
Reputation: 4692
I think what you rather need is this:
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();
}
}
Upvotes: 0
Reputation: 4692
Example:
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();
}
}
Create a class implementing the IReadWriterFactory interface for other Classes of ReadWriters.
Upvotes: 1
Reputation: 2101
Because Factory.GetClass
returns ITextX
, and this interface doesn't have that methods declared.
You can try casting that objects to the interface you need and then you will have an opportunity to invoke corresponding methods.
UPDATE: You can see an example implementation on the wiki page.
Upvotes: 2