Skip to content

Commit 699d6dc

Browse files
committed
Update the simple factory example
1 parent 9fbbb8d commit 699d6dc

File tree

6 files changed

+29
-37
lines changed

6 files changed

+29
-37
lines changed
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using DesignPatternsLibrary.PatternExecutors;
22
using MealSimpleFactory.Factories;
3+
using MealSimpleFactory.Meals;
34
using MealSimpleFactory.Meals.Common;
45

56
namespace MealSimpleFactory;
@@ -10,13 +11,20 @@ public class Executor : PatternExecutor
1011

1112
public override void Execute()
1213
{
13-
Console.WriteLine("Please enter desired meal name (green salad/hamburger): ");
14-
string mealName = Console.ReadLine();
14+
var response = string.Empty;
15+
while (response is not "1" and not "2")
16+
{
17+
Console.WriteLine("Please choose the desired meal name (enter the number): ");
18+
Console.WriteLine("1. Green salad");
19+
Console.WriteLine("2. Hamburger");
20+
response = Console.ReadLine()!;
21+
}
1522

16-
MealFactory factory = new MealFactory();
17-
IMeal meal = factory.CreateMeal(mealName);
23+
var mealName = response == "1" ? nameof(GreenSalad) : nameof(Hamburger);
24+
var factory = new MealFactory();
1825

26+
IMeal meal = factory.CreateMeal(mealName);
1927
meal.ShowDescription();
2028
meal.ShowAmountOfCalories();
2129
}
22-
}
30+
}
Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Reflection;
1+
using System.Globalization;
2+
using System.Reflection;
23
using MealSimpleFactory.Meals;
34
using MealSimpleFactory.Meals.Common;
45

@@ -10,7 +11,7 @@ namespace MealSimpleFactory.Factories;
1011
/// </summary>
1112
public class MealFactory
1213
{
13-
private Dictionary<string, Type> _meals;
14+
private readonly Dictionary<string, Type> _meals = new();
1415

1516
public MealFactory()
1617
{
@@ -25,18 +26,11 @@ public IMeal CreateMeal(string mealName)
2526
return new NullMeal();
2627
}
2728

28-
return Activator.CreateInstance(type) as IMeal;
29+
return (Activator.CreateInstance(type) as IMeal)!;
2930
}
3031

31-
private Type GetTypeForCreation(string mealName)
32-
{
33-
if (!_meals.TryGetValue(mealName, out Type type))
34-
{
35-
return null;
36-
}
37-
38-
return type;
39-
}
32+
private Type? GetTypeForCreation(string mealName) =>
33+
_meals.TryGetValue(mealName, out var type) ? type : null;
4034

4135
/// <summary>
4236
/// Load available meal types.
@@ -45,16 +39,14 @@ private Type GetTypeForCreation(string mealName)
4539
/// </summary>
4640
private void LoadAvailableMealTypes()
4741
{
48-
_meals = new Dictionary<string, Type>();
49-
5042
Type[] assemblyTypes = Assembly.GetExecutingAssembly().GetTypes();
5143

5244
foreach (Type type in assemblyTypes)
5345
{
5446
if (type.GetInterface(typeof(IMeal).ToString()) != null)
5547
{
56-
_meals.Add(type.Name.ToLower(), type);
48+
_meals.Add(type.Name, type);
5749
}
5850
}
5951
}
60-
}
52+
}

src/CreationalPatterns/AbstractFactory/MealSimpleFactory/Meals/Common/IMeal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ public interface IMeal
55
void ShowAmountOfCalories();
66

77
void ShowDescription();
8-
}
8+
}

src/CreationalPatterns/AbstractFactory/MealSimpleFactory/Meals/GreenSalad.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@ namespace MealSimpleFactory.Meals;
44

55
public class GreenSalad : IMeal
66
{
7-
public void ShowAmountOfCalories()
8-
{
7+
public void ShowAmountOfCalories() =>
98
Console.WriteLine("Calories: 250 kcal");
10-
}
119

12-
public void ShowDescription()
13-
{
10+
public void ShowDescription() =>
1411
Console.WriteLine("Green salad: lettuce, cucumber and green olives");
15-
}
16-
}
12+
}

src/CreationalPatterns/AbstractFactory/MealSimpleFactory/Meals/Hamburger.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@ namespace MealSimpleFactory.Meals;
44

55
public class Hamburger : IMeal
66
{
7-
public void ShowAmountOfCalories()
8-
{
7+
public void ShowAmountOfCalories() =>
98
Console.WriteLine("Calories: 450 kcal");
10-
}
119

12-
public void ShowDescription()
13-
{
10+
public void ShowDescription() =>
1411
Console.WriteLine("Hamburger: beef meat, Worcestershire sauce and cheese.");
15-
}
16-
}
12+
}

src/CreationalPatterns/AbstractFactory/MealSimpleFactory/Meals/NullMeal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ public void ShowAmountOfCalories()
1111
public void ShowDescription()
1212
{
1313
}
14-
}
14+
}

0 commit comments

Comments
 (0)