CSharp: Factory Method Pattern in donet 6
/// <summary>
/// The Product declares the interface, which is common to all objects
/// that can be produced by the creator <see cref="Restaurants.Common.Restaurant"/> and its subclasses.
/// 工厂方法模式 Factory Method - Creational Pattern
/// </summary>
public interface IMeal
{
/// <summary>
///
/// </summary>
void ShowDescription();
}
/// <summary>
/// The Creator class declares the factory method that returns new product objects.
/// 工厂方法模式 Factory Method - Creational Pattern
/// </summary>
public abstract class Restaurant
{
/// <summary>
/// The factory method that returns new product objects.
/// It’s important that the return type of this method matches the product interface.
/// You can declare the factory method as abstract to force all subclasses to implement their own versions of the method.
/// As an alternative, the base factory method can return some default product type.
/// </summary>
/// <returns>Meal.</returns>
public abstract IMeal PrepareMainCourse();
/// <summary>
/// Note, despite its name, product creation is not the primary responsibility of the creator.
/// Usually, the creator class already has some core business logic related to products.
/// The factory method helps to decouple this logic using the concrete product classes.
/// </summary>
public void OrderDailySpecial()
{
Console.WriteLine("Geovin Du 要饮料:苏打水");
var mainCourse = PrepareMainCourse();
mainCourse.ShowDescription();
}
}
/// <summary>
/// Concrete products are different implementations of the product interface <see cref="IMeal"/>.
/// </summary>
public class GreenSalad : IMeal
{
/// <summary>
///
/// </summary>
public void ShowDescription()
{
Console.WriteLine("涂聚文叫单:蔬菜沙拉——有生菜、黄瓜和青橄榄");
}
}
/// <summary>
/// Concrete products are different implementations of the product interface <see cref="IMeal"/>.
/// </summary>
public class Hamburger : IMeal
{
/// <summary>
///
/// </summary>
public void ShowDescription()
{
Console.WriteLine("Geovin Du叫单:汉堡-加牛肉,伍斯特沙司和奶酪。再来一份中餐:鸳鸯火锅.");
}
}
/// <summary>
/// Concrete creators override the base factory method so it returns a different type of product.
/// 工厂方法模式 Factory Method - Creational Pattern
/// </summary>
public class VegetarianRestaurant : Restaurant
{
/// <summary>
///
/// </summary>
/// <returns></returns>
public override IMeal PrepareMainCourse()
{
return new GreenSalad();
}
}
/// <summary>
/// Concrete creators override the base factory method so it returns a different type of product.
/// 工厂方法模式 Factory Method - Creational Pattern
/// </summary>
public class FastFoodRestaurant : Restaurant
{
/// <summary>
///
/// </summary>
/// <returns></returns>
public override IMeal PrepareMainCourse()
{
return new Hamburger();
}
}
/// <summary>
/// 工厂方法模式 Factory Method - Creational Pattern
///
/// </summary>
public class Executor : PatternExecutor
{
/// <summary>
///
/// </summary>
public override string Name => "工厂方法模式 Factory Method - Creational Pattern";
/// <summary>
///
/// </summary>
public override void Execute()
{
Restaurant restaurant = InitializeRestaurant();
restaurant.OrderDailySpecial();
}
/// <summary>
///
/// </summary>
/// <returns></returns>
private Restaurant InitializeRestaurant()
{
// This is usually stored within some configuration
var choosenType = typeof(FastFoodRestaurant).FullName;
choosenType = typeof(VegetarianRestaurant).FullName;
return Assembly.GetExecutingAssembly().CreateInstance(choosenType) as Restaurant;
}
}
调用:
Console.WriteLine("Hello, Geovin Du! 学习 .net 6 ");
//Geovin.Du.DuEmail.EmailExecutor.Execute();
//Geovin.Du.DuStock.StockExecutor.Execute();
// Geovin.Du.DuShoppingCart.ShoppingCartExecutor.Execute();
//Geovin.Du.DuNullObject.SmartphoneApplicationExecutor.Execute();
Geovin.Du.DuFactoryMethod.Executor ex = new Geovin.Du.DuFactoryMethod.Executor();
ex.Execute();
输出:
Hello, Geovin Du! 学习 .net 6 Geovin Du 要饮料:苏打水 涂聚文叫单:蔬菜沙拉——有生菜、黄瓜和青橄榄

哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
浙公网安备 33010602011771号