<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>{ Duane.Wingett } - Inheritance</title>
    <link>http://www.duanewingett.info/</link>
    <description>.Net Code Monkey</description>
    <language>en-gb</language>
    <copyright>Duane Wingett</copyright>
    <lastBuildDate>Fri, 11 Apr 2008 20:44:42 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.0.7226.0</generator>
    <managingEditor>mail@duanewingett.info</managingEditor>
    <webMaster>mail@duanewingett.info</webMaster>
    <item>
      <trackback:ping>http://www.duanewingett.info/Trackback.aspx?guid=4cac64a8-2865-4fce-9d68-b79f5c9bfeeb</trackback:ping>
      <pingback:server>http://www.duanewingett.info/pingback.aspx</pingback:server>
      <pingback:target>http://www.duanewingett.info/PermaLink,guid,4cac64a8-2865-4fce-9d68-b79f5c9bfeeb.aspx</pingback:target>
      <dc:creator>Duane Wingett</dc:creator>
      <wfw:comment>http://www.duanewingett.info/CommentView,guid,4cac64a8-2865-4fce-9d68-b79f5c9bfeeb.aspx</wfw:comment>
      <wfw:commentRss>http://www.duanewingett.info/SyndicationService.asmx/GetEntryCommentsRss?guid=4cac64a8-2865-4fce-9d68-b79f5c9bfeeb</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Back to <strong><a href="http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart2.aspx">Part
2</a></strong></p>
        <h3>Review Of Progress So Far
</h3>
        <p>
So, to date we have created an interface for our <strong>ShapeFactory</strong> and
any other shape factory to implement, an enumeration that represents all the available
shapes we may wish to build, and a factory class that can build any of our two
current shapes. The next step is to create a graphical user interface that will call
the factory and receive a couple of shape objects for it's trouble.
</p>
        <h3>The Client Gets What They Deserve
</h3>
        <p>
Next we need a client to display the shapes, so for simplicity we are going to create
a small Windows Forms project called <strong>WindowsApplication </strong>and add it
to the solution. The project will have a single form called <strong>Main</strong> (700x550)
which will have a picture box (<strong>uxPictureBox</strong>) and a button, <strong>uxDrawButton</strong>.
</p>
        <p>
We now need to add a handler for the Draw <strong>Button</strong> and declare a <strong>Bitmap</strong> and
a <strong>Graphics</strong> object at the top of the class. In the form constructor
we will initialise the bitmap to a new instance which will be the size of the <strong>PictureBox</strong>.
We will then create the <strong>Graphics</strong> object from the <strong>Bitmap</strong>.
</p>
        <p>
In the button handler method we will clear the graphics object to a white background.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> partial <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Main
: Form { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">Bitmap</font> _bitmap; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">Graphics</font> _graphics; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> Main()
{ InitializeComponent(); _bitmap <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><font color="#7fffd4">Bitmap</font>(uxPictureBox.Width,
uxPictureBox.Height); _graphics <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><font color="#7fffd4">Graphics</font>.FromImage(_bitmap);
uxPictureBox.Image <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> _bitmap;
} <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> uxDrawButton_Click(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">object</span> sender,
EventArgs e) { _graphics.Clear(Color.White); } }</span>
        </pre>
        <p>
We now need a collection to hold our shapes, so we will declare a list of objects
that implement IShape at the top of the class, and to do this we will need to provide
a reference to the <strong>ShapeInterfaces</strong> project. At the same time add
a reference to the <strong>Factories</strong> project and import the namespaces for
both projects at the top of the form code.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Text; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Windows.Forms; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> Playground.ShapeMaker.Factories; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> Playground.ShapeMaker.ShapeInterfaces; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ShapeMaker.WindowsApplication
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> partial <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Main
: Form { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">Bitmap</font> _bitmap; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">Graphics</font> _graphics; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span> List&lt;<font color="#7fffd4">IShape</font>&gt;
_shapes; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> Main()
{</span>
        </pre>
        <p>
Next we need to add two method stubs that are void of return values, <strong>AddShapes</strong>()
and <strong>DrawShapes</strong>(). Surprisingly, <strong>AddShapes</strong>() will take
responsibility for adding new shapes to our list and <strong>DrawShapes</strong>()
will draw those shapes. We will add a line to create an instance of our list in the
constructor and below that we will place a call to the <strong>AddShapes</strong>()
method. We will place a call in the button handler to the <strong>DrawShapes</strong>()
method, below the line that clears the graphics to a white background. Follow this
with a refresh of the PictureBox.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> Main()
{ InitializeComponent(); _bitmap <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Bitmap(uxPictureBox.Width,
uxPictureBox.Height); _graphics <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> Graphics.FromImage(_bitmap);
uxPictureBox.Image <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> _bitmap;
_shapes <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> List&lt;IShape&gt;();
AddShapes(); } <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Handles the draw button pressed event.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;param name="sender"&gt;&lt;/param&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;param name="e"&gt;&lt;/param&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> uxDrawButton_Click(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">object</span> sender,
EventArgs e) { _graphics.Clear(Color.White); DrawShapes();<br />
uxPictureBox.Refresh(); }</span>
        </pre>
        <p>
In the <strong>AddShapes</strong>() method we need to create a new instance of our <strong>ShapeFactory</strong> object.
Then using the <strong>ShapeFactory</strong> object add two shapes, a circle and a
rectangle to the list.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"> ///
&lt;summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Adds shapes to the list</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> AddShapes()
{ <font color="#7fffd4">ShapeFactory</font> shapeFactory <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><font color="#7fffd4">ShapeFactory</font>();
_shapes.Add(shapeFactory.CreateShape(<font color="#7fffd4">Shape</font>.Circle));
_shapes.Add(shapeFactory.CreateShape(<font color="#7fffd4">Shape</font>.Rectangle));
}</span>
        </pre>
        <p>
Because we know that the shapes Implement the <strong>IShape</strong> interface we
have a contract that says we can call the <strong>Draw</strong>() methods on any of
these shapes. These objects will then take the responsibility for drawing them selves.
So in the <strong>DrawShapes</strong>() method all we need to do is iterate through
all the shapes in the list and call each shapes <strong>Draw</strong>() method.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"> ///
&lt;summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Draws the shapes in the list</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> DrawShapes()
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">foreach</span>(IShape
shape <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">in</span> _shapes)
{ shape.Draw(); } }</span>
        </pre>
        <p>
And that would be it... apart from we haven't added any working code into the Draw()
methods of each concrete object yet!
</p>
        <h3>A Change In The Contract
</h3>
        <p>
Apart from the fact there is no code in the concrete objects to actually draw them
selves, I have just noticed the need to be able to pass the <strong>Graphics</strong> object
into each shape object, so we need to go back to the interfaces and change the signature
for the <strong>Draw</strong>() method.
</p>
        <p>
So if we take a look in the <strong>IShape</strong> interface we need to be able to
pass in a <strong>Graphics</strong> object as a method parameter.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span> IShape
{ <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Represents the center of the shape</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span> Point Center { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">get</span>; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">set</span>;} <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
The method used to draw the shape</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;param name="graphics"&gt;A reference to the Graphics object to draw the shape
on.&lt;/param&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Draw(Graphics
graphics); }</span>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
          </span>
        </pre>
        <p>
And while we are in this project I think we will take the opportunity to extend the <strong>CreateShape</strong>()
method signature in the <strong>IShapeFactory</strong> interface, too. We are going
to add a center point parameter; this will allow us to position the shape where we
like.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span> IShapeFactory
{ <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Use this method to create a shape.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;param name="shape"&gt;Indicates the type of shape to be made.&lt;/param&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;param name="center"&gt;Indicates the center point of the shape.&lt;/param&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;An object that implements the IShape interface.&lt;/returns&gt;</span><font color="#7fffd4">IShape</font> CreateShape(<font color="#7fffd4">Shape</font> shape, <font color="#7fffd4">Point</font> center);
}</span>
        </pre>
        <p>
A quick build of the project will highlight where our change in interface has broken
the contract between it and the objects that implement it so in the <strong>BaseShape</strong> object
we need to add a <strong>Graphics</strong> parameter into the <strong>Draw</strong>()
method. This will be the same in the <strong>Rectangle</strong> and the <strong>Circle</strong> class.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">abstract</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Draw(<font color="#7fffd4">Graphics</font> graphics);</span>
        </pre>
        <p>
In the <strong>ShapeFactory</strong> class we need to extend the <strong>CreateShape</strong>(), <strong>CreateCircle</strong>()
and <strong>CreateRectangle</strong>() methods signatures to include the center point
parameter. Then in the <strong>CreateShape</strong>() method we need to pass the center
point parameter on to the worker method calls.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;A shape object that implements IShape.&lt;/returns&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> IShape
CreateShape(Shape shape, Point center) {</span>
        </pre>
        <p>
In the two worker methods, CreateCircle() and CreateRectangle() we will populate the
object's Center property with the value from the center parameter passed in.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"> ///
&lt;summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
This method creates a circle object.</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;A shape object that implements ICircle.&lt;/returns&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span>
            <font color="#7fffd4">ICircle</font> CreateCircle(Point
center) { <font color="#7fffd4">Circle</font> circle <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><font color="#7fffd4">Circle</font>();
circle.Center <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> center;
circle.Diameter <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 50; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> (<font color="#7fffd4">ICircle</font>)circle;
} <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
This method creates a rectangle object.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;A shape object that implements IRectangle.&lt;/returns&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">IRectangle</font> CreateRectangle(Point
center) { BusinessEntity.<font color="#7fffd4">Rectangle</font> rectangle <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> BusinessEntity.<font color="#7fffd4">Rectangle</font>();
rectangle.Center <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> center;
rectangle.Size <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Size(400,
200); <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> (<font color="#7fffd4">IRectangle</font>)rectangle;
}</span>
        </pre>
        <p>
Then back in the form code we need to alter the calls to the <strong>ShapeFactory</strong><strong>CreateShape</strong>()
methods to include a center position
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"> ///
&lt;summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Adds shapes to the list</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> AddShapes()
{ <font color="#7fffd4">ShapeFactory</font> shapeFactory <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><font color="#7fffd4">ShapeFactory</font>();
_shapes.Add(shapeFactory.CreateShape(<font color="#7fffd4">Shape</font>.Circle, <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><font color="#7fffd4">Point</font>(250,250)));
_shapes.Add(shapeFactory.CreateShape(<font color="#7fffd4">Shape</font>.Rectangle, <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><font color="#7fffd4">Point</font>(350,
200))); } <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Draws the shapes in the list</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> DrawShapes()
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">foreach</span>(<font color="#7fffd4">IShape</font> shape <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">in</span> _shapes)
{ shape.Draw(_graphics); } }</span>
        </pre>
        <h3>Back to the Drawing Board
</h3>
        <p>
So finally we can get back into the concrete shape classes and write the code that
will draw the shape.
</p>
        <p>
So let us look at the Circle's <strong>Draw</strong>() method first. We'll add
the code to allow the Circle to draw it's self.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"> ///
&lt;summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Use this method to draw the shape</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">override</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Draw(Graphics
graphics) { Pen pen <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Pen(Color.DarkGray,
0); <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> left <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span>)Center.X <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">-</span> (Diameter/2); <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> top <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span>)Center.Y <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">-</span> (Diameter/2);
System.Drawing.Rectangle outlineRectangle <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> System.Drawing.Rectangle(left,
top, Diameter, Diameter); graphics.DrawEllipse(pen, outlineRectangle); }</span>
        </pre>
        <p>
And the same for the Rectangle.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"> ///
&lt;summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Use this method to draw the shape</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">override</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Draw(Graphics
graphics) { Pen pen <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Pen(Color.Red,
0); <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> left <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span>)Center.X <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">-</span> (Size.Width <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">/</span> 2); <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> top <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> (<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span>)Center.Y <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">-</span> (Size.Height <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">/</span> 2);
System.Drawing.Rectangle outlineRectangle <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> System.Drawing.Rectangle(left,
top, Size.Width, Size.Height); graphics.DrawRectangle(pen, outlineRectangle); }</span>
        </pre>
        <p>
Now we need to make sure we have set the start up project to be the windows application
and we should be good to go!
</p>
        <p>
          <img src="http://www.duanewingett.info/content/binary/ShapeMaker_Form_01.jpg" border="0" />
        </p>
        <h3>Conclusions
