zoukankan      html  css  js  c++  java
  • Pluge模式

    Pluge模式,是最常见的模式,实现后期绑定。增加程序灵活性,通过配制文件可以方便配制不同的实现。
    这篇POST以简单方式实现,代码中细节就不说了。

    接口:
    namespace SamplePlugePattern.Interface
    {
        
    /// <summary>
        
    /// define a action interface
        
    /// </summary>
        public interface IAction
        {
            
    void Burn();
        }
    }

    实现:
    namespace Machine
    {
        
    public class MachineT50:IAction
        {
            
    public void Burn()
            {
                Console.WriteLine(
    "MachineT50 is burning now.");
            }
        }
    }

    客户类:
    using System;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Configuration;
    using SamplePlugePattern.Interface;

    namespace WorkPlat
    {
        
    /// <summary>
        
    /// Pluge Pattern Sample
        
    /// </summary>
        
    /// <remarks>author PetterLiu http://wintersun.cnblogs.com </remarks>
        class Program
        {
            
    static void Main(string[] args)
            {
                
    string AssemblyName = ConfigurationManager.AppSettings["AssemblyName"];
                
    string TypeName = ConfigurationManager.AppSettings["TypeName"];
                IAction action 
    = UsingActivator(AssemblyName, TypeName);
                action.Burn();
            }

            
    private static IAction UsingCurrentAppDomain(string assemblyname, string typename)
            {
                
    return AppDomain.CurrentDomain.CreateInstanceAndUnwrap(assemblyname, typename) as IAction;
            }

            
    private static IAction UsingActivator(string assemblyname, string typename)
            {
                Assembly assembly 
    = Assembly.Load(assemblyname);
                Type type 
    = assembly.GetType(typename);
                
    return Activator.CreateInstance(type) as IAction;
            }
        }
    }

    这里使用的是appSettings,实际中还可以用自定义配制节。
    <configuration>
      
    <appSettings>
        
    <add key="AssemblyName" value="Machine"/>
        
    <add key="TypeName" value="Machine.MachineT50"/>
      
    </appSettings>
    </configuration>

    Ps:我们还可以用Asp.net中的Provider模式,IOC来实现。
  • 相关阅读:
    nefu 628 Garden visiting
    codeforces 814 C. An impassioned circulation of affection 【尺取法 or DP】
    bzoj 2111: [ZJOI2010]Perm 排列计数 (dp+卢卡斯定理)
    Codeforces Round #423 (Div. 2)
    hdu 5955 Guessing the Dice Roll 【AC自动机+高斯消元】
    poj1322 Chocolate 【 概率DP 】
    poj 3414 Pots 【BFS+记录路径 】
    hdu5194 DZY Loves Balls 【概率论 or 搜索】
    51nod 1515 明辨是非 [并查集+set]
    hdu 1175 连连看 [DFS]
  • 原文地址:https://www.cnblogs.com/wintersun/p/1314596.html
Copyright © 2011-2022 走看看