使用事件创建对象

本文关键字:创建对象 事件 | 更新日期: 2023-09-27 18:29:59

我有一个项目,其中是Client类。我想要一个创建客户端对象的事件。我阅读了有关事件的信息,但我仍然对如何使用事件感到困惑这是我的代码:

public class Client
{
    public string FirstName { get; set; }
    public string SecondName { get; set; }
    public string DateOfBirth { get; set; }
    public string Address { get; set; }
    public Client(string name, string surname, string dateOfBirth, string address)
    {
        FirstName = name;
        SecondName = surname;
        DateOfBirth = name;
        Address = address;
    }
    public event EventHandler NewClient;
    public virtual void OnNewClient(object sender, EventArgs e)
    {
        NewClient(this, e);
    }
class Program
{
    static void Main(string[] args)
    {
        Client client = new Client();
        client.OnNewClient();
    }

    public void CreateNewClient( string name, string surname, string dateOfBirth, string address)
    {
        string Name = name;
        string Surname = surname;
        string Date = dateOfBirth;
        string Address = address;
        Client client = new Client(Name, Surname,Date, Address);
    }
}

使用事件创建对象

我不确定应该从哪里开始…事件属于一个对象,需要订阅。因此,在您创建的对象上创建事件并没有什么帮助,因为您必须在实际创建对象之前订阅事件(这是不可能的)。因此,事件应该在CreateNewClient方法所在的位置。说到这里,您可能希望它也返回创建的客户端。该方法应该引发事件。

一个合适的解决方案可能看起来像这样:

// Client class only cares about itself; is completely independent
public class Client
{
    public string FirstName { get; set; }
    public string SecondName { get; set; }
    public string DateOfBirth { get; set; }
    public string Address { get; set; }
    public Client(string name, string surname, string dateOfBirth, string address)
    {
        FirstName = name;
        SecondName = surname;
        DateOfBirth = name;
        Address = address;
    }
}
// Client factory is responsible for creating clients
class ClientFactory
{
    // Event name should describe what happened
    public event EventHandler ClientCreated;
    public Client CreateNewClient(string name, string surname, string dateOfBirth, string address)
    {
        // no need to copy the arguments into new local variables again; arguments
        // are already local
        Client client = new Client(name, surname, dateOfBirth, address);
        // raise the ClientCreated event
        OnClientCreated();
        // return the created client
        return client;
    }
    // provide this proctected virtual method for raising the event
    protected virtual void OnClientCreated(EventArgs e)
    {
        var handler = ClientCreated;
        if (handler != null)
            handler(this, e);
    }
    // provide private specialized versions for convenience (e.g. to call it without
    // passing an argument)
    private void OnClientCreated()
    {
        OnClientCreated(EventArgs.Empty);
    }
}

像这样使用:

// create the client factory
ClientFactory factory = new ClientFactory();
// subscribe to the event
factory.ClientCreated += (s, e) => {
   Console.WriteLine("Client created.");
};
// create clients
Client c1 = factory.CreateNewClient("Foo", "Bar", "Baz", "Baf");
Client c2 = factory.CreateNewClient("Foo", "Bar", "Baz", "Baf");

使用此代码,该事件几乎不会工作;因为对象不存在,并且事件在创建后立即引发。

换句话说:没有人有机会注册该活动

如果真的想要这种行为,请考虑使用工厂

public static class ClientFactory
{
     public static event Action ClientCreated;
     public static Client CreateClient(..)
     {
         Client retValue = new Client(...);
         if (ClientCreated != null)
             ClientCreated();
         return retValue;
     }
}

现在您有了一个持久事件,客户端对象可以注册该事件。注意,static不是实现它的最佳方式;这是打字最快的。请随意使用Factory模式的更好实现。