2011-01-26 16 views
10

Ho appena finito uno stage di 6 mesi presso un'azienda che utilizza C# per la maggior parte della propria programmazione. Durante questo periodo ho usato per la prima volta e mi sono abituato al modo C# di fare eventi. Come mostrato di seguito:Eventi simili a C# in linguaggio di programmazione D

acc.AccountBalanceLow += new AccountBalanceDelegate(atm.AccountToLow); 
acc.AccountBalanceLow +=new AccountBalanceDelegate(atm.AccountToLowAgain); 

D supporta tali costrutti? Immagino che uno possa essere creato dall'utente utilizzando l'overloading dell'operatore, ma non ne sono del tutto sicuro. Se non è possibile quale sarebbe allora un modo comune di farlo, allora?

risposta

11

Il costrutto equivalente in D indica Signals and Slots. Questo è un modo diverso per implementare lo Observer Pattern, che è effettivamente ciò che fa un evento C#.

1

Se ti senti la necessità di utilizzare C# stile-eventi invece di segnali e slot, sono estremamente semplice da implementare:

module fluidity.core.event; 

class Event { 
    alias void delegate(EventArgs) handler_t; 

    handler_t[] handlers; 
    Object owner; 

    this() {} 
    this(Object o) { owner = o; } 

    void attach(handler_t handler) { 
     if (handler) 
      handlers ~= handler; 
    } 

    void detach(handler_t handler) { 
     int i = -1; 
     foreach (j, h; handlers) 
     { 
      if (h is handler) 
      { 
       i = j; 
       break; 
      } 
     } 

     if (i > -1) 
      handlers = handlers[0..i] ~ handlers[i+1..$]; 
    } 

    void raise() { raise(new EventArgs(owner)); } 
    void raise(EventArgs e) { 
     // call all handlers 
     foreach (handler; handlers) 
     { 
      if (handler) 
       handler(e); 
     } 
    } 

    void opAddAssign(handler_t handler) { 
     attach(handler); 
    } 

    void opSubAssign(handler_t handler) { 
     detach(handler); 
    } 

} 

class EventArgs { 
    Object source; 
    bool handled; 

    void handle() { handled = true; } 

    this() {} 
    this(Object s) { 
     source = s; 
    } 
} 
0

Partenza sistema di eventi di DFL. Funziona ESATTAMENTE allo stesso modo di C# .NET.

DFL Event Example

Scarica DFL, afferrare il modulo di eventi e utilizzarlo nel modo che preferite. L'ho modificato per utilizzare argomenti modello variadic. Questo dà la massima flessibilità.

http://www.dprogramming.com/dfl098.zip

1

Ecco un esempio di C# eventi di stile utilizzando segnali, slot, e un template:

events.d:

import std.signals; 

class Event(T...){ 
    mixin Signal!(T);  

    void broadcast(T args){ 
     emit(args); 
    }  
    void opAddAssign(slot_t slot){ 
     connect(slot); 
    } 
    void opSubAssign(slot_t slot) { 
     disconnect(slot); 
    } 
} 

dichiarazione:

public Event!(int) onSomeEventOfInt; 
    public Event!(string, int) onSomeEventOfStringAndInt; 

istanziazione:

this.onSomeEventOfInt = new Event!(int)(); 
    this.onSomeEventOfStringAndInt = new Event!(string, int)(); 

evento incendio:

int i = 4; 
    string str = "hello"; 
    this.onSomeEventOfInt.broadcast(i); 
    this.onSomeEventOfStringAndInt.broadcast(str, 4); 

registrazione osservatore:

obj1.onSomeEventOfInt += &handleEventOfInt 
    obj1.onSomeEventOfStringAndInt += &handleEventOfStringAndInt 

    void handleEventOfInt(int g) 
    { /*do something */ } 
    void handleEventOfStringAndInt(string str, int g) 
    { /*do something */ } 
Problemi correlati