The University of Georgia (UGA or Georgia) is a public land-grant research university with its main campus in Athens, Georgia. Chartered in 1785, it is one of the oldest public universities in the United States. It is the flagship school of the University System of Georgia.
```csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DesignPatterns.Creational.FactoryMethod { // Product public abstract class Page { public abstract string GetContent(); } // ConcreteProduct public class SkillsPage : Page { public override string GetContent() { return "Skills: C#, .NET, Azure, SQL Server"; } } // ConcreteProduct public class EducationPage : Page { public override string GetContent() { return "Education: Bachelor's Degree in Computer Science"; } } // ConcreteProduct public class ExperiencePage : Page { public override string GetContent() { return "Experience: 5 years as a Software Developer"; } } // ConcreteProduct public class IntroductionPage : Page { public override string GetContent() { return "Introduction: This report provides an overview of the project."; } } // ConcreteProduct public class ResultsPage : Page { public override string GetContent() { return "Results: The project achieved its goals within the timeline."; } } // ConcreteProduct public class ConclusionPage : Page { public override string GetContent() { return "Conclusion: The project was a success and future work is recommended."; } } // Creator public abstract class Document { private List<Page> _pages = new List<Page>(); // Constructor calls abstract Factory Method public Document() { this.CreatePages(); } public List<Page> Pages { get { return _pages; } } // Factory Method public abstract void CreatePages(); } // ConcreteCreator public class Report : Document { // Factory Method implementation public override void CreatePages() { Pages.Add(new IntroductionPage()); Pages.Add(new ResultsPage()); Pages.Add(new ConclusionPage()); } } // ConcreteCreator public class Resume : Document { // Factory Method implementation public override void CreatePages() { Pages.Add(new SkillsPage()); Pages.Add(new EducationPage()); Pages.Add(new ExperiencePage()); } } public class Client { public static void Main(string[] args) { // Create a Report document Document report = new Report(); Console.WriteLine("Report pages:"); foreach (var page in report.Pages) { Console.WriteLine(page.GetContent()); } Console.WriteLine(); // Create a Resume document Document resume = new Resume(); Console.WriteLine("Resume pages:"); foreach (var page in resume.Pages) { Console.WriteLine(page.GetContent()); } } } } ``` Explanation of Changes: 1. Added `GetContent()` to `Page`: The abstract `Page` class now has an abstract method `GetContent()` which concrete page types will implement to provide their specific content. 2. Implemented `GetContent()` in Concrete Pages: Each concrete page class (`SkillsPage`, `EducationPage`, etc.) now overrides `GetContent()` and returns a string representing the content of that page. 3. Client Prints Content: In the `Main` method, instead of printing the type name, we now call `page.GetContent()` to display the actual content of each page. Why this is better: * More Realistic Example: Documents are composed of content, not just abstract types. This modification makes the example more practical and demonstrates how the factory method can be used to create objects that have specific data or behavior. * Illustrates Polymorphism: The client code interacts with the pages through the base `Page` class and calls the `GetContent()` method. The actual implementation of `GetContent()` that is executed depends on the concrete type of the page object, showcasing polymorphism. * Clearer Demonstration of Object Creation: By showing the content, it's more evident that the factory method is creating distinct objects with different characteristics. This revised code provides a more complete and illustrative example of the Factory Method pattern by demonstrating how the created objects can have different data and behavior, which is a common use case for this pattern.
View Veterinary Medicine