zoukankan      html  css  js  c++  java
  • 抽象工厂

    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

     

  • 相关阅读:
    题解-CF1375E Inversion SwapSort
    寒门再难出贵子
    js获取链接中的内容方法
    MySQL添加用户、删除用户、授权及撤销权限
    Ubuntu保存退出vim编辑器
    最全!Linux服务器下安装SVN,并添加SVN项目,自动更新项目文件到web目录
    php $_SERVER中的SERVER_NAME 和HTTP_HOST的区别以及REQUEST_URI的讲解
    RESTful API 最佳实践----转载阮一峰
    PHP图像处理(GD库)
    nginx.conf配置
  • 原文地址:https://www.cnblogs.com/Luck1996/p/12006937.html
Copyright © 2011-2022 走看看