C# 简单的工厂模式demo

工厂模式:在一个类中定义方法,返回需要的对象,在主函数中调用。

实现功能:
在下拉框中选择要输出的格式,点击读取,把读取到的内容显示在文本框内。
在这里插入图片描述
在这里插入图片描述
声明接口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;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值