• 抽象工厂


    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

     

  • 相关阅读:
    VIM下的可视模式的相关知识
    VIM下的插入模式的相关知识:
    VIM下的普通模式的相关知识
    VIM中一些按键的作用:
    大师源于勤奋
    在你放弃以前,问问自己这6个问题
    一键查看IE密码!IE PassView简易教程
    如何查看Chrome浏览器保存的账号密码
    如何查看Firefox中保存的登录密码
    谈谈360浏览器保存密码的差异
  • 原文地址:https://www.cnblogs.com/Luck1996/p/12006937.html
Copyright © 2020-2023  润新知