關(guān)于委托的模板方法的應(yīng)用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ModelAndCallback
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? ProductFactory productFactory = new ProductFactory();
? ? ? ? ? ? WrapFactory wrapFactory = new WrapFactory();
? ? ? ? ? ? Func<Product> func1 = new Func<Product>(productFactory.MakePizze);
? ? ? ? ? ? Func<Product> func2 = new Func<Product>(productFactory.MakeToyCar);
? ? ? ? ? ? //Box box1 = wrapFactory.WrapProduct(func1);
? ? ? ? ? ? //Box box2 = wrapFactory.WrapProduct(func2);
? ? ? ? ? ? //Console.WriteLine(box1.Product.Name);
? ? ? ? ? ? //Console.WriteLine(box2.Product.Name);
? ? ? ? ? ? Console.WriteLine(func1.Invoke().Name);
? ? ? ? ? ? Console.WriteLine(func2.Invoke().Name);
? ? ? ? }
? ? }
? ? class Product
? ? {
? ? ? ? public string Name { get; set; }
? ? }
? ? class Box
? ? {
? ? ? ? public Product Product { get; set; }
? ? }
? ? class WrapFactory
? ? {
? ? ? ? public Box WrapProduct(Func<Product> getProduct)
? ? ? ? {
? ? ? ? ? ? Box box = new Box();
? ? ? ? ? ? box.Product = getProduct.Invoke();
? ? ? ? ? ? return box;
? ? ? ? }
? ? }
? ? class ProductFactory
? ? {
? ? ? ? public Product MakePizze()
? ? ? ? {
? ? ? ? ? ? Product product = new Product();
? ? ? ? ? ? product.Name = "Pizze";
? ? ? ? ? ? return product;
? ? ? ? }
? ? ? ? public Product MakeToyCar()
? ? ? ? {
? ? ? ? ? ? Product product = new Product();
? ? ? ? ? ? product.Name = "ToyCar";
? ? ? ? ? ? return product;
? ? ? ? }
? ? }
}
圖一中注釋掉的寫(xiě)法與沒(méi)有注釋掉的寫(xiě)法debug出來(lái)的結(jié)果都是一樣的,因?yàn)樽⑨尩恼Z(yǔ)句看不懂,所以想問(wèn)下注釋部分的解釋以及我沒(méi)注釋部分的寫(xiě)法是否合理。求大神解答下,謝謝!
2018-10-20
都是取product.name,沒(méi)毛病啊