c#抽象工厂实例

来源:互联网 发布:吊带袜天使结局 知乎 编辑:程序博客网 时间:2024/05/16 10:47

 在工作中采有良好模式方法,可以提高程序的可读性,可扩展性,而且易于管理。下面就为大家介绍一下抽象工厂。

1基本流程:

实际操作类-------->抽象控制类-------->接口定义类

                                                                  接口实现类1    接口实现类2

(图片传不上去了,rar文件里面有)

2代码

接口定义类

using System;

namespace IFactoryList
{
    
/// <summary>
    
/// 接口中的方法必须是保护类型,而且继承后必须实现。
    
/// </summary>

    public interface IFactory
    
{        
        
void DoOne();
        
void DoTwo();
        
void DoThree();
    }

}

抽象制作类

 

using System;
using System.Reflection;
namespace IFactoryList
{
    
/// <summary>
    
/// FacotryControl 的摘要说明。
    
/// </summary>

    public class FacotryControl
    
{
        
public FacotryControl()
        
{
            
        }

        
public void FactryCreateObject(string typeName)
        
{
            
try
            
{
                Assembly assembly 
= Assembly.LoadFrom("IFactoryList.dll");
                Type t 
= assembly.GetType(typeName);
                IFactory ifa 
=null
                ifa 
= (IFactory)System.Activator.CreateInstance(t);
                Console.Read();
            }

            
catch(Exception ex)
            
{
                Console.WriteLine(
"类不存在:"+ex.ToString());
            }

        }

    }

}

具体实现类

 

using System;

namespace IFactoryList
{
    
/// <summary>
    
/// DoOne 的摘要说明。
    
/// </summary>

    public class IFactoryDoOne:IFactoryList.IFactory
    
{
        
public IFactoryDoOne()
        
{
            
this.DoOne();
            
this.DoTwo();
            
this.DoThree();
        }

        
IFactory 成员
    }

}

实际操作类

 

using System;
using System.Reflection;
using IFactoryList;
namespace IFactoryDemo
{
    
/// <summary>
    
/// 
    
/// </summary>

    class ActionFactory
    
{
        
/// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>

        [STAThread]
        
static void Main(string[] args)
        
{
            IFactoryList.FacotryControl fc 
= new FacotryControl();
            fc.FactryCreateObject(
"IFactoryList.IFactoryDoOne");
        }

    }

}

3、关于抽象工厂的实际的操作,还应该配制xml同时操作,具体的情况请大家自己添写。

4、打包下载

5、更多模式尽请期待中(如有不对之处,望指出,谢谢,记得给评论呀)

 

原创粉丝点击