Simplest Open-Closed Principle with Code Example in C#

preview_player
Показать описание
In this video, you can learn about the open-closed principle in software engineering and its usage with a C# code example.

►►►Like this Video? Please be sure to SUBSCRIBE for more:

Need more support? Join the Code First discord server and become part of the community.

----------My Social Links:

#OpenClosedPrinciple
Рекомендации по теме
Комментарии
Автор

When i Understand this right in the first example we blow up the method of PizzaMaker, when we added new Types of pizza's.
Under OCP, we must only add new Type of Pizza, creates this (Obj) and changed nothing at existing code. Short good Example. GJ :)

thomasrullmann
Автор

The point is that whether one more kind of pizza extended or not, the making function works as it's supposed to. So the functioning class doesn't need to know the type from external information. the example above was demonstrated with using polymorphism principle.
the Program function created different pizza classes inside of it. However, It'd be very static and useless when it comes to users to make their own decisions. So somehow this program needs to get the decision information from outside of the class. To get the information, It can directly refer the information and caching it which breaks single responsibility principle. so a better way is to use delegate chains in that case.

It's a good and easy example for beginners. great

susi
Автор

What if we don't know which instance to create? In the end you manually create TomatoPizza and MushroomPizza and send them to MakePizza method. But usually that's not the case. Usually user chooses the PizzaType and then based on his chose we have to create a new instance. And to do that i would end up again with the same case as the beggining saying if PizzaType.Tomato return new TomatoPizza(), else if pizzaType.Mushroom return new MushroomPizza(), and then when i have the instance i can send it to MakePizza method no? How to solve that by sticking to principle

pauloperovic
Автор

In your example, if you want to add a new pizza (say anchovy'), you would also have to update the PizzaType enum, and recompile.

tetley