Iwas building a bridge object class and thought that it would be a loteasier if there was some syntax to do this automatically. There areseveral technical definitions of a bridge (a good one is athttp://en.wikipedia.org/wiki/Bridge_pattern), but I like to think of abridge as a class that implements the same interface as another class,but provides a layer of abstraction from the other class to hide itsimplementation, and in a way allowing the implementation to be variedat runtime (through factories or other means).Implementing a bridge typically requires defining a class with methodsthe same as the type being bridged, and then calling through to theother methods on the type that is bridged from each method on the newclass. This typically is very simple to do, but at the same time a lotof what can be considered unnecessary typing.My thoughts are that there should be some syntax to set this upautomatically.
For example:
[Bridge(TypeToBridgeTo)]
Class abridge : BaseClass, Interface1, … InterfaceN
{
}
This would automatically:
- Create a private instance of type ‘TypeToBridge’
- Create accessors for that instance (a get property to retrieve the instance)
- Automatically generate methods into the abridge class that when called defer into the ‘TypeToBridgeTo’ instance
- Automatically call the default constructor on the bridged type
- Allow declaration on alternate constructor to call through attributes
- Allow overriding of automatically generated methods in the class declaration, and the ability to call default methods.
Perhapsthis could be done better through an alternate syntax with attributes,and code injection, and there would most likely need to be come kind ofruntime initializer that would scan for classes with this attribute andautomatically inject the methods into the class at runtime, and thiscould also be an interesting parallel to AoP.
Technorati Tags: Patterns, P#
9efd231a-47a1-43b7-833b-18e51d39c188|0|.0