</h3>
        <p>
Now I'm certain that the application architecture and the code may not be laid out
to best practises, and I'm aware there are plenty of parts that could have been better
thought out and implemented, but really I just wanted to get a simple factory based
application down on paper, so to speak, to get it a little clearer in my mind.
</p>
        <p>
If any of these articles have helped you or planted the seed of inquisitiveness
in your mind to find out more about factories then that can only be a good thing.
If there is anything I have done in this article that is quite clearly incorrect or
bad practise, please feel free to leave a comment. Any constructive feed back is appreciated.
</p>
        <p>
The next step will be to make the objects prinatable. But until then, have fun!
</p>
        <p>
          <a href="http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart1.aspx">
            <strong>Part
1</strong>
          </a> | <a href="http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart2.aspx"><strong>Part
2</strong></a></p>
        <a href="http://www.duanewingett.info/content/binary/2008-04-11_ShapeMaker.zip">2008-04-11_ShapeMaker.zip
(103.25 KB)</a> &lt;- Solution files all zipped up. <img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=4cac64a8-2865-4fce-9d68-b79f5c9bfeeb" /></body>
      <title>I can see a pattern forming: First Day At The Factory... Part 3</title>
      <guid isPermaLink="false">http://www.duanewingett.info/PermaLink,guid,4cac64a8-2865-4fce-9d68-b79f5c9bfeeb.aspx</guid>
      <link>http://www.duanewingett.info/2008/04/11/ICanSeeAPatternFormingFirstDayAtTheFactoryPart3.aspx</link>
      <pubDate>Fri, 11 Apr 2008 20:44:42 GMT</pubDate>
      <description>&lt;p&gt;
