抽象工厂

Posted luck1996

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了抽象工厂相关的知识,希望对你有一定的参考价值。

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

 

 

 

 

 技术图片

 

 

 

 

 

 

 

 

以上是关于抽象工厂的主要内容,如果未能解决你的问题,请参考以下文章

一. 抽象工厂&工厂方法&简单工厂方法

工厂方法与抽象工厂

设计模式之工厂方法和抽象工厂

C++工厂模式(简单工厂工厂方法抽象工厂)

C++工厂模式(简单工厂工厂方法抽象工厂)

设计模式学习——简单工厂模式工厂模式抽象工厂模式