工厂模式:在一个类中定义方法,返回需要的对象,在主函数中调用。
实现功能:
在下拉框中选择要输出的格式,点击读取,把读取到的内容显示在文本框内。
声明接口IReadFile.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FactoryModeDemo.InterfaceReadFile
{
public interface IReadFile
{
string readFile();
}
}
读取txt文件类:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FactoryModeDemo.InterfaceReadFile;
namespace FactoryModeDemo.ReadFileItem
{
public class ReadTxt : IReadFile
{
public string readFile()
{
StreamReader reader = new StreamReader(@"E:\YCL\WindowsFormsApplication6\WindowsFormsApplication6\File\TextFile1.txt");
string txt = reader.ReadToEnd();
reader.Close();
return txt;
}
}
}
读取xml文件类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace FactoryModeDemo.ReadFileItem
{
public class ReadXml : IReadFile
{
public string readFile()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"E:\YCL\WindowsFormsApplication6\WindowsFormsApplication6\File\XMLFile1.xml");
//XmlNode cNodes = xmlDoc.SelectSingleNode("columns");
//string text = (cNodes.SelectSingleNode("from")).InnerText;
string text = xmlDoc.SelectSingleNode("columns").SelectSingleNode("from").InnerText;
return text;
}
}
}
接下来有两种方法实现功能:
第一种:传入选中的Index
form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FactoryModeDemo.InterfaceReadFile;
using FactoryModeDemo.ReadFileItem;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ReadFile file = new ReadFile();
IReadFile readh = file.GetReadObject(cmbFile.SelectedIndex);
string readStr = readh.readFile();
this.txtText.Text = readStr;
}
}
}
Readfile.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using FactoryModeDemo.InterfaceReadFile;
using FactoryModeDemo.ReadFileItem;
namespace FactoryModeDemo.ReadFileItem
{
public class ReadFile
{
public IReadFile GetReadObject(int index)
{
IReadFile objectClass = null;
switch (index)
{
case 0:
objectClass = new ReadTxt();
break;
case 1:
objectClass = new ReadXml();
break;
}
return objectClass;
}
}
第二种方法:
传入选中的文本内容:
form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FactoryModeDemo.InterfaceReadFile;
using FactoryModeDemo.ReadFileItem;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
ReadFile file = new ReadFile();
string FileType = cmbFile.SelectedItem.ToString();
IReadFile readh1 = file.GetReadObject(FileType);
string readStr = readh1.readFile();
this.txtText.Text = readStr;
}
}
}
ReadFile.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using FactoryModeDemo.InterfaceReadFile;
using FactoryModeDemo.ReadFileItem;
namespace FactoryModeDemo.ReadFileItem
{
public class ReadFile
{
public IReadFile GetReadObject(string className)
{
IReadFile objectClass = null;
//获取继承了接口的类,判断传进来的类名和实现接口的类名是否能匹配上
//获取当前方法命名空间:System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace
var bases = Assembly.GetExecutingAssembly().GetTypes()
.Where(item => item.GetInterfaces().Contains(typeof(IReadFile)) && item.IsClass == true && item.FullName == (MethodBase.GetCurrentMethod().DeclaringType.Namespace +"."+ className))
.Select(type =>(IReadFile)Activator.CreateInstance(type))
.First();
if (bases != null)
objectClass = (IReadFile)bases;
return objectClass;
}
}
}