Back to &lt;strong&gt;&lt;a href="http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart2.aspx"&gt;Part
2&lt;/a&gt;&lt;/strong&gt;
&lt;/p&gt;
&lt;h3&gt;Review Of Progress So Far
&lt;/h3&gt;
&lt;p&gt;
So, to date&amp;nbsp;we have created an interface for&amp;nbsp;our &lt;strong&gt;ShapeFactory&lt;/strong&gt;&amp;nbsp;and
any other shape factory to implement, an enumeration that represents all the available
shapes we may wish to build,&amp;nbsp;and a factory class that can build any of our two
current shapes. The next step is to create a graphical user interface that will call
the factory and receive a couple of shape objects for it's trouble.
&lt;/p&gt;
&lt;h3&gt;The Client Gets What They Deserve
&lt;/h3&gt;
&lt;p&gt;
Next we need a client to display the shapes, so for simplicity we are going to create
a small Windows Forms project called &lt;strong&gt;WindowsApplication &lt;/strong&gt;and add it
to the solution. The project will have a single form called &lt;strong&gt;Main&lt;/strong&gt; (700x550)
which will have a picture box (&lt;strong&gt;uxPictureBox&lt;/strong&gt;) and a button, &lt;strong&gt;uxDrawButton&lt;/strong&gt;.
&lt;/p&gt;
&lt;p&gt;
We now need to add a handler for the Draw &lt;strong&gt;Button&lt;/strong&gt; and declare a &lt;strong&gt;Bitmap&lt;/strong&gt; and
a &lt;strong&gt;Graphics&lt;/strong&gt; object at the top of the class. In the form constructor
we will initialise the bitmap to a new instance which will be the size of the &lt;strong&gt;PictureBox&lt;/strong&gt;.
We will then create the &lt;strong&gt;Graphics&lt;/strong&gt; object from the &lt;strong&gt;Bitmap&lt;/strong&gt;.
&lt;/p&gt;
&lt;p&gt;
In the button handler method we will clear the graphics object to a white background.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; partial &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Main
: Form { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;Bitmap&lt;/font&gt; _bitmap; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;Graphics&lt;/font&gt; _graphics; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; Main()
{ InitializeComponent(); _bitmap &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;font color=#7fffd4&gt;Bitmap&lt;/font&gt;(uxPictureBox.Width,
uxPictureBox.Height); _graphics &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;font color=#7fffd4&gt;Graphics&lt;/font&gt;.FromImage(_bitmap);
uxPictureBox.Image &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; _bitmap;
} &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; uxDrawButton_Click(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;object&lt;/span&gt; sender,
EventArgs e) { _graphics.Clear(Color.White); } }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
We now need a collection to hold our shapes, so we will declare a list of objects
that implement IShape at the top of the class, and to do this we will need to provide
a reference to the &lt;strong&gt;ShapeInterfaces&lt;/strong&gt; project. At the same time add
a reference to the &lt;strong&gt;Factories&lt;/strong&gt; project and import the namespaces for
both projects at the top of the form code.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Text; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Windows.Forms; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; Playground.ShapeMaker.Factories; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; Playground.ShapeMaker.ShapeInterfaces; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ShapeMaker.WindowsApplication
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; partial &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Main
: Form { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;Bitmap&lt;/font&gt; _bitmap; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;Graphics&lt;/font&gt; _graphics; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; List&amp;lt;&lt;font color=#7fffd4&gt;IShape&lt;/font&gt;&amp;gt;
_shapes; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; Main()
{&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Next we need to add two method stubs that are void of return values, &lt;strong&gt;AddShapes&lt;/strong&gt;()
and &lt;strong&gt;DrawShapes&lt;/strong&gt;(). Surprisingly, &lt;strong&gt;AddShapes&lt;/strong&gt;() will&amp;nbsp;take
responsibility for&amp;nbsp;adding new shapes&amp;nbsp;to our list and &lt;strong&gt;DrawShapes&lt;/strong&gt;()
will draw those shapes. We will add a line to create an instance of our list in the
constructor and below that we will place a call to the &lt;strong&gt;AddShapes&lt;/strong&gt;()
method. We will place a call in the button handler to the &lt;strong&gt;DrawShapes&lt;/strong&gt;()
method, below the line that clears the graphics to a white background. Follow this
with a refresh of the PictureBox.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; Main()
{ InitializeComponent(); _bitmap &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Bitmap(uxPictureBox.Width,
uxPictureBox.Height); _graphics &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; Graphics.FromImage(_bitmap);
uxPictureBox.Image &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; _bitmap;
_shapes &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; List&amp;lt;IShape&amp;gt;();
AddShapes(); } &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Handles the draw button pressed event.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="sender"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="e"&amp;gt;&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; uxDrawButton_Click(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;object&lt;/span&gt; sender,
EventArgs e) { _graphics.Clear(Color.White); DrawShapes();&lt;br&gt;
uxPictureBox.Refresh(); }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
In the &lt;strong&gt;AddShapes&lt;/strong&gt;() method we need to create a new instance of our &lt;strong&gt;ShapeFactory&lt;/strong&gt; object.
Then using the &lt;strong&gt;ShapeFactory&lt;/strong&gt; object add two shapes, a circle and a
rectangle to the list.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt; ///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Adds shapes to the list&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; AddShapes()
{ &lt;font color=#7fffd4&gt;ShapeFactory&lt;/font&gt; shapeFactory &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;font color=#7fffd4&gt;ShapeFactory&lt;/font&gt;();
_shapes.Add(shapeFactory.CreateShape(&lt;font color=#7fffd4&gt;Shape&lt;/font&gt;.Circle)); _shapes.Add(shapeFactory.CreateShape(&lt;font color=#7fffd4&gt;Shape&lt;/font&gt;.Rectangle));
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Because we know that the shapes Implement the &lt;strong&gt;IShape&lt;/strong&gt; interface we
have a contract that says we can call the &lt;strong&gt;Draw&lt;/strong&gt;() methods on any of
these shapes. These objects will then take the responsibility for drawing them selves.
So in the &lt;strong&gt;DrawShapes&lt;/strong&gt;() method all we need to do is iterate through
all the shapes in the list and call each shapes &lt;strong&gt;Draw&lt;/strong&gt;() method.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt; ///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Draws the shapes in the list&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; DrawShapes()
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;foreach&lt;/span&gt;(IShape
shape &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;in&lt;/span&gt; _shapes)
{ shape.Draw(); } }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
And that would be it... apart from we haven't added any working code into the Draw()
methods of each concrete object yet!
&lt;/p&gt;
&lt;h3&gt;A Change In The Contract
&lt;/h3&gt;
&lt;p&gt;
Apart from the fact there is no code in the concrete objects to actually draw them
selves, I have just noticed the need to be able to pass the &lt;strong&gt;Graphics&lt;/strong&gt; object
into each shape object, so we need to go back to the interfaces and change the signature
for the &lt;strong&gt;Draw&lt;/strong&gt;() method.
&lt;/p&gt;
&lt;p&gt;
So if we take a look in the &lt;strong&gt;IShape&lt;/strong&gt; interface we need to be able to
pass in a &lt;strong&gt;Graphics&lt;/strong&gt; object as a method parameter.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; IShape
{ &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Represents the center of the shape&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; Point Center { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;get&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;set&lt;/span&gt;;} &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
The method used to draw the shape&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="graphics"&amp;gt;A reference to the Graphics object to draw the shape
on.&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Draw(Graphics
graphics); }&lt;/span&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;
&lt;/pre&gt;
&gt; 
&lt;p&gt;
And while we are in this project I think we will take the opportunity to extend the &lt;strong&gt;CreateShape&lt;/strong&gt;()
method signature in the &lt;strong&gt;IShapeFactory&lt;/strong&gt; interface, too. We are going
to add a center point parameter; this will allow us to position the shape where we
like.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; IShapeFactory
{ &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Use this method to create a shape.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="shape"&amp;gt;Indicates the type of shape to be made.&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="center"&amp;gt;Indicates the center point of the shape.&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;An object that implements the IShape interface.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;font color=#7fffd4&gt;IShape&lt;/font&gt; CreateShape(&lt;font color=#7fffd4&gt;Shape&lt;/font&gt; shape, &lt;font color=#7fffd4&gt;Point&lt;/font&gt; center);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
A quick build of the project will highlight where our change in interface has broken
the contract between it and the objects that implement it so in the &lt;strong&gt;BaseShape&lt;/strong&gt; object
we need to add a &lt;strong&gt;Graphics&lt;/strong&gt; parameter into the &lt;strong&gt;Draw&lt;/strong&gt;()
method. This will be the same in the &lt;strong&gt;Rectangle&lt;/strong&gt; and the &lt;strong&gt;Circle&lt;/strong&gt; class.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;abstract&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Draw(&lt;font color=#7fffd4&gt;Graphics&lt;/font&gt; graphics);&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
In the &lt;strong&gt;ShapeFactory&lt;/strong&gt; class we need to extend the &lt;strong&gt;CreateShape&lt;/strong&gt;(), &lt;strong&gt;CreateCircle&lt;/strong&gt;()
and &lt;strong&gt;CreateRectangle&lt;/strong&gt;() methods signatures to include the center point
parameter. Then in the &lt;strong&gt;CreateShape&lt;/strong&gt;() method we need to pass the center
point parameter on to the worker method calls.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;A shape object that implements IShape.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; IShape
CreateShape(Shape shape, Point center) {&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
In the two worker methods, CreateCircle() and CreateRectangle() we will populate the
object's Center property with the value from the center parameter passed in.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt; ///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
This method creates a circle object.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;A shape object that implements ICircle.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;ICircle&lt;/font&gt; CreateCircle(Point
center) { &lt;font color=#7fffd4&gt;Circle&lt;/font&gt; circle &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;font color=#7fffd4&gt;Circle&lt;/font&gt;();
circle.Center &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; center;
circle.Diameter &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 50; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; (&lt;font color=#7fffd4&gt;ICircle&lt;/font&gt;)circle;
} &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
This method creates a rectangle object.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;A shape object that implements IRectangle.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;IRectangle&lt;/font&gt; CreateRectangle(Point
center) { BusinessEntity.&lt;font color=#7fffd4&gt;Rectangle&lt;/font&gt; rectangle &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; BusinessEntity.&lt;font color=#7fffd4&gt;Rectangle&lt;/font&gt;();
rectangle.Center &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; center;
rectangle.Size &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Size(400,
200); &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; (&lt;font color=#7fffd4&gt;IRectangle&lt;/font&gt;)rectangle;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Then back in the form code we need to alter the calls to the &lt;strong&gt;ShapeFactory&lt;/strong&gt; &lt;strong&gt;CreateShape&lt;/strong&gt;()
methods to include a center position
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt; ///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Adds shapes to the list&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; AddShapes()
{ &lt;font color=#7fffd4&gt;ShapeFactory&lt;/font&gt; shapeFactory &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;font color=#7fffd4&gt;ShapeFactory&lt;/font&gt;();
_shapes.Add(shapeFactory.CreateShape(&lt;font color=#7fffd4&gt;Shape&lt;/font&gt;.Circle, &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;font color=#7fffd4&gt;Point&lt;/font&gt;(250,250)));
_shapes.Add(shapeFactory.CreateShape(&lt;font color=#7fffd4&gt;Shape&lt;/font&gt;.Rectangle, &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;font color=#7fffd4&gt;Point&lt;/font&gt;(350,
200))); } &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Draws the shapes in the list&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; DrawShapes()
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;foreach&lt;/span&gt;(&lt;font color=#7fffd4&gt;IShape&lt;/font&gt; shape &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;in&lt;/span&gt; _shapes)
{ shape.Draw(_graphics); } }&lt;/span&gt;&lt;/pre&gt;
&lt;h3&gt;Back to the Drawing Board
&lt;/h3&gt;
&lt;p&gt;
So finally we can get back into the concrete shape classes and write the code that
will draw the shape.
&lt;/p&gt;
&lt;p&gt;
So let us look at the Circle's &lt;strong&gt;Draw&lt;/strong&gt;() method&amp;nbsp;first. We'll add
the code to allow the Circle to draw it's self.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt; ///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Use this method to draw the shape&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;override&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Draw(Graphics
graphics) { Pen pen &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Pen(Color.DarkGray,
0); &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; left &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt;)Center.X &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;-&lt;/span&gt; (Diameter/2); &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; top &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt;)Center.Y &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;-&lt;/span&gt; (Diameter/2);
System.Drawing.Rectangle outlineRectangle &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; System.Drawing.Rectangle(left,
top, Diameter, Diameter); graphics.DrawEllipse(pen, outlineRectangle); }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
And the same for the Rectangle.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt; ///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Use this method to draw the shape&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;override&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Draw(Graphics
graphics) { Pen pen &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Pen(Color.Red,
0); &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; left &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt;)Center.X &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;-&lt;/span&gt; (Size.Width &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;/&lt;/span&gt; 2); &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; top &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; (&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt;)Center.Y &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;-&lt;/span&gt; (Size.Height &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;/&lt;/span&gt; 2);
System.Drawing.Rectangle outlineRectangle &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; System.Drawing.Rectangle(left,
top, Size.Width, Size.Height); graphics.DrawRectangle(pen, outlineRectangle); }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Now we need to make sure we have set the start up project to be the windows application
and we should be good to go!
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.duanewingett.info/content/binary/ShapeMaker_Form_01.jpg" border=0&gt;
&lt;/p&gt;
&lt;h3&gt;Conclusions
&lt;/h3&gt;
&lt;p&gt;
Now I'm certain that the application architecture and the code may not be laid out
to best practises, and I'm aware there are plenty of parts that could have been better
thought out and implemented, but really I just wanted to get a simple factory based
application down on paper, so to speak, to get it a little clearer in my mind.
&lt;/p&gt;
&lt;p&gt;
If any of these articles have helped you or planted&amp;nbsp;the seed of inquisitiveness
in your mind to find out more about factories then that can only be a good thing.
If there is anything I have done in this article that is quite clearly incorrect or
bad practise, please feel free to leave a comment. Any constructive feed back is appreciated.
&lt;/p&gt;
&lt;p&gt;
The next step will be to make the objects prinatable. But until then, have fun!
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart1.aspx"&gt;&lt;strong&gt;Part
1&lt;/strong&gt;&lt;/a&gt; | &lt;a href="http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart2.aspx"&gt;&lt;strong&gt;Part
2&lt;/strong&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;a href="http://www.duanewingett.info/content/binary/2008-04-11_ShapeMaker.zip"&gt;2008-04-11_ShapeMaker.zip
(103.25 KB)&lt;/a&gt;&amp;nbsp;&amp;lt;- Solution files all zipped up. &lt;img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=4cac64a8-2865-4fce-9d68-b79f5c9bfeeb" /&gt;</description>
      <comments>http://www.duanewingett.info/CommentView,guid,4cac64a8-2865-4fce-9d68-b79f5c9bfeeb.aspx</comments>
      <category>.Net</category>
      <category>C#</category>
      <category>Classes</category>
      <category>Factory Pattern</category>
      <category>Inheritance</category>
      <category>Interfaces</category>
    </item>
    <item>
      <trackback:ping>http://www.duanewingett.info/Trackback.aspx?guid=81f8a739-ee84-4f69-8642-e47546c4ec52</trackback:ping>
      <pingback:server>http://www.duanewingett.info/pingback.aspx</pingback:server>
      <pingback:target>http://www.duanewingett.info/PermaLink,guid,81f8a739-ee84-4f69-8642-e47546c4ec52.aspx</pingback:target>
      <dc:creator>Duane Wingett</dc:creator>
      <wfw:comment>http://www.duanewingett.info/CommentView,guid,81f8a739-ee84-4f69-8642-e47546c4ec52.aspx</wfw:comment>
      <wfw:commentRss>http://www.duanewingett.info/SyndicationService.asmx/GetEntryCommentsRss?guid=81f8a739-ee84-4f69-8642-e47546c4ec52</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Back to <a href="http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart1.aspx"><strong>Part
1</strong></a></p>
        <h3>Review Of Progress So Far
</h3>
        <p>
So far we have two concrete shape classes, <strong>Circle</strong> and <strong>Rectangle</strong> that
derive from a base shape class. The base shape class implements the <strong>IShape</strong> interface
and the concrete classes implement in turn the <strong>IShape</strong> interface as
well as their respective shape interfaces. The next step is to create a factory class
to create our shapes.
</p>
        <p align="center">
          <img src="http://www.duanewingett.info/content/binary/ShapeMaker_ClassDiagram_01.jpg" border="0" />
        </p>
        <h3>Working In The Factory
</h3>
        <p>
So, we need to add to the solution, another new class library project which
we will call <strong>Factories</strong>. This will house our <strong>ShapeFactory</strong> class. The <strong>ShapeFactory</strong> class will
be responsible for creating our shape objects. It will be able to create and
return an object that implements the <strong>IShape</strong> interface. So far we
have ensured that both our circle and rectangle inherently implement the <strong>IShape</strong> interface
for two reasons. One, because they both derive from the BaseShape object
and we know that implements <strong>IShape</strong>, and two, because they implement <strong>ICircle</strong> and <strong>IRectangle</strong> which
in turn implements <strong>IShape</strong>, as well. (<em>Maybe some one with a little
more experience than I, could advise if this is an acceptable practise. -&gt; I can't
see why not!</em>)
</p>
        <p>
But before we create our <strong>ShapeFactory</strong> we have one more thing to do.
Because we may want to use more than one factory in the future to create our shapes
we are first going to create another interface, which will describe what a <strong>ShapeFactory</strong> need
to create some shape objects. This will be the <strong>IShapeFactory</strong> interface,
and we are going to add that for the sake of simplicity to our <strong>ShapeInterfaces</strong> project
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ShapeMaker.ShapeInterfaces
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span><font color="#7fffd4">IShapeFactory</font> { <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Use this method to create a shape.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;An object that implements the IShape interface.&lt;/returns&gt;</span><font color="#7fffd4">IShape</font> CreateShape();
} }</span>
        </pre>
        <p>
As you can see we have determined that any factory that implements <strong>IShapeFactory</strong> will
have to contain a method that returns an object implementing <strong>IShape</strong>.
The problem is, how do we determine what sort of shape we want to get? Well even though
we only have two shapes at the moment for future expansion we will need an enumeration
to represent these. As enumeration is to be used in every class that implements <strong>IShapeFactory</strong> and
will need to be a parameter in the <strong>CreateShape</strong> method, it seems logical
to me to place the enumeration in the <strong>ShapeInterfaces</strong> project. If
any one can advise whether this is good practise or not, please do! So, let us add
the <strong>Shape</strong> enumeration.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ShapeMaker.ShapeInterfaces
{ <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Represents the types of shapes that can be made</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">enum</span><font color="#7fffd4">Shape</font> { <font color="#a9a9a9"><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Indicates the shape is to be a circle</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span></font> Circle, <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Indicates the shape is to be a rectangle</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span> Rectangle } }</span>
        </pre>
We can now modify the <strong>CreateShape</strong> method of the <strong>IShapeFactory</strong> interface
to accept a <strong>Shape</strong> enumerated value.<pre><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span><font color="#7fffd4">IShapeFactory</font> { <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Use this method to create a shape.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;param name="shape"&gt;Indicates the type of shape to be made.&lt;/param&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;An object that implements the IShape interface.&lt;/returns&gt;</span><font color="#7fffd4">IShape</font> CreateShape(<font color="#7fffd4">Shape</font> shape);
}</span></pre><p>
So it's back to the <strong>Factories</strong> project now to create the <strong>ShapeFactory</strong> class
that will provide us with some shapes. To create some shapes we need to add a reference
to both the <strong>BusinessEntity</strong> and the <strong>ShapeInterfaces</strong> projects
into the <strong>Factories</strong> project. After adding our new <strong>ShapeFactory</strong> class
into the <strong>Factories</strong> project we need to import the <strong>BusinessEntity</strong> and
the <strong>ShapeInterfaces</strong> namespaces into the top of it. We now need to
make the <strong>ShapeFactory</strong> class implement <strong>IShapeFactory</strong>.
</p><pre><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> Playground.ShapeMaker.BusinessEntity; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> Playground.ShapeMaker.ShapeInterfaces; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ShapeMaker.Factories
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span><font color="#7fffd4">ShapeFactory</font> :
IShapeFactory { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#region</span> IShapeFactory
Members <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><font color="#7fffd4">IShape</font> CreateShape(<font color="#7fffd4">Shape</font> shape)
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">throw</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><font color="#7fffd4">Exception</font>(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"The
method or operation is not implemented."</span>); } <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#endregion</span> }
}</span></pre><p>
We are now going to create a pair of private methods that will create our two shapes.
Although these methods will create two concrete objects the methods will return not
the objects but their interfaces. First though we will just create the method stubs.
Then we will modify the <strong>CreateShape</strong>() method to call one or the other
of these two methods depending upon the enumerated value passed in.
</p><pre><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#region</span> IShapeFactory
Members <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Creates a shape and returns it.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;param name="shape"&gt;Indicates the shape to create.&lt;/param&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;A shape object that implements IShape.&lt;/returns&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><font color="#7fffd4">IShape</font> CreateShape(<font color="#7fffd4">Shape</font> shape)
{ <font color="#7fffd4">IShape</font> createdShape <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">null</span>; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">switch</span> (shape)
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">case</span><font color="#7fffd4">Shape</font>.Circle:
createdShape <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> CreateCircle(); <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">break</span>; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">case</span><font color="#7fffd4">Shape</font>.Rectangle:
createdShape <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> CreateRectangle(); <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">break</span>; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">default</span>: <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">throw</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Exception(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"Unknown
shape encountered in CreateShape() method."</span>); } <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> createdShape;
} <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#endregion</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
This method creates a circle object.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;A shape object that implements ICircle.&lt;/returns&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">ICircle</font> CreateCircle()
{ } <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
This method creates a rectangle object.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;A shape object that implements IRectangle.&lt;/returns&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">IRectangle</font> CreateRectangle()
{ }</span></pre><p>
We have used switch / case to select which method will create our shape object. Notice
that we use the default case to throw an exception if we encounter a shape that
we haven't catered for. This is in case at any time the shape enumeration is extended
to provide more shapes. Notice also that although the <strong>CreateShape</strong>()
method returns an object that implements <strong>IShape</strong>, the two worker
methods return <strong>ICircle</strong> and <strong>IRectangle</strong>. This works
because both <strong>ICircle</strong> and <strong>IRectangle</strong> both implement
the <strong>IShape</strong> interface, and that is what <strong>CreateShape</strong>()
will return.
</p><p>
Anyway let us crack on and 'flesh out' the <strong>CreateCircle</strong>() worker
method! Because our shapes using the <strong>System.Drawing</strong> namespace, our
factory that is going to create these will need to also. So we need to add a reference
to and import that namespace for the <strong>ShapeFactory</strong>. (<em>Incidentally
have you noticed the more you look at the word "circle" the more wrong it looks!</em>)
</p><pre><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"> ///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
This method creates a circle object.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;A shape object that implements ICircle.&lt;/returns&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">ICircle</font> CreateCircle()
{ <font color="#7fffd4">Circle</font> circle <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><font color="#7fffd4">Circle</font>();
circle.Center <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><font color="#7fffd4">Point</font>(250,250);
circle.Diameter <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> 50; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> (<font color="#7fffd4">ICircle</font>)circle;
}</span></pre><p>
Now I would normally use constructor in my code, but this example isn't to show best
practise of constructing objects, so I am going to construct my objects using the
properties today. You will probably also notice that I am casting the newly constructed <strong>Circle</strong> object
to the type of <strong>ICircle</strong> before returning it. You don't need to
do this but I believe it is good practise. (<em>Can any one confirm this please?</em>)
</p><p>
At this point I have just realised an error with choosing a rectangle for a shape,
due to a conflict with the <strong>System.Drawing.Rectangle</strong> object. Well
we could change the shape to a square or a triangle.... but we have got this far,
so I am going to stick with it. It will however mean there will be some ugly code
with the BusinessEntity namespace forced in here and there!!!
</p><pre><span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"> ///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
This method creates a rectangle object.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;A shape object that implements IRectangle.&lt;/returns&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">IRectangle</font> CreateRectangle()
{ BusinessEntity.<font color="#7fffd4">Rectangle</font> rectangle <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> BusinessEntity.<font color="#7fffd4">Rectangle</font>();
rectangle.Center <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Point(300,
300); rectangle.Size <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Size(400,
200); <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> (<font color="#7fffd4">IRectangle</font>)rectangle;
}</span></pre><p>
Now that should be it for our ShapeFactory for the moment. If all has gone well for
you then your project should build without errors.
</p><p>
To sum up we have created an interface for this and any other shape factory to implement,
an enumeration that represents all the available shapes we may wish to build, and
a factory class that can build any of our two current shapes. the next step is to
create a graphical user interface that will call the factory and receive a couple
of shape objects for it's trouble.
</p><p><strong><a href="http://www.duanewingett.info/2008/04/11/ICanSeeAPatternFormingFirstDayAtTheFactoryPart3.aspx">Part
3</a></strong></p><img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=81f8a739-ee84-4f69-8642-e47546c4ec52" /></body>
      <title>I can see a pattern forming: First Day At The Factory... Part 2</title>
      <guid isPermaLink="false">http://www.duanewingett.info/PermaLink,guid,81f8a739-ee84-4f69-8642-e47546c4ec52.aspx</guid>
      <link>http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart2.aspx</link>
      <pubDate>Thu, 10 Apr 2008 18:28:13 GMT</pubDate>
      <description>&lt;p&gt;
Back to &lt;a href="http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart1.aspx"&gt;&lt;strong&gt;Part
1&lt;/strong&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;h3&gt;Review Of Progress So Far
&lt;/h3&gt;
&lt;p&gt;
So far we have two concrete shape classes, &lt;strong&gt;Circle&lt;/strong&gt; and &lt;strong&gt;Rectangle&lt;/strong&gt;&amp;nbsp;that
derive from a base shape class. The base shape class implements the &lt;strong&gt;IShape&lt;/strong&gt; interface
and the concrete classes implement in turn the &lt;strong&gt;IShape&lt;/strong&gt; interface as
well as their respective shape interfaces. The next step is to create a factory class
to create our shapes.
&lt;/p&gt;
&lt;p align=center&gt;
&lt;img src="http://www.duanewingett.info/content/binary/ShapeMaker_ClassDiagram_01.jpg" border=0&gt;
&lt;/p&gt;
&lt;h3&gt;Working In The Factory
&lt;/h3&gt;
&lt;p&gt;
So, we need&amp;nbsp;to add to the solution, another new class library project&amp;nbsp;which
we will&amp;nbsp;call &lt;strong&gt;Factories&lt;/strong&gt;. This will house our &lt;strong&gt;ShapeFactory&lt;/strong&gt; class.&amp;nbsp;The &lt;strong&gt;ShapeFactory&lt;/strong&gt; class&amp;nbsp;will
be&amp;nbsp;responsible for creating our shape objects. It will be able to create and
return an object that implements the &lt;strong&gt;IShape&lt;/strong&gt; interface. So far we
have ensured that both our circle and rectangle inherently implement the &lt;strong&gt;IShape&lt;/strong&gt; interface
for two reasons.&amp;nbsp;One,&amp;nbsp;because they both derive from the BaseShape object
and we know that implements &lt;strong&gt;IShape&lt;/strong&gt;, and two, because they implement &lt;strong&gt;ICircle&lt;/strong&gt; and &lt;strong&gt;IRectangle&lt;/strong&gt; which
in turn implements &lt;strong&gt;IShape&lt;/strong&gt;, as well. (&lt;em&gt;Maybe some one with a little
more experience than I, could advise if this is an acceptable practise. -&amp;gt; I can't
see why not!&lt;/em&gt;)
&lt;/p&gt;
&lt;p&gt;
But before we create our &lt;strong&gt;ShapeFactory&lt;/strong&gt; we have one more thing to do.
Because we may want to use more than one factory in the future to create our shapes
we are first going to create another interface, which will describe&amp;nbsp;what a&amp;nbsp;&lt;strong&gt;ShapeFactory&lt;/strong&gt; need
to create some shape objects. This will be the &lt;strong&gt;IShapeFactory&lt;/strong&gt; interface,
and we are going to add that for the sake of simplicity to our &lt;strong&gt;ShapeInterfaces&lt;/strong&gt; project
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ShapeMaker.ShapeInterfaces
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; &lt;font color=#7fffd4&gt;IShapeFactory&lt;/font&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Use this method to create a shape.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;An object that implements the IShape interface.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;font color=#7fffd4&gt;IShape&lt;/font&gt; CreateShape();
} }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
As you can see we have determined that any factory that implements &lt;strong&gt;IShapeFactory&lt;/strong&gt; will
have to contain a method that returns an object implementing &lt;strong&gt;IShape&lt;/strong&gt;.
The problem is, how do we determine what sort of shape we want to get? Well even though
we only have two shapes at the moment for future expansion we will need an enumeration
to represent these. As enumeration is to be used in every class that implements &lt;strong&gt;IShapeFactory&lt;/strong&gt; and
will need to be a parameter in the &lt;strong&gt;CreateShape&lt;/strong&gt; method, it seems logical
to me to place the enumeration in the &lt;strong&gt;ShapeInterfaces&lt;/strong&gt; project. If
any one can advise whether this is good practise or not, please do! So, let us add
the &lt;strong&gt;Shape&lt;/strong&gt; enumeration.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ShapeMaker.ShapeInterfaces
{ &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Represents the types of shapes that can be made&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;enum&lt;/span&gt; &lt;font color=#7fffd4&gt;Shape&lt;/font&gt; { &lt;font color=#a9a9a9&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Indicates the shape is to be a circle&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/font&gt; Circle, &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Indicates the shape is to be a rectangle&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; Rectangle } }&lt;/span&gt;&lt;/pre&gt;
We can now modify the &lt;strong&gt;CreateShape&lt;/strong&gt; method of the &lt;strong&gt;IShapeFactory&lt;/strong&gt; interface
to accept a &lt;strong&gt;Shape&lt;/strong&gt; enumerated value.&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; &lt;font color=#7fffd4&gt;IShapeFactory&lt;/font&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Use this method to create a shape.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="shape"&amp;gt;Indicates the type of shape to be made.&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;An object that implements the IShape interface.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;font color=#7fffd4&gt;IShape&lt;/font&gt; CreateShape(&lt;font color=#7fffd4&gt;Shape&lt;/font&gt; shape);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
So it's back to the &lt;strong&gt;Factories&lt;/strong&gt; project now to create&amp;nbsp;the &lt;strong&gt;ShapeFactory&lt;/strong&gt; class
that will provide us with some shapes. To create some shapes we need to add a&amp;nbsp;reference
to both&amp;nbsp;the &lt;strong&gt;BusinessEntity&lt;/strong&gt; and the &lt;strong&gt;ShapeInterfaces&lt;/strong&gt; projects
into the &lt;strong&gt;Factories&lt;/strong&gt; project. After adding our new &lt;strong&gt;ShapeFactory&lt;/strong&gt; class
into&amp;nbsp;the &lt;strong&gt;Factories&lt;/strong&gt; project we need to import the &lt;strong&gt;BusinessEntity&lt;/strong&gt; and
the &lt;strong&gt;ShapeInterfaces&lt;/strong&gt; namespaces into the top of it. We now need to
make the&amp;nbsp;&lt;strong&gt;ShapeFactory&lt;/strong&gt; class implement &lt;strong&gt;IShapeFactory&lt;/strong&gt;.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; Playground.ShapeMaker.BusinessEntity; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; Playground.ShapeMaker.ShapeInterfaces; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ShapeMaker.Factories
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; &lt;font color=#7fffd4&gt;ShapeFactory&lt;/font&gt; :
IShapeFactory { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#region&lt;/span&gt; IShapeFactory
Members &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;font color=#7fffd4&gt;IShape&lt;/font&gt; CreateShape(&lt;font color=#7fffd4&gt;Shape&lt;/font&gt; shape)
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;throw&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;font color=#7fffd4&gt;Exception&lt;/font&gt;(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"The
method or operation is not implemented."&lt;/span&gt;); } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#endregion&lt;/span&gt; }
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
We are now going to create a pair of private methods that will create our two shapes.
Although these methods will create two concrete objects the methods will return not
the objects but their interfaces. First though we will just create the method stubs.
Then we will modify the &lt;strong&gt;CreateShape&lt;/strong&gt;() method to call one or the other
of these two methods depending upon the enumerated value passed in.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#region&lt;/span&gt; IShapeFactory
Members &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Creates a shape and returns it.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;param name="shape"&amp;gt;Indicates the shape to create.&amp;lt;/param&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;A shape object that implements IShape.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;font color=#7fffd4&gt;IShape&lt;/font&gt; CreateShape(&lt;font color=#7fffd4&gt;Shape&lt;/font&gt; shape)
{ &lt;font color=#7fffd4&gt;IShape&lt;/font&gt; createdShape &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;null&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;switch&lt;/span&gt; (shape)
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;case&lt;/span&gt; &lt;font color=#7fffd4&gt;Shape&lt;/font&gt;.Circle:
createdShape &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; CreateCircle(); &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;break&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;case&lt;/span&gt; &lt;font color=#7fffd4&gt;Shape&lt;/font&gt;.Rectangle:
createdShape &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; CreateRectangle(); &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;break&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;default&lt;/span&gt;: &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;throw&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Exception(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"Unknown
shape encountered in CreateShape() method."&lt;/span&gt;); } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; createdShape;
} &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#endregion&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
This method creates a circle object.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;A shape object that implements ICircle.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;ICircle&lt;/font&gt; CreateCircle()
{ } &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
This method creates a rectangle object.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;A shape object that implements IRectangle.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;IRectangle&lt;/font&gt; CreateRectangle()
{ }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
We have used switch / case to select which method will create our shape object. Notice
that we use the default case to throw an exception&amp;nbsp;if we encounter a shape that
we haven't catered for. This is in case at any time the shape enumeration is extended
to provide more shapes. Notice also that although the &lt;strong&gt;CreateShape&lt;/strong&gt;()
method&amp;nbsp;returns an object that implements &lt;strong&gt;IShape&lt;/strong&gt;, the two worker
methods return &lt;strong&gt;ICircle&lt;/strong&gt; and &lt;strong&gt;IRectangle&lt;/strong&gt;. This works
because both &lt;strong&gt;ICircle&lt;/strong&gt; and &lt;strong&gt;IRectangle&lt;/strong&gt; both implement
the &lt;strong&gt;IShape&lt;/strong&gt; interface, and that is what &lt;strong&gt;CreateShape&lt;/strong&gt;()
will return.
&lt;/p&gt;
&lt;p&gt;
Anyway let us crack on and 'flesh out' the &lt;strong&gt;CreateCircle&lt;/strong&gt;() worker
method! Because our shapes using the &lt;strong&gt;System.Drawing&lt;/strong&gt; namespace, our
factory that is going to create these will need to also. So we need to add a reference
to and import that namespace for the &lt;strong&gt;ShapeFactory&lt;/strong&gt;.&amp;nbsp;(&lt;em&gt;Incidentally
have you noticed the more you look at the word "circle" the more wrong it looks!&lt;/em&gt;)
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt; ///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
This method creates a circle object.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;A shape object that implements ICircle.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;ICircle&lt;/font&gt; CreateCircle()
{ &lt;font color=#7fffd4&gt;Circle&lt;/font&gt; circle &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;font color=#7fffd4&gt;Circle&lt;/font&gt;();
circle.Center &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;font color=#7fffd4&gt;Point&lt;/font&gt;(250,250);
circle.Diameter &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; 50; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; (&lt;font color=#7fffd4&gt;ICircle&lt;/font&gt;)circle;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Now I would normally use constructor in my code, but this example isn't to show best
practise of constructing objects, so I am going to construct my objects using the
properties today. You will probably also notice that I am casting the newly constructed &lt;strong&gt;Circle&lt;/strong&gt; object
to the type of&amp;nbsp;&lt;strong&gt;ICircle&lt;/strong&gt; before returning it. You don't need to
do this but I believe it is good practise. (&lt;em&gt;Can any one confirm this please?&lt;/em&gt;)
&lt;/p&gt;
&lt;p&gt;
At this point I have just realised an error with choosing a rectangle for a shape,
due to a conflict with the &lt;strong&gt;System.Drawing.Rectangle&lt;/strong&gt; object. Well
we could change the shape to a square or a triangle.... but we have got this far,
so I am going to stick with it. It will however mean there will be some ugly code
with the BusinessEntity namespace forced in here and there!!!
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt; ///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
This method creates a rectangle object.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;A shape object that implements IRectangle.&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;IRectangle&lt;/font&gt; CreateRectangle()
{ BusinessEntity.&lt;font color=#7fffd4&gt;Rectangle&lt;/font&gt; rectangle &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; BusinessEntity.&lt;font color=#7fffd4&gt;Rectangle&lt;/font&gt;();
rectangle.Center &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Point(300,
300); rectangle.Size &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Size(400,
200); &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; (&lt;font color=#7fffd4&gt;IRectangle&lt;/font&gt;)rectangle;
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Now that should be it for our ShapeFactory for the moment. If all has gone well for
you then your project should build without errors.
&lt;/p&gt;
&lt;p&gt;
To sum up we have created an interface for this and any other shape factory to implement,
an enumeration that represents all the available shapes we may wish to build,&amp;nbsp;and
a factory class that can build any of our two current shapes. the next step is to
create a graphical user interface that will call the factory and receive a couple
of shape objects for it's trouble.
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;a href="http://www.duanewingett.info/2008/04/11/ICanSeeAPatternFormingFirstDayAtTheFactoryPart3.aspx"&gt;Part
3&lt;/a&gt;&lt;/strong&gt; 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=81f8a739-ee84-4f69-8642-e47546c4ec52" /&gt;</description>
      <comments>http://www.duanewingett.info/CommentView,guid,81f8a739-ee84-4f69-8642-e47546c4ec52.aspx</comments>
      <category>.Net</category>
      <category>C#</category>
      <category>Classes</category>
      <category>Factory Pattern</category>
      <category>Inheritance</category>
      <category>Interfaces</category>
    </item>
    <item>
      <trackback:ping>http://www.duanewingett.info/Trackback.aspx?guid=45331794-5e79-41ae-a2be-f799238f61f0</trackback:ping>
      <pingback:server>http://www.duanewingett.info/pingback.aspx</pingback:server>
      <pingback:target>http://www.duanewingett.info/PermaLink,guid,45331794-5e79-41ae-a2be-f799238f61f0.aspx</pingback:target>
      <dc:creator>Duane Wingett</dc:creator>
      <wfw:comment>http://www.duanewingett.info/CommentView,guid,45331794-5e79-41ae-a2be-f799238f61f0.aspx</wfw:comment>
      <wfw:commentRss>http://www.duanewingett.info/SyndicationService.asmx/GetEntryCommentsRss?guid=45331794-5e79-41ae-a2be-f799238f61f0</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <h3>Background
</h3>
        <p>
Recently I have been reading quite a bit about Factory Patterns and Inversion of Control
(IOC). However like most principles that are new to me I often find them very hard
to visualise at first, and even harder to put them into practise. So I am going to
try a simple application using what I believe to be a Factory Pattern. The <em>aim
of the game</em> will be to use a factory to produce shapes that will be drawn using
.Net's GDI classes in a Windows form application.
</p>
        <p>
This article assumes that the reader is already comfortable with the use of interfaces
and <a href="http://support.microsoft.com/kb/307205">inheritance</a> in classes.
</p>
        <p>
The files created in this article can be downloaded from here: <a href="http://www.duanewingett.info/content/binary/2008-04-11_ShapeMaker.zip">2008-04-11_ShapeMaker.zip
(103.25 KB)</a> 
</p>
        <h3>Interfaces
</h3>
        <p>
The first thing I'm going to do is to create a Blank Solution, using Visual Studio
2005, which I am going to call <strong>ShapeMaker</strong>. To this solution I am
going to add a <strong>New Project</strong> which will be a Visual C# <strong>Class
Library</strong> which I am going to call <strong>ShapeInterfaces</strong>.
</p>
        <p>
          <img src="http://www.duanewingett.info/content/binary/Solution_01.jpg" border="0" />
        </p>
        <p>
I'll close and delete the default <strong>Class1.cs</strong> class file before I proceed.
I shall now add a new Interface called <strong>IShape</strong> and remove the two
unused using statements. To the <strong>IShape</strong> interface I am going to add
a <strong>System.Drawing.Point</strong> property called <strong>Center</strong> and
import it's namespace after first adding a reference to the <strong>System.Drawing</strong> assembly.
I shall then add a method that is void of a return type and call it <strong>Draw()</strong>. 
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Drawing; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ShapeMaker.ShapeInterfaces
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span> IShape
{ <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Represents the center of the shape</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span> Point Center { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">get</span>; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">set</span>;} <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
The method used to draw the shape</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Draw();
} }</span>
        </pre>
        <p>
All shapes that I intend to create will implement this interface. The <strong>Center</strong> property
will define the center point of the shape, and the <strong>Draw()</strong> method
will allow any user interface to know that when it is required to draw the shape it
can call this method and the shape will be responsible for drawing it self.
</p>
        <p>
I'm now going to add two more interfaces which will both implement the <strong>IShape</strong> interface,
the <strong>ICircle</strong> which will also have a integer <strong>Diameter</strong> property...
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Drawing; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ShapeMaker.ShapeInterfaces
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span> ICircle
: IShape { <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Represents the diameter of the circle</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> Diameter
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">get</span>; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">set</span>;
} } }</span>
        </pre>
        <p>
...and the <strong>IRectangle</strong> which will have a <strong>Size</strong> property.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Drawing; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ShapeMaker.ShapeInterfaces
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span> IRectangle
: IShape { <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Represents the size of the rectangle</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span> Size Size { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">get</span>; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">set</span>;}
} }</span>
        </pre>
        <h3>Base Classes
</h3>
        <p>
I'm now going to add another class library project to the solution which will be my <strong>BusinessEntity</strong> layer,
from which I will also delete the default <strong>Class1</strong> class. To the <strong>BusinessEntity</strong> project
I am going to add a folder called <strong>BaseClasses</strong>. I would probably normally
use a separate project for my base classes but for clarity of this example I shall
place them in this folder.
</p>
        <p>
          <img src="http://www.duanewingett.info/content/binary/Solution_02.jpg" border="0" />
        </p>
        <p>
To the <strong>BaseClassses</strong> folder I am going to add a class that all my
shape entities will inherit from. I shall call this <strong>BaseShape</strong>. As
I intend for <strong>BaseShape</strong> to implement the interface <strong>IShape</strong> I
need to add a reference to the <strong>ShapeInterfaces</strong> project from the <strong>BusinessEntity</strong> project,
and then import the namespace into the <strong>BaseShape</strong> class.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> Playground.ShapeMaker.ShapeInterfaces; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ShapeMaker.BusinessEntity.BaseClasses
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">abstract</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> BaseShape
: IShape { } }</span>
        </pre>
        <p>
We have declared the class as abstract as we do not want this class to be able to
be constructed. It will just be used a s a base for other classes to derive (inherit)
from. Right clicking on the interface name and selecting <strong>Implement Interface</strong> from
the context menu...
</p>
        <p>
          <img src="http://www.duanewingett.info/content/binary/ImplementInterface.jpg" border="0" />
        </p>
        <p>
... should inject the following code into the class. 
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#region</span> IShape
Members <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> System.Drawing.Point
Center { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">get</span> { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">throw</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Exception(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"The
method or operation is not implemented."</span>); } <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">set</span> { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">throw</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Exception(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"The
method or operation is not implemented."</span>); } } <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Draw()
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">throw</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Exception(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"The
method or operation is not implemented."</span>); } <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#endregion</span></span>
        </pre>
        <p>
We will now need to add a reference to the <strong>System.Drawing</strong> assembly
and import the name space before we can build or we will receive an error. The next
task to do is to declare a private field, <strong>_center</strong>, which will hold
our center point data and then we will modify the <strong>Center</strong> property
to access that field. Then we will declare the draw method as <strong>abstract</strong> so
it must be overridden by any derived class.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span> Point
_center; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#region</span> IShape
Members <font color="#a9a9a9"><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;<font color="#a9a9a9">summary</font>&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font color="#a9a9a9">///
Gets or sets the center point of the shape.</font></span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/<font color="#a9a9a9">summary</font>&gt;</span></font><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> Point
Center { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">get</span> { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> _center;
} <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">set</span> {
_center <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> value;
} } <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font color="#a9a9a9">///
&lt;summary&gt;</font></span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font color="#a9a9a9">///
All inherited classes will contain this method.</font></span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font color="#a9a9a9">///
The method will be used to draw it's self.</font></span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"><font color="#a9a9a9">///
&lt;/summary&gt;</font></span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">abstract</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Draw(); <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#endregion</span></span>
        </pre>
        <h3>Derived classes
</h3>
        <p>
Next up is to create two shape classes that will derive from the base class. The first
shape we will create will be a <strong>Circle</strong>. We will add it to the root
of the <strong>BusinessEntity</strong> project, and make it both derive from the <strong>BaseShape</strong> class
and implement the <strong>ICircle</strong> interface. To make this class derive
correctly from the <strong>BaseShape</strong> class we will need to declare a <strong>Draw()</strong> method
and mark it as an <strong>override</strong>.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> Playground.ShapeMaker.BusinessEntity.BaseClasses; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> Playground.ShapeMaker.ShapeInterfaces; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ShapeMaker.BusinessEntity
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Circle
: BaseShape, ICircle { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#region</span> ICircle
Members <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> Diameter
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">get</span> { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">throw</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Exception(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"The
method or operation is not implemented."</span>); } <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">set</span> { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">throw</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> Exception(<span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4">"The
method or operation is not implemented."</span>); } } <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#endregion</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Use this method to draw the shape</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">override</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Draw()
{ } } }</span>
        </pre>
        <p>
We can now create a private field <strong>_diameter</strong> and set the <strong>Diameter</strong> property
to access it fully. The next shape we will add will be the <strong>Rectangle</strong>,
which we will do in much the same way, except that we will create a field to
hold the size data and access it from the <strong>Size</strong> property.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> System.Drawing; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> Playground.ShapeMaker.BusinessEntity.BaseClasses; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">using</span> Playground.ShapeMaker.ShapeInterfaces; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">namespace</span> Playground.ShapeMaker.BusinessEntity
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Rectangle
: BaseShape, IRectangle { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">Size</font> _size; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#region</span> IRectangle
Members <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Gets or sets the size of the rectangle.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><font color="#7fffd4">Size</font> Size
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">get</span> { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> _size;
} <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">set</span> {
_size <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> value;
} } <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#endregion</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Use this method to draw the shape</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">override</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Draw()
{ } } }</span>
        </pre>
        <h3>Summing up so far...
</h3>
        <p>
So far we have two concrete shape classes, <strong>Circle</strong> and <strong>Rectangle</strong> that
derive from a base shape class. The base shape class implements the <strong>IShape</strong> interface
and the concrete classes implement in turn the <strong>IShape</strong> interface as
well as their respective shape interfaces.
</p>
        <p>
          <img src="http://www.duanewingett.info/content/binary/Solution_03.jpg" border="0" />
        </p>
        <p>
          <strong>
            <a href="http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart2.aspx">Part
2</a>
          </strong> will show the next step, which is to build a factory class
which will be used to create and return our shape objects at runtime.
</p>
        <img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=45331794-5e79-41ae-a2be-f799238f61f0" />
      </body>
      <title>I can see a pattern forming: First Day At The Factory... Part 1</title>
      <guid isPermaLink="false">http://www.duanewingett.info/PermaLink,guid,45331794-5e79-41ae-a2be-f799238f61f0.aspx</guid>
      <link>http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart1.aspx</link>
      <pubDate>Thu, 10 Apr 2008 04:03:42 GMT</pubDate>
      <description>&lt;h3&gt;Background
&lt;/h3&gt;
&lt;p&gt;
Recently I have been reading quite a bit about Factory Patterns and Inversion of Control
(IOC). However like most principles that are new to me I often find them very hard
to visualise at first, and even harder to put them into practise. So I am going to
try a simple application using what I believe to be a&amp;nbsp;Factory Pattern. The &lt;em&gt;aim
of the game&lt;/em&gt; will be to use a factory to produce shapes that will be drawn using
.Net's GDI classes in a Windows form application.
&lt;/p&gt;
&lt;p&gt;
This article assumes that the reader is already comfortable with the use of interfaces
and &lt;a href="http://support.microsoft.com/kb/307205"&gt;inheritance&lt;/a&gt; in classes.
&lt;/p&gt;
&lt;p&gt;
The files created in this article can be downloaded from here: &lt;a href="http://www.duanewingett.info/content/binary/2008-04-11_ShapeMaker.zip"&gt;2008-04-11_ShapeMaker.zip
(103.25 KB)&lt;/a&gt;&amp;nbsp;
&lt;/p&gt;
&lt;h3&gt;Interfaces
&lt;/h3&gt;
&lt;p&gt;
The first thing I'm going to do is to create a Blank Solution, using Visual Studio
2005, which I am going to call &lt;strong&gt;ShapeMaker&lt;/strong&gt;. To this solution I am
going to add a &lt;strong&gt;New Project&lt;/strong&gt; which will be a Visual C# &lt;strong&gt;Class
Library&lt;/strong&gt; which I am going to call &lt;strong&gt;ShapeInterfaces&lt;/strong&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.duanewingett.info/content/binary/Solution_01.jpg" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
I'll close and delete the default &lt;strong&gt;Class1.cs&lt;/strong&gt; class file before I proceed.
I shall now add a new Interface called &lt;strong&gt;IShape&lt;/strong&gt; and remove the two
unused using statements. To the &lt;strong&gt;IShape&lt;/strong&gt; interface I am going to add
a &lt;strong&gt;System.Drawing.Point&lt;/strong&gt; property called &lt;strong&gt;Center&lt;/strong&gt;&amp;nbsp;and
import it's namespace after first adding a reference to the &lt;strong&gt;System.Drawing&lt;/strong&gt; assembly.
I shall then add a method that is void of&amp;nbsp;a return type and call it&amp;nbsp;&lt;strong&gt;Draw()&lt;/strong&gt;. 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Drawing; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ShapeMaker.ShapeInterfaces
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; IShape
{ &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Represents the center of the shape&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; Point Center { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;get&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;set&lt;/span&gt;;} &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
The method used to draw the shape&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Draw();
} }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
All shapes that I intend to create will implement this interface. The &lt;strong&gt;Center&lt;/strong&gt; property
will define the center point of the shape, and the &lt;strong&gt;Draw()&lt;/strong&gt; method
will allow any user interface to know that when it is required to draw the shape it
can call this method and the shape&amp;nbsp;will&amp;nbsp;be responsible for drawing it self.
&lt;/p&gt;
&lt;p&gt;
I'm now going to add two more interfaces which will both implement the &lt;strong&gt;IShape&lt;/strong&gt; interface,
the &lt;strong&gt;ICircle&lt;/strong&gt; which will&amp;nbsp;also have a integer &lt;strong&gt;Diameter&lt;/strong&gt; property...
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Drawing; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ShapeMaker.ShapeInterfaces
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; ICircle
: IShape { &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Represents the diameter of the circle&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; Diameter
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;get&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;set&lt;/span&gt;;
} } }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
...and the &lt;strong&gt;IRectangle&lt;/strong&gt; which will have a &lt;strong&gt;Size&lt;/strong&gt; property.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Drawing; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ShapeMaker.ShapeInterfaces
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; IRectangle
: IShape { &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Represents the size of the rectangle&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; Size Size { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;get&lt;/span&gt;; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;set&lt;/span&gt;;}
} }&lt;/span&gt;&lt;/pre&gt;
&lt;h3&gt;Base Classes
&lt;/h3&gt;
&lt;p&gt;
I'm now going to add another class library project to the solution which will be my &lt;strong&gt;BusinessEntity&lt;/strong&gt; layer,
from which I will also delete the default &lt;strong&gt;Class1&lt;/strong&gt; class. To the &lt;strong&gt;BusinessEntity&lt;/strong&gt; project
I am going to add a folder called &lt;strong&gt;BaseClasses&lt;/strong&gt;. I would probably normally
use a separate project for my base classes but for clarity of this example I shall
place them in this folder.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.duanewingett.info/content/binary/Solution_02.jpg" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
To the &lt;strong&gt;BaseClassses&lt;/strong&gt; folder I am going to add a class that all my
shape entities will inherit from. I shall call this &lt;strong&gt;BaseShape&lt;/strong&gt;. As
I intend for &lt;strong&gt;BaseShape&lt;/strong&gt;&amp;nbsp;to implement the interface &lt;strong&gt;IShape&lt;/strong&gt; I
need to add a reference to the &lt;strong&gt;ShapeInterfaces&lt;/strong&gt; project from the &lt;strong&gt;BusinessEntity&lt;/strong&gt; project,
and then import the namespace into the &lt;strong&gt;BaseShape&lt;/strong&gt; class.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; Playground.ShapeMaker.ShapeInterfaces; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ShapeMaker.BusinessEntity.BaseClasses
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;abstract&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; BaseShape
: IShape { } }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
We have declared the class as abstract as we do not want this class to be able to
be constructed. It will just be used a s a base for other classes to derive (inherit)
from. Right clicking on the interface name and selecting &lt;strong&gt;Implement Interface&lt;/strong&gt; from
the context menu...
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.duanewingett.info/content/binary/ImplementInterface.jpg" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
...&amp;nbsp;should inject the following code into the class. 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#region&lt;/span&gt; IShape
Members &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; System.Drawing.Point
Center { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;get&lt;/span&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;throw&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Exception(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"The
method or operation is not implemented."&lt;/span&gt;); } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;set&lt;/span&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;throw&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Exception(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"The
method or operation is not implemented."&lt;/span&gt;); } } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Draw()
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;throw&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Exception(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"The
method or operation is not implemented."&lt;/span&gt;); } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#endregion&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
We will now need to add a reference to the &lt;strong&gt;System.Drawing&lt;/strong&gt; assembly
and import the name space before we can build or we will receive an error. The next
task to do is to declare a private field, &lt;strong&gt;_center&lt;/strong&gt;, which will hold
our center point data and then we will modify the &lt;strong&gt;Center&lt;/strong&gt; property
to access that field. Then we will declare the draw method as &lt;strong&gt;abstract&lt;/strong&gt; so
it must be overridden by any derived class.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; Point
_center; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#region&lt;/span&gt; IShape
Members &lt;font color=#a9a9a9&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;&lt;font color=#a9a9a9&gt;summary&lt;/font&gt;&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;font color=#a9a9a9&gt;///
Gets or sets the center point of the shape.&lt;/font&gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/&lt;font color=#a9a9a9&gt;summary&lt;/font&gt;&amp;gt;&lt;/span&gt;&lt;/font&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; Point
Center { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;get&lt;/span&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; _center;
} &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;set&lt;/span&gt; {
_center &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; value;
} } &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;font color=#a9a9a9&gt;///
&amp;lt;summary&amp;gt;&lt;/font&gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;font color=#a9a9a9&gt;///
All inherited classes will contain this method.&lt;/font&gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;font color=#a9a9a9&gt;///
The method will be used to draw it's self.&lt;/font&gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;font color=#a9a9a9&gt;///
&amp;lt;/summary&amp;gt;&lt;/font&gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;abstract&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Draw(); &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#endregion&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;h3&gt;Derived classes
&lt;/h3&gt;
&lt;p&gt;
Next up is to create two shape classes that will derive from the base class. The first
shape we will create will be a &lt;strong&gt;Circle&lt;/strong&gt;. We will add it to the root
of the &lt;strong&gt;BusinessEntity&lt;/strong&gt; project, and make it both derive from the &lt;strong&gt;BaseShape&lt;/strong&gt; class
and implement the &lt;strong&gt;ICircle&lt;/strong&gt; interface. To make this class&amp;nbsp;derive
correctly from the &lt;strong&gt;BaseShape&lt;/strong&gt; class we will need to declare a &lt;strong&gt;Draw()&lt;/strong&gt; method
and mark it as an &lt;strong&gt;override&lt;/strong&gt;.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; Playground.ShapeMaker.BusinessEntity.BaseClasses; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; Playground.ShapeMaker.ShapeInterfaces; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ShapeMaker.BusinessEntity
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Circle
: BaseShape, ICircle { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#region&lt;/span&gt; ICircle
Members &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; Diameter
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;get&lt;/span&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;throw&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Exception(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"The
method or operation is not implemented."&lt;/span&gt;); } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;set&lt;/span&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;throw&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; Exception(&lt;span style="FONT-SIZE: 11px; COLOR: #666666; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e4e4e4"&gt;"The
method or operation is not implemented."&lt;/span&gt;); } } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#endregion&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Use this method to draw the shape&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;override&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Draw()
{ } } }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
We&amp;nbsp;can now create a private field &lt;strong&gt;_diameter&lt;/strong&gt; and set the &lt;strong&gt;Diameter&lt;/strong&gt; property
to access it fully. The next&amp;nbsp;shape we will add will be the &lt;strong&gt;Rectangle&lt;/strong&gt;,
which we will do in much the same way, except that we will create a&amp;nbsp;field to
hold the size data and access it from the &lt;strong&gt;Size&lt;/strong&gt; property.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; System.Drawing; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; Playground.ShapeMaker.BusinessEntity.BaseClasses; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;using&lt;/span&gt; Playground.ShapeMaker.ShapeInterfaces; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;namespace&lt;/span&gt; Playground.ShapeMaker.BusinessEntity
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Rectangle
: BaseShape, IRectangle { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;Size&lt;/font&gt; _size; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#region&lt;/span&gt; IRectangle
Members &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Gets or sets the size of the rectangle.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;font color=#7fffd4&gt;Size&lt;/font&gt; Size
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;get&lt;/span&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; _size;
} &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;set&lt;/span&gt; {
_size &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; value;
} } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#endregion&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Use this method to draw the shape&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;override&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Draw()
{ } } }&lt;/span&gt;&lt;/pre&gt;
&lt;h3&gt;Summing up so far...
&lt;/h3&gt;
&lt;p&gt;
So far we have two concrete shape classes, &lt;strong&gt;Circle&lt;/strong&gt; and &lt;strong&gt;Rectangle&lt;/strong&gt;&amp;nbsp;that
derive from a base shape class. The base shape class implements the &lt;strong&gt;IShape&lt;/strong&gt; interface
and the concrete classes implement in turn the &lt;strong&gt;IShape&lt;/strong&gt; interface as
well as their respective shape interfaces.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://www.duanewingett.info/content/binary/Solution_03.jpg" border=0&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;&lt;a href="http://www.duanewingett.info/2008/04/10/ICanSeeAPatternFormingFirstDayAtTheFactoryPart2.aspx"&gt;Part
2&lt;/a&gt;&lt;/strong&gt; will show the next step, which&amp;nbsp;is to&amp;nbsp;build a factory class
which will be used to create and return&amp;nbsp;our shape objects at runtime.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=45331794-5e79-41ae-a2be-f799238f61f0" /&gt;</description>
      <comments>http://www.duanewingett.info/CommentView,guid,45331794-5e79-41ae-a2be-f799238f61f0.aspx</comments>
      <category>.Net</category>
      <category>C#</category>
      <category>Classes</category>
      <category>Factory Pattern</category>
      <category>Inheritance</category>
      <category>Interfaces</category>
    </item>
    <item>
      <trackback:ping>http://www.duanewingett.info/Trackback.aspx?guid=e52ed85e-a815-451e-862d-710c6113e52a</trackback:ping>
      <pingback:server>http://www.duanewingett.info/pingback.aspx</pingback:server>
      <pingback:target>http://www.duanewingett.info/PermaLink,guid,e52ed85e-a815-451e-862d-710c6113e52a.aspx</pingback:target>
      <dc:creator>Duane Wingett</dc:creator>
      <wfw:comment>http://www.duanewingett.info/CommentView,guid,e52ed85e-a815-451e-862d-710c6113e52a.aspx</wfw:comment>
      <wfw:commentRss>http://www.duanewingett.info/SyndicationService.asmx/GetEntryCommentsRss?guid=e52ed85e-a815-451e-862d-710c6113e52a</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I'm sure most of us endeaver to keep the tightest scope possible for the members of
