c# 之抽象工厂模式

 

Email整体项目

Email类


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Service;

namespace Email
{
   public class email:Ifiles
    {
       public void GetEmail() 
       {
           Console.WriteLine("发送邮件了");
       }
    }
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Service;
namespace Email
{
    public class EmailFactory:IfileFactory
    {
        public Ifiles Create() 
        {
            return new email();
        }
    }

    //public class EmailFactory : PhoneFactory 
    //{
    //    public Phone Create() 
    //    {
    //        return new Phones();
    //    }
    //}
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Service;

namespace Email
{
   //public class Phones:Phone
   // {
   //    public void GetPhone() 
   //    {
   //        Console.WriteLine("发送短信了");
   //    }
   // }
}

 引用其他类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Service
{
    public interface Ifiles 
    {
        void GetEmail();
    }

    //public interface Phone 
    //{
    //    void GetPhone();
    //}
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Service
{
    public interface IfileFactory
    {
        Ifiles Create();
    }

    //public interface PhoneFactory 
    //{
    //    Phone Create();
    //}
}

  抽象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Service;
using System.Reflection;


namespace Email
{
    class AbstractFactory
    {

        public Static IfileFactory GetInstace() 
        {
          string names=  System.Configuration.ConfigurationManager.AppSettings[""].ToString();

          return (Service.IfileFactory)Assembly.Load(names).CreateInstance(names + ".EmailFactory");
        }
    }
}

  

 

 

 

使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Service;

namespace Email
{
    class Program
    {
        static void Main(string[] args)
        {
            Ifiles em = (new EmailFactory()).Create();
            em.GetEmail();
            Console.ReadKey();
        }
    }
}

  

posted @ 2016-08-04 09:35  尘梦  阅读(168)  评论(0编辑  收藏  举报