Design Pattern - Structural - AbstractFactory

  

Scenario:

Create a Student factory to get student object by the school and department.

Solution:

This pattern provides to encapsulate individual factories.

Example: StudentFactory to create students based on the school and the department they study in
  1. StudentFactory 

  2. 1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    namespace TestProject
    {
        public interface IStudent
        {
            List<string> Courses();
        }
    
        public class UclaComputerScienceSchool : IStudent
        {
            public List<string> Courses()
            {
                return new List<string>() { "C#" };
            }
        }
    
        public class UclaMechanicalEngineeringSchool : IStudent
        {
            public List<string> Courses()
            {
                return new List<string>() { "Mechanics" };
            }
        }
    
        public class BoothFinanceSchool : IStudent
        {
            public List<string> Courses()
            {
                return new List<string>() { "StockMarkets" };
            }
        }
        public class BoothPolicySchool : IStudent
        {
            public List<string> Courses()
            {
                return new List<string>() { "Governance" };
            }
        }
    
        public abstract class StudentFactory
        {
            public abstract IStudent? GetStudent(string department);
    
            public static StudentFactory CreateStudentFactory(string school)
            {
                if (school.Equals("ucla", StringComparison.InvariantCultureIgnoreCase))
                {
                    return new UclaStudentFactory();
                }
                else
                {
                    return new BoothStudentFactory();
                }
            }
        }
    
        public class BoothStudentFactory : StudentFactory
        {
            public override IStudent? GetStudent(string department)
            {
                if (department.Equals("finance", StringComparison.InvariantCultureIgnoreCase))
                {
                    return new BoothFinanceSchool();
                }
                else if (department.Equals("policy"))
                {
                    return new BoothPolicySchool();
                }
    
                return null;
            }
        }
    
        public class UclaStudentFactory : StudentFactory
        {
            public override IStudent? GetStudent(string department)
            {
                if (department.Equals("computerscience", StringComparison.InvariantCultureIgnoreCase))
                {
                    return new UclaComputerScienceSchool();
                }
                else if (department.Equals("mechanical"))
                {
                    return new UclaMechanicalEngineeringSchool();
                }
    
                return null;
            }
        }
    }

  3. Create students and get courses they study in based on school and department they have picked.
  4.  
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    StudentFactory studentFactory = null;
    IStudent? student = null;
    
    studentFactory = StudentFactory.CreateStudentFactory("ucla");
    
    Console.WriteLine($"Student's School is {studentFactory.GetType().Name}");
    
    student = studentFactory.GetStudent("computerscience");
    
    Console.WriteLine($"Student's Stream is {student.GetType().Name}");
    
    var courses = student.Courses();
    
    foreach (var course in courses)
    {
        Console.WriteLine($"Courses Taken:{course}");
    }
    
    Console.ReadLine();

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...