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; } } }
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; } } }
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); } }
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); } }
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(); } } }
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(); } } }
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(); } } }
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(); } } }
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); } } }
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(); } } }
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>