Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Word
- {
- System.Drawing.Font font; //a System type
- string text;
- }
- class Canvass
- {
- System.Drawing.Graphics graphics; //another, related System type
- ... and other data members ...
- //a method whose implementation combines the two System types
- internal void draw(Word word, Point point)
- {
- //make the System API call
- graphics.DrawString(word.text, word.font, Brushes.Block, point);
- }
- }
- interface IMyFont
- {
- }
- interface IMyGraphics
- {
- void drawString(string text, IMyFont font, Point point);
- }
- class Word
- {
- IMyFont font; //no longer depends on System.Drawing.Font
- string text;
- }
- class Canvass
- {
- IMyGraphics graphics; //no longer depends on System.Drawing.Graphics
- ... and other data ...
- internal void draw(Word word, Point point)
- {
- //use interface method instead of making a direct System API call
- graphics.drawText(word.text, word.font, point);
- }
- }
- class MyFont : IMyFont
- {
- System.Drawing.Font theFont;
- }
- class MyGraphics : IMyGraphics
- {
- System.Drawing.Graphics theGraphics;
- public void drawString(string text, IMyFont font, Point point)
- {
- //!!! downcast !!!
- System.Drawing.Font theFont = ((MyFont)font).theFont;
- //make the System API call
- theGraphics.DrawString(word.text, theFont, Brushes.Block, point);
- }
- }
- class Word
- {
- //System.Drawing.Font font; //declared in a subclass of Word
- string text;
- }
- class Canvass
- {
- //System.Drawing.Graphics graphics; //declared in a subclass of Canvass
- //concrete draw method is defined in a subclass of Canvass
- internal abstract void draw(Word word, Point point);
- }
- public struct FontHandle
- {
- public readonly int handleValue;
- FontHandle(int handleValue)
- {
- this.handleValue = handleValue;
- }
- }
- [public|internal] MyFont GetFont(IFont font) {...} // or just Font
- public void drawString(string text, IMyFont font, Point point)
- {
- using(System.Drawing.Font theFont = GetFont(font))
- {
- theGraphics.DrawString(word.text, theFont, Brushes.Block, point);
- }
- // etc
- }
- interface IFont {
- object Font { get; }
- }
- class MyFont : IFont {
- object Font { get { return ...; } }
- }
- public void DrawString(string text, IFont font, Point point)
- {
- System.Drawing.Font f = (Font)font.Font;
- graphics.DrawString(text, f, Brushes.Block, point);
- }
Add Comment
Please, Sign In to add comment