classes we write. But I have a situation where I just can't seem to be able to keep
the scope of members of my inherited class as tight as I would like.
</p>
        <p>
What I am intending to create is a <strong>Job</strong> object that will inherit from
a <strong>BaseJob</strong> object that will implement the <strong>IJob</strong> interface.
While doing this I want to ensure the <strong>JobFactory</strong> member of the <strong>Job</strong> object
is kept private and the <strong>Id</strong> member is public. Both of these members
belong in the <strong>BaseJob</strong> object.
</p>
        <p>
So I'll start with new assembly that will contain three interfaces; <strong>IJob,
IIdentifyable</strong> and <strong>IJobFactory</strong>. 
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span>
            <font color="#7fffd4">IJob</font> : <font color="#7fffd4">IIdentifyable</font> { <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Gets or sets a reference to the factory that created the IJob object.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><font color="#7fffd4">IJobFactory</font> JobFactory { <font color="#0000ff">get</font>; <font color="#0000ff">set</font>;
} }</span>
        </pre>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
          </span>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
              <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
                <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span>
                <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Represents the signature for an object that is identifyable<br /></span>
              </span>
            </span>
          </span>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
              <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
                <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span> public</span>
              <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span>
              <font color="#7fffd4">IIdentifyable</font> { <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Gets or sets a reference to the identity of the IJob object.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><font color="#0000ff">int</font> Id { <font color="#0000ff">get</font>; <font color="#0000ff">set</font>;
} }</span>
          </span>
        </pre>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            </span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Represents the signature required for a job factory.</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span>
            <font color="#7fffd4">IJobFactory</font> { <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Creates an object that implements the IJob interface.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;returns&gt;Returns an object that implements the IJob interface&lt;/returns&gt;</span><font color="#7fffd4">IJob</font> Create(); <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Saves the current job</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">void</span> Save(<font color="#7fffd4">IJob</font> job);
}</span>
        </pre>
        <p>
          <strong>IJob</strong> has a member which will be used to hold a reference to an
