码迷,mamicode.com
首页 > 其他好文 > 详细

抽象工厂

时间:2019-12-08 20:14:24      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:manage   空间   on()   gic   工厂   console   except   setting   pps   

1,创建Models

2,创建业务接口IBusinessInterface----添加Models引用

3,创建业务实现BusinessClass1与BusinessClass2(注意:两组中的实现类要同名,但命名空间不可以相同)---添加Models和IBusinessInterface引用

4,创建工厂Factroy

5,Program中添加所有引用

技术图片

 Models代码

技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Models
{
    public class Car
    {
        public string WheelName { get; set; }
        public string LightName { get; set; }
    }
}
Car

 

技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Models
{
   public class Person
    {
        public string HeadName { get; set; }
        public string EarName { get; set; }
    }
}
Person

 

 

 

 IBusinessInterface代码

技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IBusinessInterface
{
    public interface ICarLogic
    {
        void Wheel(int Param1);
        void Light(int Param1);
    }
}
ICarLogic

 

技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IBusinessInterface
{
   public interface IPersonLogic
    {
        void Head(int param1);
        void Ear(int Param1);
    }
}
IPersonLogic

 

BusinessClass1代码

技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IBusinessInterface;
using Models;

namespace BusinessClass1
{
    public class CarLogic : ICarLogic
    {
        public void Light(int Param1)
        {
            throw new NotImplementedException();
        }

        public void Wheel(int Param1)
        {
            throw new NotImplementedException();
        }
    }
}
CarLogic

 

技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IBusinessInterface;
using Models;

namespace BusinessClass1
{
    public class PersonLogic : IPersonLogic
    {

        public void Ear(int Param1)
        {
            Person person = new Person { EarName = "耳朵" };
            Console.WriteLine($"我有{Param1}只 { person.EarName}");
        }

        public void Head(int param1)
        {
            throw new NotImplementedException();
        }
    }
}
PersonLogic

 

BusinessClass2代码

技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using IBusinessInterface;
using Models;
namespace BusinessClass2
{
    public class CarLogic : ICarLogic
    {
        public void Light(int Param1)
        {
            throw new NotImplementedException();
        }

        public void Wheel(int Param1)
        {
            throw new NotImplementedException();
        }
    }
}
CarLogic

 

技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using IBusinessInterface;
using Models;
namespace BusinessClass2
{
    public class PersonLogic : IPersonLogic
    {
        public void Ear(int Param1)
        {
            throw new NotImplementedException();
        }

        public void Head(int param1)
        {
            throw new NotImplementedException();
        }
    }
}
PersonLogic

 

Factroy代码

技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Reflection;
using System.Configuration;

namespace Factory
{
    public class ProductsFactory
    {
        private static string Business = ConfigurationManager.AppSettings["Business"];
        public static T GetT<T>(string ClassName)
        {
            return (T) Assembly.Load(Business).CreateInstance(Business+"."+ClassName);       
        }
    }
}
ProductsFactory

 

Program代码

技术图片
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Factory;
using IBusinessInterface;
namespace 抽象工厂
{
    class Program
    {
        static void Main(string[] args)
        {
            IPersonLogic personLogic = ProductsFactory.GetT<IPersonLogic>("PersonLogic");
            personLogic.Ear(2);
            Console.ReadLine();
        }
    }
}
Program

 

App.config代码

技术图片
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
  
  <appSettings>
    <add key="Business" value="BusinessClass1"/>    
  </appSettings>
  
</configuration>
App.config

 

 

 

 

 技术图片

 

 

 

 

 

 

 

 

抽象工厂

标签:manage   空间   on()   gic   工厂   console   except   setting   pps   

原文地址:https://www.cnblogs.com/Luck1996/p/12006937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!