Document Factory
Author: Joshua Taylor Version: 1.0.0
Overview
This project is a demonstration of implementing the factory method pattern from the Gang of Four's Design Patterns. The scenario of this example is for generating pages using the factory method pattern for two types of documents: resumes and APA reports. These document classes derive from an abstract Document class and implement a method called MakePage which implements the factory method pattern.
Getting Started
Document Factory is a console application targeting the .NET Core 2.0 platform. The .NET Core 2.0 SDK can be downloaded from the following URL for Windows, Linux, and macOS:
https://www.microsoft.com/net/download/
The dotnet CLI utility can be used to build and run the application (after cloning this repository):
cd DocumentFactory/DocumentFactory
dotnet build
dotnet run
Additionally, users can build, debug, and run Document Factory using Visual Studio 2017 or greater by opening the solution file from this repository. All dependencies are referenced via NuGet and should be brought in during the restore process. If this does not occur, the following will download all needed dependencies:
dotnet restore
Operation
Classes
Document
An abstract class containing the abstract MakePage factory method to be overridden by any classes assignable to Document. In addition, Document contains a Type property which provides a human-readable string representation of the type of document being used.
Resume
Concrete class deriving from Document which contains an override for the MakePage method which switches on its string parameter to decide which Page assignable class to instantiate and return to the caller. These class types match against the names of Page assignable classes found within the DocumentFactory.Pages.Resume namespace. These classes all represent pages found within a real-world resume. If no match is found, then this factory method simply returns null.
Report
Concrete class deriving from Document which contains an override for the MakePage method which switches on its string parameter to decide which Page assignable class to instantiate and return to the caller. These class types match against the names of Page assignable classes found within the _DocumentFactory.Pages.Report namespace. These classes all represent pages found within a real-world APA report. If no match is found, then this factory method simply returns null.
Page
An abstract class containing the abstract Type and Content string properties representing a human-readable type name of the page ("Cover Letter" for the CoverLetter class) and the body of the page (respectively) when overridden by any Page assignable class. All classes deriving from Page can be found within the DocumentFactory.Pages.Resume and DocumentFactory.Pages.Report namespaces. Additionally, an override to the ToString() method is provided which provides a nicely formatted string representation of the page using its Type and Content properties.
Factory Method Pattern
The factory method pattern involves a method that can be overridden to provide a means of instantiating objects using polymorphism to change the behavior of constructing the requested objects. This can include choosing a specific child class and/or initializing the instantiated object's state based on arguments to the factory method or some form of state found within the factory's class or elsewhere in the running program. In this example, the Document abstract class specifies a MakePage method which implements this pattern and returns a new object assignable from the Page abstract class based on MakePage's string argument.
Product
The product of this factory method implementation is a new object that is assignable to the Page abstract class. If using the Resume class (derived from Document), then this could be any of the classes defined in the DocumentFactory.Pages.Resume namespace. If using the Report class (also derived from Document), this could be any of the classes defined in the DocumentFactory.Pages.Report namespace.
Concrete Product
Each class assignable to the Page abstract class provide two strings: Type and Content. Type is a human-readable representation of the type of page, such as "Cover Letter" for the class CoverLetter. Content is the actual body of the page.
Creator
The creator in this project is the MakePage abstract method from in the Document abstract class which is overridden by the two concrete classes that derive from Document: Resume and Report. Each of these classes' MakePage overrides contain a switch statement on their single string argument determining which type of page associated with that document to instantiate. The cases matching this argument correspond with the output of the C# keyword nameof for the type of page the caller would like to instantiate using this factory method. If the provided input does not match any of the cases found in that override of the MakePage method, then the factory will simply return null.
Concrete Creator
The creator for this project does not contain any state beyond the override for the abstract Type string property specific to the Document assignable class being used. Type for Document assignable classes serves the same purpose as Type for Page assignable classes in that it provides a human-readable representation of the type of document being used.
Unit Testing
Unit testing has not been included for this project.
Change Log
- 5.16.2018 - Joshua William Taylor - Initial release. No unit testing included.