Design pattern for encapsulating common functionality among user interface controls

I am brainstorming some ideas around a template for use in the following scenario.

I have third-party controls for which I want to add general functionality. Functionality is added by processing several events and performing certain actions when events are triggered along with the addition of some private variables to store state information between events. I want to reuse code and functionality, so I usually do this.

Create a class for this function and pass in the control instance that I want to add to the constructor.

Then I can add event handlers to the control in an instance of the class.

Can anyone think of alternative patterns to use this method for reuse?

+5
source share
3 answers

The most suitable "design pattern" is Observer. The reusable functions that you want to develop can be implemented as simple Control observers that subscribe to some subsets of Control events. Fortunately, many events are implemented in Windows Forms Controls, which makes adding functionality from outside the class almost as easy as from the inside using a regular subclass.

, , DragOver DragDrop (, , DragLeave), DragDropEvent.

, ​​ .

0

:

. . .

+1

, Observer . :

UtilityClass, ( ): observer

Register the various Utility classes (and therefore the controls) for the various Events that interest him: Observed

Now, when a specific event occurs, it updates various observers, where you can delegate the main eventHandling to the control that it wraps, and also have a place to perform custom processing based on the control and the event.

0
source

All Articles