using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication4
{
class Factory
{
public static ITest create(int itype) // return a ITest(interface) Type. This interface can be used to save the return method.
{
if (itype == 1)
{
return new Test();
}
else if (itype == 2)
{
return new Test2();
}
else if (itype == 3)
{
return new Test3();
}
else
{
return new Test4();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication4
{
interface ITest
{
// This interface class collects the same method(s) from Test.cs, Test2.cs, Test3.cs and Test4.cs.
// So that we can modify these classes through modify the methods in these classes.
string iText();
// string iText2();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication4
{
class Test : ITest
{
#region ITest Members
public string iText()
{
// TODO: Add Test.printText implementation
return ("Test string.");
}
//public void testmethod()
//{
// int aaa;
//}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication4
{
class Test2 : ITest
{
#region ITest Members
public string iText()
{
// TODO: Add Test.printText implementation
return ("Test2 string.");
}
//public void testmethod1()
//{
// int aaa;
//}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication4
{
class Test3 : ITest
{
#region ITest Members
public string iText()
{
// TODO: Add Test.printText implementation
return ("Test3 string.");
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication4
{
class Test4 : ITest
{
#region ITest Members
public string iText()
{
// TODO: Add Test.printText implementation
return ("Test4 string.");
}
//public void testmethod4()
//{
// int aaa;
//}
#endregion
}
}
private void button1_Click(object sender, EventArgs e)
{
ITest it; // We define a interface function it.
it = Factory.create(4); // Return a interface function, after Factory.create.
this.label1.Text = it.iText(); // Directly use the interface function "it", just like Test4.iText();
}