c# - Portable class library design -
i have portable class library that, example, exposes interface method:
public interface iportablepainter { void paintthesky(portablecolor color); }
then, need define platform-specific implementation, (totally invented, example):
android implementation:
public class androidpainter : iportablepainter { public void paintthesky(portablecolor color) { androidframeworkcolor convertedcolor = tocolor(color); // drawsky needs androidframeworkcolor, not portablecolor androidframeworkpainter.drawsky(convertedcolor); } } public static androidframeworkcolor tocolor(portablecolor color) { return new androidframeworkcolor() { = color.a, r = color.r, g = color.g, b = color.b }; }
ios implementation:
public class iospainter : iportablepainter { public void paintthesky(portablecolor color) { iosframeworkcolor convertedcolor = tocolor(color); // drawsky needs iosframeworkcolor, not portablecolor iosframeworkpainter.drawsky(convertedcolor); } } public static iosframeworkcolor tocolor(portablecolor color) { return new iosframeworkcolor() { alphachannel = color.a, redchannel = color.r, greenchannel = color.g, bluechannel = color.b }; }
note don't own androidframeworkcolor
, iosframeworkcolor
, nor androidframeworkpainter
, iosframeworkpainter
, can't touch classes' definition, consider them built-in on platform base framework.
the questions are:
- is defining converter function, in situation, best choice in terms of performance? define implicit operator, can't because don't own framework specific classes. also, don't feel "clear" writing
tocolor
everytime, since i'll have call lot. - what other patterns or design strategies define abstract/interface method encapsulate pre-existing class method don't own?
Comments
Post a Comment