object (that implements the <strong>IJobFactory</strong> inteface) that created it.
It will also have a Id member implemented through the <strong>IIdentifyable</strong> interface. <strong>IJobFactory</strong> has
a method for returning a newly created object that implements the <strong>IJob</strong> interface
and a method to save an object that implements the <strong>IJob</strong> interface.
Both interfaces must be <font color="#0000ff">public</font> as they need to be seen from
outside the assembly. As far as I am aware you canot declare the scope of the methods
in an interface so each will be of the same scope as the interface.
</p>
        <p>
I now need to another new assembly which will contain a base class (<strong>BaseJob</strong>)
for my <strong>Job</strong> object to inherit from. I create a reference to the interfaces
assembly, and allow the <strong>BaseJob</strong> class will implement the <strong>IJob</strong> interface
from it.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
The base class that all Job objects should inherit from.</span>
            <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">abstract</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> BaseJob
: IJob { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> _id; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span><font color="#7fffd4">IJobFactory</font> _jobFactory; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#region</span> IIdentifyable
Members <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Represents the identity of an Job</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> Id
{ <font color="#0000ff">get</font> { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> _id
; } <font color="#0000ff">set</font> { _id <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> value;
} } <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#endregion</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#region</span> IJob
Members <span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;summary&gt;</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
Gets or sets a reference to the factory that created the IJob object.</span><span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">///
&lt;/summary&gt;</span><font color="#0000ff">public</font><font color="#7fffd4">IJobFactory</font> JobFactory
{ <font color="#0000ff">get</font> { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span> _jobFactory;
} <font color="#0000ff">set</font> { _jobFactory <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> value;
} } <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">#endregion</span> }</span>
        </pre>
        <p>
Now, I would like to set the scope of the <strong>BaseJob</strong>'s <strong>Id</strong> and <strong>Jobfactory</strong> members
as <font color="#0000ff">protected</font>, do they can only be seen by objects that
inherit from the <strong>BaseJob</strong> object. However if I try and make the
scope any tighter then VS advises me of an error when I try to build.
</p>
        <p>
          <font face="Courier New" color="#ff0000">Error 1 'BaseJob' does not implement
interface member 'IJob.JobFactory'. 'BaseJob.JobFactory' is either static, not public,
or has the wrong return type.</font>
        </p>
        <p>
So as far as I can see it has to stay <font color="#0000ff">public</font>. Any way
on to my <strong>Job</strong> object now. In a third assembly, which has a reference
to both the base class assembly and the interfaces assembly,  I create the <strong>Job</strong> class.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Job
: BaseJob { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> Job(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> id, <font color="#7fffd4">IJobFactory</font> jobFactory)
: <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">base</span>()
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">base</span>.Id <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> id; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">base</span>.JobFactory <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> jobFactory;
} }</span>
        </pre>
        <p>
