#751 – Inheritance Only Partially Preserves Encapsulation

It can be argued that inheritance breaks encapsulation, since there is a tight coupling between the parent and child classes and because the child class can override behavior in the parent class.

But inheritance does partially preserve encapsulation, with respect to private members of the base class.

  • An instance of the child class can’t access private members in the base class
  • The implementation of the child class can’t access private members in the base class

Assume that we have a Dog class with a private field barkCount and a Terrier class that derives from Dog.

            Terrier t = new Terrier();

            // ERROR: Dog.barkCount inaccessible due to its protection level
            int i = t.barkCount;

Also:

    public class Terrier : Dog
    {
        public void Test()
        {
            Console.WriteLine("Terrier " + Name);

            // ERROR: Dog.barkCount inaccessible due to its protection level
            int i = this.barkCount;
        }
    }

#750 – Use xUnit.net for Unit Testing

On Codeplex, you’ll find a project called xUnit.net–a unit testing framework that allows you to write and run unit tests against your code.

To use xUnit.net, you write test methods that invoke the methods that you want to test and then make various assertions about the expected results.

For example, assume that you have a Divide method:

        public static double Divide(double a, double b)

You can then use xUnit.net to write a test method that makes several assertions about what it expects the results of calling the Divide method to be.

    public class SomeTests
    {
        [Fact]
        public void DivideTest()
        {
            Assert.Equal(3.0, MathClass.Divide(6.0, 2.0));
            Assert.InRange<double>(MathClass.Divide(1.0, 3.0), 0.3333, 0.3334);

            Assert.Equal(3.14, MathClass.Divide(Math.PI, 1.0));  // Fails!
        }
    }

After you build your project, you can use the xUnit test console to run your unit tests.  It will indicate how many tests passed or failed.

750-001

#749 – Example of Some C# Design Patterns

There is a nice little project over at Codeplex that includes a bunch of examples of common design patterns, written in C#.

Tutorial: Common Design Patterns in C# 4.0

This project appears to be only partially done, but does include an implementation of the following design patterns:

  • Abstract Factory
  • Builder
  • Factory Method
  • Prototype
  • Singleton
  • Adapter
  • Bridge

The project has placeholders for other patterns, including:

  • Composite
  • Decorator
  • Facade
  • Flyweight
  • Proxy
  • Chain of Responsibility
  • Command
  • Interpreter
  • Iterator
  • Mediator
  • Memento
  • Observer
  • State
  • Strategy
  • Template Method
  • Visitor

These patterns all come from the well known “Gang of Four” book, Design Patterns: Elements of Reusable Object-Oriented Software.  (Gamma, Helm, Johnson, and Vlissides).

The project has been dormant since July, 2011.  So this is perhaps an opportunity for some other software developers out there to contribute to the project by implementing some of the remaining patterns.

#748 – Use GhostDoc Tool to Document Your Code

GhostDog, available at http://submain.com/products/ghostdoc.aspx, is a Visual Studio add-in that helps you to generate XML style documentation of your source code.

After installing, you’ll see GhostDoc commands under the Tools menu.

748-001

 

The free version of GhostDoc allows you to generate one type member at a time.  If the member is inherited from a base type, the XML documentation is copied from the base type.

748-002

 

There is a Pro version, available for $40, which allows you to document an entire type or entire file with a single command.