gpt4 book ai didi

c# - "adorn objects with functionality"的最佳方法是什么?

转载 作者:行者123 更新时间:2023-11-30 22:48:45 24 4
gpt4 key购买 nike

我做了下面的示例,它使工厂能够将具有功能的对象打包,但是问题是功能与对象分离

我的最终目标是附加功能,如logsavedisplay,它们对每个不同对象的特定属性进行操作。

我如何保留此示例的外部装饰方面,但启用诸如将对象数据保存到数据库的“保存”或记录其事件的“日志”等功能?

using System;
using System.Collections.Generic;

namespace FuncAdorn3923
{
class Program
{
static void Main(string[] args)
{

Customer customer = new Customer();
ObjectFactory.Instance.AdornFunctionality(customer, "add");
Console.WriteLine(customer.CallAlgorithm("add", 64, 36));

Employee employee = new Employee();
ObjectFactory.Instance.AdornFunctionality(employee, "add");
ObjectFactory.Instance.AdornFunctionality(employee, "subtract");
Console.WriteLine(employee.CallAlgorithm("add", 5, 15));
Console.WriteLine(employee.CallAlgorithm("subtract", 66, 16));

Console.ReadLine();
}
}

public class ObjectFactory
{
private static ObjectFactory singleton;

public void AdornFunctionality(AdornedObject ao, string idCode)
{
Func<int, int, int> add = (i, j) => i + j;
Func<int, int, int> subtract = (i, j) => i - j;

switch (idCode)
{
case "add":
ao.LoadAlgorithm(idCode, add);
break;
case "subtract":
ao.LoadAlgorithm(idCode, subtract);
break;
}
}

public static ObjectFactory Instance
{
get
{
if (singleton == null)
singleton = new ObjectFactory();
return singleton;
}
}

}

public abstract class AdornedObject
{
private Dictionary<string, Func<int, int, int>> algorithms =
new Dictionary<string, Func<int, int, int>>();

public void LoadAlgorithm(string idCode, Func<int,int,int> func)
{
algorithms.Add(idCode, func);
}

public int CallAlgorithm(string idCode, int i1, int i2)
{
Func<int,int,int> func = algorithms[idCode];
return func.Invoke(i1, i2);
}
}

public class Customer : AdornedObject
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int NumberOfProductsBought { get; set; }
}

public class Employee : AdornedObject
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}

}

最佳答案

我个人会推荐一种更好的设计模式,如访问者模式,但为了它的值(value),您可以通过放弃类型安全来使您的代码正常工作。使用 Delegate 而不是其派生类 FuncAction:

    static void Main(string[] args)
{

Customer customer = new Customer();
ObjectFactory.Instance.AdornFunctionality(customer, "add");
Console.WriteLine(customer.CallAlgorithm("add", 64, 36));

Employee employee = new Employee();
ObjectFactory.Instance.AdornFunctionality(employee, "add");
ObjectFactory.Instance.AdornFunctionality(employee, "subtract");
ObjectFactory.Instance.AdornFunctionality(employee, "save");
Console.WriteLine(employee.CallAlgorithm("add", 5, 15));
Console.WriteLine(employee.CallAlgorithm("subtract", 66, 16));
Console.WriteLine(employee.CallAlgorithm("save"));

Console.ReadLine();
}
}

public class ObjectFactory
{
private static ObjectFactory singleton;

public void AdornFunctionality(AdornedObject ao, string idCode)
{
Func<int, int, int> add = (i, j) => i + j;
Func<int, int, int> subtract = (i, j) => i - j;
Action save = () => Console.WriteLine("{0} has been saved", ao.ToString());

switch (idCode)
{
case "add":
ao.LoadAlgorithm(idCode, add);
break;
case "subtract":
ao.LoadAlgorithm(idCode, subtract);
break;
case "save":
ao.LoadAlgorithm(idCode, save);
break;
}
}

public static ObjectFactory Instance
{
get
{
if (singleton == null)
singleton = new ObjectFactory();
return singleton;
}
}

}

public abstract class AdornedObject
{
private Dictionary<string, Delegate> algorithms = new Dictionary<string, Delegate>();

public void LoadAlgorithm(string idCode, Delegate func)
{
algorithms.Add(idCode, func);
}

public object CallAlgorithm(string idCode, params object[] args)
{
Delegate func = algorithms[idCode];
return func.DynamicInvoke(args);
}
}

关于c# - "adorn objects with functionality"的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1538257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com