The <strong>Job</strong> class has a contructor which allows me to pass in the identity
and the JobFactory which is used to create it ( I hope to write an post / question
on dependency injection next!). My problem is I don't intend the <strong>JobFactory</strong> property
of the <strong>Job</strong> object to be visiable outside the class code, so ideally
I would like to scope it as <font color="#0000ff">private</font>. However it is an
inherited member that already has a scope of <font color="#0000ff">public</font>.
I can quite clearly see the member like so...
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> TestClass
{ <font color="#7fffd4">Job</font> _job; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> TestClass()
{ _job <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span><font color="#7fffd4">Job</font>();
_job.JobFactory <span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">=</span> ...;
} }</span>
        </pre>
        <p>
The only way I can see to hide the member is by overriding the <font color="#7fffd4">JobFactory</font> member
to the <font color="#7fffd4">Job</font> object with the <font color="#0000ff">new</font> modifier
like so.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">private</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">new</span> IJobFactory
JobFactory { <font color="#0000ff">get</font> { <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">return</span><span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">base</span>.JobFactory;
} }</span>
        </pre>
        <p>
Is this really the best way to go about this, or am I doing something fundementally
wrong? Your comments appreciated!
</p>
        <p>
 
</p>
        <img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=e52ed85e-a815-451e-862d-710c6113e52a" />
      </body>
      <title>Interfaces, Inheritance and Scope - Am I being some kind of fool? It is April, afterall.</title>
      <guid isPermaLink="false">http://www.duanewingett.info/PermaLink,guid,e52ed85e-a815-451e-862d-710c6113e52a.aspx</guid>
      <link>http://www.duanewingett.info/2008/04/01/InterfacesInheritanceAndScopeAmIBeingSomeKindOfFoolItIsAprilAfterall.aspx</link>
      <pubDate>Tue, 01 Apr 2008 20:35:00 GMT</pubDate>
      <description>&lt;p&gt;
