code_junkie

Is it possible to avoid a downcast

Nov 14th, 2011
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. class Word
  2. {
  3. System.Drawing.Font font; //a System type
  4. string text;
  5. }
  6.  
  7. class Canvass
  8. {
  9. System.Drawing.Graphics graphics; //another, related System type
  10. ... and other data members ...
  11. //a method whose implementation combines the two System types
  12. internal void draw(Word word, Point point)
  13. {
  14. //make the System API call
  15. graphics.DrawString(word.text, word.font, Brushes.Block, point);
  16. }
  17. }
  18.  
  19. interface IMyFont
  20. {
  21. }
  22.  
  23. interface IMyGraphics
  24. {
  25. void drawString(string text, IMyFont font, Point point);
  26. }
  27.  
  28. class Word
  29. {
  30. IMyFont font; //no longer depends on System.Drawing.Font
  31. string text;
  32. }
  33.  
  34. class Canvass
  35. {
  36. IMyGraphics graphics; //no longer depends on System.Drawing.Graphics
  37. ... and other data ...
  38.  
  39. internal void draw(Word word, Point point)
  40. {
  41. //use interface method instead of making a direct System API call
  42. graphics.drawText(word.text, word.font, point);
  43. }
  44. }
  45.  
  46. class MyFont : IMyFont
  47. {
  48. System.Drawing.Font theFont;
  49. }
  50.  
  51. class MyGraphics : IMyGraphics
  52. {
  53. System.Drawing.Graphics theGraphics;
  54.  
  55. public void drawString(string text, IMyFont font, Point point)
  56. {
  57.  
  58. //!!! downcast !!!
  59.  
  60. System.Drawing.Font theFont = ((MyFont)font).theFont;
  61.  
  62. //make the System API call
  63. theGraphics.DrawString(word.text, theFont, Brushes.Block, point);
  64. }
  65. }
  66.  
  67. class Word
  68. {
  69. //System.Drawing.Font font; //declared in a subclass of Word
  70. string text;
  71. }
  72.  
  73. class Canvass
  74. {
  75. //System.Drawing.Graphics graphics; //declared in a subclass of Canvass
  76. //concrete draw method is defined in a subclass of Canvass
  77. internal abstract void draw(Word word, Point point);
  78. }
  79.  
  80. public struct FontHandle
  81. {
  82. public readonly int handleValue;
  83. FontHandle(int handleValue)
  84. {
  85. this.handleValue = handleValue;
  86. }
  87. }
  88.  
  89. [public|internal] MyFont GetFont(IFont font) {...} // or just Font
  90.  
  91. public void drawString(string text, IMyFont font, Point point)
  92. {
  93. using(System.Drawing.Font theFont = GetFont(font))
  94. {
  95. theGraphics.DrawString(word.text, theFont, Brushes.Block, point);
  96. }
  97. // etc
  98. }
  99.  
  100. interface IFont {
  101. object Font { get; }
  102. }
  103.  
  104. class MyFont : IFont {
  105. object Font { get { return ...; } }
  106. }
  107.  
  108. public void DrawString(string text, IFont font, Point point)
  109. {
  110. System.Drawing.Font f = (Font)font.Font;
  111. graphics.DrawString(text, f, Brushes.Block, point);
  112. }
Add Comment
Please, Sign In to add comment