Youhoooo

导航

【转】背后的故事之 - 快乐的Lambda表达式(二)

快乐的Lambda表达式

  上一篇 背后的故事之 - 快乐的Lambda表达式(一)我们由浅入深的分析了一下Lambda表达式。知道了它和委托以及普通方法的区别,并且通过测试对比他们之间的性能,然后我们通过IL代码深入了解了Lambda表达式,以及介绍了如何在.NET中用Lambda表达式来实现JavaScript中流行的一些模式。

  今天,我们接着来看Lambda表达式在.NET中还有哪些新鲜的玩法。

Lambda表达式玩转多态

  Lambda如何实现多态?我们用抽象类和虚方法了,为什么还要用Lambda这个玩意?且看下面的代码:

class MyBaseClass
{
    public Action SomeAction { get; protected set; }

    public MyBaseClass()
    {
        SomeAction = () =>
        {
            //Do something!
        };
    }
}

class MyInheritedClass : MyBaseClass
{
    public MyInheritedClass()
    {
	    SomeAction = () => {
		    //Do something different!
	    };
    }
}

  我们的基类不是抽象类,也没有虚方法,但是把属性通过委托的方式暴露出来,然后在子类中重新为我们的SomeAction赋予一个新的表达式。这就是我们实现多态的过程,当然父类中的SomeAction的set有protected的保护级别,不然就会被外部随易修改了。但是这还不完美,父类的SomeAction在子类中被覆盖之后,我们彻底访问不到它了,要知道真实情况是我们可以通过base来访问父类原来的方法的。接下来就是实现这个了:

class MyBaseClass
{
	public Action SomeAction { get; private set; }

	Stack<Action> previousActions;

	protected void AddSomeAction(Action newMethod)
	{
		previousActions.Push(SomeAction);
		SomeAction = newMethod;
	}

	protected void RemoveSomeAction()
	{
		if(previousActions.Count == 0)
			return;

		SomeAction = previousActions.Pop();
	}

	public MyBaseClass()
	{
		previousActions = new Stack<Action>();

		SomeAction = () => {
			//Do something!
		};
	}
}

  上面的代码中,我们通过AddSomeAction来实现覆盖的同时,将原来的方法保存在previousActions中。这样我们就可以保持两者同时存在了。

  大家知道子类是不能覆盖父类的静态方法的,但是假设我们想实现静态方法的覆盖呢?

void Main()
{
	var mother = HotDaughter.Activator().Message;
	//mother = "I am the mother"
	var create = new HotDaughter();
	var daughter = HotDaughter.Activator().Message;
	//daughter = "I am the daughter"
}

class CoolMother
{
	public static Func<CoolMother> Activator { get; protected set; }
	
	//We are only doing this to avoid NULL references!
	static CoolMother()
	{
		Activator = () => new CoolMother();
	}

	public CoolMother()
	{
		//Message of every mother
		Message = "I am the mother";
	}
	
	public string Message { get; protected set; }
}

class HotDaughter : CoolMother
{
	public HotDaughter()
	{
		//Once this constructor has been "touched" we set the Activator ...
		Activator = () => new HotDaughter();
		//Message of every daughter
		Message = "I am the daughter";
	}
}

  这里还是利用了将Lambda表达式作为属性,可以随时重新赋值的特点。当然这只是一个简单的示例,真实项目中并不建议大家这么去做。 

方法字典

  实际上这个模式我们在上一篇的返回方法中已经讲到了,只是没有这样一个名字而已,就算是一个总结吧。故事是这样的,你是不是经常会写到switch-case语句的时候觉得不够优雅?但是你又不想去整个什么工厂模式或者策略模式,那怎么样让你的代码看起来高级一点呢?

public Action GetFinalizer(string input)
{
	switch
	{
		case "random":
			return () => { /* ... */ };
		case "dynamic":
			return () => { /* ... */ };
		default:
			return () => { /* ... */ };
	}
}

//-------------------变身之后-----------------------
Dictionary<string, Action> finalizers;

public void BuildFinalizers()
{
	finalizers = new Dictionary<string, Action>();
	finalizers.Add("random", () => { /* ... */ });
	finalizers.Add("dynamic", () => { /* ... */ });
} 

public Action GetFinalizer(string input)
{
	if(finalizers.ContainsKey(input))
		return finalizers[input];

	return () => { /* ... */ };
}

  好像看起来是不一样了,有那么一点味道。但是一想是所有的方法都要放到那个BuildFinalizers里面,这种组织方法实在是难以接受,我们来学学插件开发的方式,让它自己去找所有我们需要的方法。

static Dictionary<string, Action> finalizers;

// 在静态的构造函数用调用这个方法
public static void BuildFinalizers()
{
	finalizers = new Dictionary<string, Action>();

	// 获得当前运行程序集下所有的类型
	var types = Assembly.GetExecutingAssembly().GetTypes();

	foreach(var type in types)
	{
		// 检查类型,我们可以提前定义接口或抽象类
		if(type.IsSubclassOf(typeof(MyMotherClass)))
		{
			// 获得默认无参构造函数
			var m = type.GetConstructor(Type.EmptyTypes);

			// 调用这个默认的无参构造函数
			if(m != null)
			{
				var instance = m.Invoke(null) as MyMotherClass;
				var name = type.Name.Remove("Mother");
				var method = instance.MyMethod;
				finalizers.Add(name, method);
			}
		}
	}
} 

public Action GetFinalizer(string input)
{
	if(finalizers.ContainsKey(input))
		return finalizers[input];

	return () => { /* ... */ };
}

  如果要实现插件化的话,我们不光要能够加载本程序集下的方法,还要能随时甚至运行时去加载外部的方法,请继续往下看:

internal static void BuildInitialFinalizers()
{
	finalizers = new Dictionary<string, Action>();
	LoadPlugin(Assembly.GetExecutingAssembly());
}

public static void LoadPlugin(Assembly assembly)
{
	var types = assembly.GetTypes();
	foreach(var type in types)
	{
		if(type.IsSubclassOf(typeof(MyMotherClass)))
		{
			var m = type.GetConstructor(Type.EmptyTypes);

			if(m != null)
			{
				var instance = m.Invoke(null) as MyMotherClass;
				var name = type.Name.Remove("Mother");
				var method = instance.MyMethod;
				finalizers.Add(name, method);
			}
		}
	}
} 

  现在,我们就可以用这个方法,给它指定程序集去加载我们需要的东西了。

  最后留给大家一个问题,我们能写递归表达式么?下面的方法如果用表达式如何写呢?

int factorial(int n)
{
	if(n == 0)
		return 1;
	else
		return n * factorial(n - 1);
}

  

posted on 2018-01-12 14:26  RYouhoo  阅读(702)  评论(0编辑  收藏  举报