I'm sure most of us endeaver to keep the tightest scope possible for the members of
classes we write. But I have a situation where I just can't seem to be able to keep
the scope of members of my inherited class as tight as I would like.
&lt;/p&gt;
&lt;p&gt;
What I am intending to create is a &lt;strong&gt;Job&lt;/strong&gt; object that will inherit from
a &lt;strong&gt;BaseJob&lt;/strong&gt; object that will implement the &lt;strong&gt;IJob&lt;/strong&gt; interface.
While doing this I want to ensure the &lt;strong&gt;JobFactory&lt;/strong&gt; member of the &lt;strong&gt;Job&lt;/strong&gt; object
is kept private and the &lt;strong&gt;Id&lt;/strong&gt; member is public. Both of these members
belong in the &lt;strong&gt;BaseJob&lt;/strong&gt; object.
&lt;/p&gt;
&lt;p&gt;
So I'll start with new assembly that will contain&amp;nbsp;three interfaces; &lt;strong&gt;IJob,
IIdentifyable&lt;/strong&gt;&amp;nbsp;and &lt;strong&gt;IJobFactory&lt;/strong&gt;. 
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; &lt;font color=#7fffd4&gt;IJob&lt;/font&gt; : &lt;font color=#7fffd4&gt;IIdentifyable&lt;/font&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Gets or sets a reference to the factory that created the IJob object.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;font color=#7fffd4&gt;IJobFactory&lt;/font&gt; JobFactory { &lt;font color=#0000ff&gt;get&lt;/font&gt;; &lt;font color=#0000ff&gt;set&lt;/font&gt;;
} }&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Represents the signature for an object that is identifyable&lt;br&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; &lt;font color=#7fffd4&gt;IIdentifyable&lt;/font&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Gets or sets a reference to the identity of the IJob object.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;font color=#0000ff&gt;int&lt;/font&gt; Id { &lt;font color=#0000ff&gt;get&lt;/font&gt;; &lt;font color=#0000ff&gt;set&lt;/font&gt;;
} }&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Represents the signature required for a job factory.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; &lt;font color=#7fffd4&gt;IJobFactory&lt;/font&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Creates an object that implements the IJob interface.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;returns&amp;gt;Returns an object that implements the IJob interface&amp;lt;/returns&amp;gt;&lt;/span&gt; &lt;font color=#7fffd4&gt;IJob&lt;/font&gt; Create(); &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Saves the current job&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;void&lt;/span&gt; Save(&lt;font color=#7fffd4&gt;IJob&lt;/font&gt; job);
}&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;strong&gt;IJob&lt;/strong&gt; has a member which will be used to hold a reference to&amp;nbsp;an
object (that implements the &lt;strong&gt;IJobFactory&lt;/strong&gt; inteface) that created&amp;nbsp;it.
It will also have a Id member implemented through the &lt;strong&gt;IIdentifyable&lt;/strong&gt; interface.&amp;nbsp;&lt;strong&gt;IJobFactory&lt;/strong&gt; has
a method for returning a newly created object that implements the&amp;nbsp;&lt;strong&gt;IJob&lt;/strong&gt; interface
and a method to save an object that implements the &lt;strong&gt;IJob&lt;/strong&gt; interface.
Both interfaces must be &lt;font color=#0000ff&gt;public&lt;/font&gt; as they need to be seen&amp;nbsp;from
outside the assembly. As far as I am aware you canot declare the scope of the methods
in an interface so each will be of the same scope as the interface.
&lt;/p&gt;
&lt;p&gt;
I now need to another new assembly which will contain a base class (&lt;strong&gt;BaseJob&lt;/strong&gt;)
for my &lt;strong&gt;Job&lt;/strong&gt; object to inherit from. I create a reference to the interfaces
assembly, and allow the &lt;strong&gt;BaseJob&lt;/strong&gt; class will implement the &lt;strong&gt;IJob&lt;/strong&gt; interface
from it.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
The base class that all Job objects should inherit from.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;abstract&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; BaseJob
: IJob { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; _id; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;font color=#7fffd4&gt;IJobFactory&lt;/font&gt; _jobFactory; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#region&lt;/span&gt; IIdentifyable
Members &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Represents the identity of an Job&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; Id
{ &lt;font color=#0000ff&gt;get&lt;/font&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; _id
; } &lt;font color=#0000ff&gt;set&lt;/font&gt; { _id &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; value;
} } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#endregion&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#region&lt;/span&gt; IJob
Members &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;summary&amp;gt;&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
Gets or sets a reference to the factory that created the IJob object.&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: green; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;///
&amp;lt;/summary&amp;gt;&lt;/span&gt; &lt;font color=#0000ff&gt;public&lt;/font&gt; &lt;font color=#7fffd4&gt;IJobFactory&lt;/font&gt; JobFactory
{ &lt;font color=#0000ff&gt;get&lt;/font&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; _jobFactory;
} &lt;font color=#0000ff&gt;set&lt;/font&gt; { _jobFactory &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; value;
} } &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;#endregion&lt;/span&gt; }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Now, I would like to set the scope of the &lt;strong&gt;BaseJob&lt;/strong&gt;'s&amp;nbsp;&lt;strong&gt;Id&lt;/strong&gt; and &lt;strong&gt;Jobfactory&lt;/strong&gt; members
as &lt;font color=#0000ff&gt;protected&lt;/font&gt;, do they can only be seen by objects that
inherit from the &lt;strong&gt;BaseJob&lt;/strong&gt; object. However if I try and&amp;nbsp;make&amp;nbsp;the
scope any tighter then VS advises me of an error when I try to build.
&lt;/p&gt;
&lt;p&gt;
&lt;font face="Courier New" color=#ff0000&gt;Error&amp;nbsp;1&amp;nbsp;'BaseJob' does not implement
interface member 'IJob.JobFactory'. 'BaseJob.JobFactory' is either static, not public,
or has the wrong return type.&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
So as far as I can see it has to stay &lt;font color=#0000ff&gt;public&lt;/font&gt;. Any way on
to my &lt;strong&gt;Job&lt;/strong&gt; object now. In a third assembly, which has a reference
to both the base class assembly and the interfaces assembly,&amp;nbsp;&amp;nbsp;I create the &lt;strong&gt;Job&lt;/strong&gt; class.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Job
: BaseJob { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; Job(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; id, &lt;font color=#7fffd4&gt;IJobFactory&lt;/font&gt; jobFactory)
: &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;base&lt;/span&gt;()
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;base&lt;/span&gt;.Id &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; id; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;base&lt;/span&gt;.JobFactory &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; jobFactory;
} }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
The &lt;strong&gt;Job&lt;/strong&gt; class has a contructor which allows me to pass in the identity
and the JobFactory which is used to create it ( I hope to write an post / question
on dependency injection next!). My problem is I don't intend the &lt;strong&gt;JobFactory&lt;/strong&gt; property
of the &lt;strong&gt;Job&lt;/strong&gt; object to be visiable outside the class code, so ideally
I would like to scope it as &lt;font color=#0000ff&gt;private&lt;/font&gt;. However it is an inherited
member that already has a scope of &lt;font color=#0000ff&gt;public&lt;/font&gt;. I can quite
clearly see the member like so...
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; TestClass
{ &lt;font color=#7fffd4&gt;Job&lt;/font&gt; _job; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; TestClass()
{ _job &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; &lt;font color=#7fffd4&gt;Job&lt;/font&gt;();
_job.JobFactory &lt;span style="FONT-SIZE: 11px; COLOR: red; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;=&lt;/span&gt; ...;
} }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
The only way I can see to hide the member is by&amp;nbsp;overriding the&amp;nbsp;&lt;font color=#7fffd4&gt;JobFactory&lt;/font&gt; member
to the &lt;font color=#7fffd4&gt;Job&lt;/font&gt; object with the &lt;font color=#0000ff&gt;new&lt;/font&gt; modifier
like so.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;private&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;new&lt;/span&gt; IJobFactory
JobFactory { &lt;font color=#0000ff&gt;get&lt;/font&gt; { &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;return&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;base&lt;/span&gt;.JobFactory;
} }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
Is this really the best way to go about this, or am I doing something fundementally
wrong? Your comments appreciated!
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=e52ed85e-a815-451e-862d-710c6113e52a" /&gt;</description>
      <comments>http://www.duanewingett.info/CommentView,guid,e52ed85e-a815-451e-862d-710c6113e52a.aspx</comments>
      <category>.Net</category>
      <category>C#</category>
      <category>Inheritance</category>
      <category>Interfaces</category>
      <category>Scope</category>
    </item>
    <item>
      <trackback:ping>http://www.duanewingett.info/Trackback.aspx?guid=eeefddc3-6c61-4adb-aad3-d43f78c94121</trackback:ping>
      <pingback:server>http://www.duanewingett.info/pingback.aspx</pingback:server>
      <pingback:target>http://www.duanewingett.info/PermaLink,guid,eeefddc3-6c61-4adb-aad3-d43f78c94121.aspx</pingback:target>
      <dc:creator>Duane Wingett</dc:creator>
      <wfw:comment>http://www.duanewingett.info/CommentView,guid,eeefddc3-6c61-4adb-aad3-d43f78c94121.aspx</wfw:comment>
      <wfw:commentRss>http://www.duanewingett.info/SyndicationService.asmx/GetEntryCommentsRss?guid=eeefddc3-6c61-4adb-aad3-d43f78c94121</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <h2>Background
