C#委托实现工厂规则注入

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//创建型-工厂模式、工厂规则注入、委托工厂

namespace Chap2_2
{
class Program
{
static void Main(string[] args)
{
Func<EmployeeFactory.EmployeeCreateparameterContext, Employee.EmployeeAddress> addressFactory = (context) =>
{
return new Employee.EmployeeAddress()
{
Address1 = context.AddressString.Split('、')[0],
Address2 = context.AddressString.Split('、')[1]
};
};
// Employee emp = EmployeeFactory.CreateEmployee("Plen Wu", "塔希提、塞舌尔", EmployeeFactory.addressFactory1);
Employee emp = EmployeeFactory.CreateEmployee("Plen Wu", "塔希提、塞舌尔", addressFactory);
Console.WriteLine(string.Format("员工名:{0}",emp.Name));
Console.WriteLine(string.Format("户籍地:{0}", emp.AddressCollection.Address1));
Console.WriteLine(string.Format("工作地:{0}", emp.AddressCollection.Address2));
Console.ReadKey();
}
}
public partial class Employee
{
public class EmployeeAddress
{
public string Address1 { get; set; }
public string Address2 { get; set; }
}
public EmployeeAddress AddressCollection { get; set; }
public string Name { get; set; }
}
/// <summary>
/// 工厂规则通过委托注入,可以保持工厂的稳定性
/// </summary>
public class EmployeeFactory
{
/// <summary>
/// 创建地址的上下文类
/// </summary>
public class EmployeeCreateparameterContext
{
public string AddressString { get; set; }
}
public static Employee CreateEmployee(string name, string addressStr, Func<EmployeeFactory.EmployeeCreateparameterContext, Employee.EmployeeAddress> addressFactory)
{
EmployeeCreateparameterContext parameterContext = new EmployeeCreateparameterContext() { AddressString = addressStr };
return new Employee()
{
Name = name,
AddressCollection = addressFactory(parameterContext)
};
}
static public Func<EmployeeFactory.EmployeeCreateparameterContext, Employee.EmployeeAddress> addressFactory1 = (context) =>
{
return new Employee.EmployeeAddress()
{
Address1 = context.AddressString.Split('、')[0],
Address2 = context.AddressString.Split('、')[1]
};
};
}
}

转载于:https://www.cnblogs.com/sulong/p/4917593.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ 并没有原生的委托机制,但可以通过函数指针和对象指针来模拟委托机制。 在 C++ 中,函数指针可以作为参数传递和返回值,可以使用函数指针来实现委托的功能。而对象指针可以用于保存对象的状态和数据,也可以用于调用对象的成员函数。 以下是一个简单的 C++ 示例代码,演示了如何通过函数指针和对象指针来实现委托机制: ```cpp #include <iostream> #include <functional> using namespace std; class Delegate { public: void (*func)(void*); // 函数指针 void* obj; // 对象指针 void Invoke() { func(obj); // 调用函数指针 } }; class Test { public: void Print() { cout << "Hello, world!" << endl; } }; void PrintTest(void* obj) { Test* test = (Test*)obj; // 将对象指针转换为 Test 类型指针 test->Print(); // 调用对象的成员函数 } int main() { Test test; Delegate delegate; delegate.func = PrintTest; delegate.obj = &test; delegate.Invoke(); // 调用委托 return 0; } ``` 在这个示例代码中,我们定义了一个 `Delegate` 类,其中包含了一个函数指针和一个对象指针。`Invoke` 函数用于调用委托。我们还定义了一个 `Test` 类,其中包含了一个成员函数 `Print`。在 `main` 函数中,我们创建了一个 `Test` 对象 `test`,并创建了一个 `Delegate` 对象 `delegate`,将 `PrintTest` 函数的地址和 `test` 对象的地址分别赋值给 `Delegate` 对象的函数指针和对象指针。最后,我们通过 `delegate.Invoke()` 调用委托,实际上就是调用了 `PrintTest` 函数,并将 `test` 对象传递给了该函数,从而调用了 `Test` 对象的 `Print` 成员函数。 需要注意的是,这只是一个简单的示例,实际的委托机制可能更加复杂,并且需要考虑线程安全等问题。如果需要使用委托机制,建议使用现有的 C++ 第三方库或框架,如 Boost、Qt 等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值