Skip to content

Commit 9fbbb8d

Browse files
committed
Update factory method example
1 parent ded79b7 commit 9fbbb8d

File tree

14 files changed

+666
-73
lines changed

14 files changed

+666
-73
lines changed

.editorconfig

Lines changed: 610 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace RestaurantManagement.Common;
2+
3+
/// <summary>
4+
/// The Product declares the interface, which is common to all objects that
5+
/// can be produced by the creator <see cref="Restaurant"/> and its subclasses.
6+
/// </summary>
7+
public interface IMeal
8+
{
9+
void ShowDescription();
10+
}

src/CreationalPatterns/FactoryMethod/RestaurantManagement/Restaurants/Common/Restaurant.cs renamed to src/CreationalPatterns/FactoryMethod/RestaurantManagement/Common/Restaurant.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using RestaurantManagement.Meals.Common;
2-
3-
namespace RestaurantManagement.Restaurants.Common;
1+
namespace RestaurantManagement.Common;
42

53
/// <summary>
64
/// The Creator class declares the factory method that returns new product objects.
@@ -28,4 +26,4 @@ public void OrderDailySpecial()
2826
var mainCourse = PrepareMainCourse();
2927
mainCourse.ShowDescription();
3028
}
31-
}
29+
}
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Reflection;
22
using DesignPatternsLibrary.PatternExecutors;
3-
using RestaurantManagement.Restaurants;
4-
using RestaurantManagement.Restaurants.Common;
3+
using RestaurantManagement.Common;
4+
using RestaurantManagement.FastFood;
55

66
namespace RestaurantManagement;
77

@@ -11,16 +11,17 @@ public class Executor : PatternExecutor
1111

1212
public override void Execute()
1313
{
14-
Restaurant restaurant = InitializeRestaurant();
15-
14+
var restaurant = InitializeRestaurant();
1615
restaurant.OrderDailySpecial();
1716
}
1817

19-
private Restaurant InitializeRestaurant()
18+
private static Restaurant InitializeRestaurant()
2019
{
21-
// This is usually stored within some configuration
22-
var choosenType = typeof(FastFoodRestaurant).FullName;
20+
// Choose between FastFoodRestaurant and VegetarianRestaurant.
21+
// The choice is hardcoded here, but it's usually made through some configuration.
22+
var chosenType = typeof(FastFoodRestaurant).FullName!;
2323

24-
return Assembly.GetExecutingAssembly().CreateInstance(choosenType) as Restaurant;
24+
var restaurant = Assembly.GetExecutingAssembly().CreateInstance(chosenType) as Restaurant;
25+
return restaurant!;
2526
}
26-
}
27+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using RestaurantManagement.Common;
2+
3+
namespace RestaurantManagement.FastFood;
4+
5+
/// <summary>
6+
/// Concrete creators override the base factory method so it returns a different type of product.
7+
/// </summary>
8+
public class FastFoodRestaurant : Restaurant
9+
{
10+
public override IMeal PrepareMainCourse() => new Hamburger();
11+
}

src/CreationalPatterns/FactoryMethod/RestaurantManagement/Meals/Hamburger.cs renamed to src/CreationalPatterns/FactoryMethod/RestaurantManagement/FastFood/Hamburger.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
using RestaurantManagement.Meals.Common;
1+
using RestaurantManagement.Common;
22

3-
namespace RestaurantManagement.Meals;
3+
namespace RestaurantManagement.FastFood;
44

55
/// <summary>
66
/// Concrete products are different implementations of the product interface <see cref="IMeal"/>.
77
/// </summary>
88
public class Hamburger : IMeal
99
{
10-
public void ShowDescription()
11-
{
10+
public void ShowDescription() =>
1211
Console.WriteLine("Hamburger - with beef meat, Worcestershire sauce and cheese.");
13-
}
14-
}
12+
}

src/CreationalPatterns/FactoryMethod/RestaurantManagement/Meals/Common/IMeal.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/CreationalPatterns/FactoryMethod/RestaurantManagement/Restaurants/FastFoodRestaurant.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/CreationalPatterns/FactoryMethod/RestaurantManagement/Restaurants/VegetarianRestaurant.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/CreationalPatterns/FactoryMethod/RestaurantManagement/Meals/GreenSalad.cs renamed to src/CreationalPatterns/FactoryMethod/RestaurantManagement/Vegetarian/GreenSalad.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
using RestaurantManagement.Meals.Common;
1+
using RestaurantManagement.Common;
22

3-
namespace RestaurantManagement.Meals;
3+
namespace RestaurantManagement.Vegetarian;
44

55
/// <summary>
66
/// Concrete products are different implementations of the product interface <see cref="IMeal"/>.
77
/// </summary>
88
public class GreenSalad : IMeal
99
{
10-
public void ShowDescription()
11-
{
10+
public void ShowDescription() =>
1211
Console.WriteLine("Green salad - with lettuce, cucumber and green olives");
13-
}
14-
}
12+
}

0 commit comments

Comments
 (0)