</h2>
        <p>
As part of an ongoing project where a light weight client application needs to communicate
with a database via a webservice I have been trying to get my head around a the best
way to encapsulate data within custom objects and pass it from the client 's GUI to
the Web Service's Data Access layer without replicating too much code or having the
the client hold a reference to the same assembly as the webservice.
</p>
        <p>
The intention is for the client application to have 3 layers, the presentation layer,
a Business Entity / Business Logic layer and an Webservice Adapter layer. The Webservice
will have the WebService layer, it's own Business Entity / Business Logic layer and
a Data Access layer. 
</p>
        <h2>Initial Thoughts
</h2>
        <p>
My initial thoughts were to create an assembly which would hold a set of interfaces
that will describe the business entities that both parts of the application would
use. For example the interface fot the job object would be:
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">interface</span> IJob
{ <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> Id; <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">string</span> CreatedBy; <font color="#7fffd4">DateTime</font> CreatedOn;
. . <font color="#7fffd4">DateTime</font> LastAccessed; }</span>
        </pre>
        <p>
This would require that both pairs of business entity assemblies would need to have
a reference to the assembly. My plan was then to return from the webservice's webmethods
the interface rather than the object. For example.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">[WebMethod] <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span> IJob
GetJobById(<span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">int</span> id)
{ . . }</span>
        </pre>
        <p>
          <font size="1">
            <font size="2">My plan was then bind the form controls to the properties
of the interface but among a couple of minor flaws in my plan, there was <strong>one
big</strong> one... It appears that you <em>can not</em> return an interface from
a web service. I assume do to it not being serialisable. A little research later
it appears that a better way would be to would be to use a shared base class for the
business entities on both sides of the void, so to speak. So back to the drawing board
slightly..</font>
          </font>
        </p>
        <h2>Second Thoughts
</h2>
        <p>
So now I'm thinking I need to create an abstract base class for the objects
in the business entitiy assemblies in both the client and the webservice to inherit
from. If I ensure the base classes implement the interfaces, then I can still bind
my controls to the interfaces properties. So ignoring the various properties of the
class I now have a bas class that implements the previosu
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">protected</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">abstract</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> BaseJob
: IJob { . . }</span>
        </pre>
        <p>
So now the Job class in each business entity layer will inherit from the BaseJob abstract
class which implements the IJob interface.
</p>
        <pre>
          <span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">public</span>
            <span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent">class</span> Job
: BaseJob { . . }</span>
        </pre>
        <p>
          <font size="1">
            <font size="2">So this means that now rather than just having a reference
to the assembly with the interfaces in for each business entity assembly I now have
a reference to the base class assembly too. Is this really the way I want to go?</font>
          </font>
        </p>
        <p>
          <font size="1">To be continued...</font>
        </p>
        <img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=eeefddc3-6c61-4adb-aad3-d43f78c94121" />
      </body>
      <title>Same objects on opposite sides of the void...</title>
      <guid isPermaLink="false">http://www.duanewingett.info/PermaLink,guid,eeefddc3-6c61-4adb-aad3-d43f78c94121.aspx</guid>
      <link>http://www.duanewingett.info/2008/03/31/SameObjectsOnOppositeSidesOfTheVoid.aspx</link>
      <pubDate>Mon, 31 Mar 2008 15:30:16 GMT</pubDate>
      <description>&lt;h2&gt;Background
&lt;/h2&gt;
&lt;p&gt;
As part of an ongoing project where a light weight client application needs to communicate
with a database via a webservice I have been trying to get my head around a the best
way to encapsulate data within custom objects and pass it from the client 's GUI to
the Web Service's Data Access layer without replicating too much code or having the
the client hold a reference to the same assembly as the webservice.
&lt;/p&gt;
&lt;p&gt;
The intention is for the client application to have 3 layers, the presentation layer,
a Business Entity / Business Logic layer and an Webservice Adapter layer. The Webservice
will have the WebService layer, it's own Business Entity / Business Logic layer and
a Data Access layer. 
&lt;/p&gt;
&lt;h2&gt;Initial Thoughts
&lt;/h2&gt;
&lt;p&gt;
My initial thoughts were to create an assembly which would hold a set of interfaces
that will describe the business entities that both parts of the application would
use. For example the interface fot the job object would be:
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;interface&lt;/span&gt; IJob
{ &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; Id; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;string&lt;/span&gt; CreatedBy; &lt;font color=#7fffd4&gt;DateTime&lt;/font&gt; CreatedOn;
. . &lt;font color=#7fffd4&gt;DateTime&lt;/font&gt; LastAccessed; }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
This would require that both pairs of business entity assemblies would need to have
a reference to the assembly. My plan was then to return from the webservice's webmethods
the interface rather than the object. For example.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;[WebMethod] &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; IJob
GetJobById(&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;int&lt;/span&gt; id)
{ . . }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;font size=1&gt;&lt;font size=2&gt;My plan was then bind the form controls to the properties
of the interface but among a couple of minor flaws in my plan, there was &lt;strong&gt;one
big&lt;/strong&gt; one... It appears that you &lt;em&gt;can not&lt;/em&gt; return an interface from
a web service. I assume do to it not being serialisable.&amp;nbsp;A little research later
it appears that a better way would be to would be to use a shared base class for the
business entities on both sides of the void, so to speak. So back to the drawing board
slightly..&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;h2&gt;Second Thoughts
&lt;/h2&gt;
&lt;p&gt;
So now I'm thinking I&amp;nbsp;need to create an abstract&amp;nbsp;base class for the objects
in the business entitiy assemblies&amp;nbsp;in both the client and the webservice to inherit
from. If I ensure the base classes implement the interfaces, then I&amp;nbsp;can still&amp;nbsp;bind
my controls to the interfaces properties. So ignoring the various properties of the
class I now have a bas class that implements the previosu
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;protected&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;abstract&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; BaseJob
: IJob { . . }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
So now the Job class in each business entity layer will inherit from the BaseJob abstract
class which implements the IJob interface.
&lt;/p&gt;
&lt;pre&gt;&lt;span style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;&lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;public&lt;/span&gt; &lt;span style="FONT-SIZE: 11px; COLOR: blue; FONT-FAMILY: Courier New; BACKGROUND-COLOR: transparent"&gt;class&lt;/span&gt; Job
: BaseJob { . . }&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;font size=1&gt;&lt;font size=2&gt;So this means that now rather than just having a reference
to the assembly with the interfaces in for each business entity assembly I now have
a reference to the base class assembly too. Is this really the way I want to go?&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font size=1&gt;To be continued...&lt;/font&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.duanewingett.info/aggbug.ashx?id=eeefddc3-6c61-4adb-aad3-d43f78c94121" /&gt;</description>
      <comments>http://www.duanewingett.info/CommentView,guid,eeefddc3-6c61-4adb-aad3-d43f78c94121.aspx</comments>
      <category>.Net</category>
      <category>C#</category>
      <category>Inheritance</category>
      <category>Interfaces</category>
      <category>Web Services</category>
    </item>
  </channel>
</rss>