Living in the Tech Avalanche Generation

A practitioner’s introspective on technology

GOF Series: Episode #2 [The Abstract Factory]

The pattern a day keeps the doctor away series continues. In the last instalment of the series we looked at the Factory Method pattern, this time we look at the Abstract Factory Pattern. The abstract Factory Pattern is useful in creating groups of related object types without a requirement to create their concrete types explicitly and achieves this by using composition where a class delegates the role of object instantiation to another class. This pattern can be described in UML like this:

Click to see in full size

The tests included (as per below) are for the purpose of demonstrating the code and are not meant to be taken as complete unit tests.

   1:  [Test]
   2:  public void AbstractFactoryTest()
   3:  {
   4:      //set an earlier time to that 
   5:      //when the document is created
   6:      DateTime earlier = 
   7:          DateTime.Now.Subtract(new TimeSpan(0, 1, 0));
   8:      //create the mock of the AncillaryService
   9:      var mock = 
  10:          new Mock<AncillaryService>(new 
  11:              LitigationSupportFactory());
  12:      mock.Object.
  13:          SetupProject("My Test Lit Support Project");
  14:      //use the factory method that create a new document
  15:      //using the mock instance object.
  16:      Document litDoc = 
  17:          mock.Object.
  18:          NewDocument("Lit Support Test Document");
  19:      mock.Object.
  20:          PersistDocument(litDoc);
  21:      //perform the tests for 
  22:      //the Lit support factory
  23:      //and the client (AncillaryService) 
  24:      //that has it in its composite.
  25:      Assert.IsTrue(litDoc is LitigationSupportDocument);
  26:      Assert.IsTrue(litDoc.Name == "Lit Support Test Document");
  27:      Assert.That(litDoc.DateCreated.Value > earlier);
  28:      Assert.IsTrue(File.Exists
  29:          (@"C:\DevFileDrop\Lit Support Test Document.litdoc"));
  30:   
  31:      mock = 
  32:          new Mock<AncillaryService>(
  33:              new ImageWorkflowFactory());
  34:      mock.Object.SetupProject("My Test IW Project");
  35:      ImageWorkflowDocument iwDoc = 
  36:          mock.Object.
  37:          NewDocument("IW Test Document") 
  38:          as ImageWorkflowDocument;
  39:      mock.Object.PersistAll();
  40:      Assert.IsTrue(iwDoc != null);
  41:      Assert.IsTrue(iwDoc.Name == "IW Test Document");
  42:      Assert.That(iwDoc.DateCreated.Value > earlier);
  43:      Assert.IsTrue(File.Exists
  44:          (@"C:\DevFileDrop\IW Test Document.iwdoc"));
  45:  }

The code above assumes the AncillaryService, LitigationSupportFactory and ImageWorkflowFactory classes (which are indeed in the download code. The link to the FULL example code can be found here and comes with all previous modules in the series and the accompanying power point presentation to explain each pattern in detail, so I wont repeat that content here.

Share/Save/Bookmark

No comments yet. Be the first.

Leave a reply

Creative Commons Attribution-ShareAlike 2.5 Australia
Creative Commons Attribution-ShareAlike 2.5 Australia