I am thinking of abstracting Event action in order to make further alteration/implementation/maintenance easy since the project I am currently working on has volatile requirement. So I thought of the following concept. I created an implementation of IEventAction which ex. handles closing of planning in my application, I made IEventAction extends IEventAction so I can manage all EventAction using a generic collection; and using a factory, I will get the appropriate event by passing the keypress key to the factory to resolve the correct event as shown in the code below.
interface IEventAction {
}
interface IEventAction<T> : IEvent {
void Fire(T factor);
bool CheckCondition(T factor); //check the condition to fire the event
}
class PlanningClose : IEventAction<string> {
GroupBox _gpb;
public PlanningClose(GroupBox gpb){
this._gpb = gpb;
}
public void Fire(string factor) {
this._gpb.SendToBack(); //hide planning groupeBox
}
public bool CheckCondition(string factor){
return factor == "*"; //"*" is keyboard key for close.
}
}
class frmMain : Form {
//logic here
void registerActions() {
ActionsFactory.Register(new PlanningClose(this._gpbPlanning));
}
//logic here
void frmMain_KeyPress(object sender, KeyPressEventArgs e){
//during resolve the factory checks if the event is suited be executing
//CheckCondition(T factor) and return it if appropriate
IEventAction evt = ActionsFactory.Resolve<string>(e.keychar);
evt.Fire(e.KeyChar);
}
//logic here
}
The problem here is that I am managing a lot of key presses in frmMain_KeyPress where I am using the same key several times for different purpose, the problem here is I cant figure out a proper way to handle this problem and keep Action as abstract as possible. I thought about registering named action and resolving them by name also, but I have never used this technique.
//register
ActionsFactory.Register("close_planning", new PlanningClose(this._gpbPlanning);
//call
ActionsFactory.Resolve<string>("close_planning", e.KeyChar);
Does anyone have a better way of implementing this problem. Thank you.
Aucun commentaire:
Enregistrer un commentaire