SOLID Principles of Object Oriented Programming - Real Life Examples

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] principles it is a common interview question and explaining it with real world examples will show that you really understand the concept my name is prudine and welcome to dotnet Mastery if you enjoy free content related to.net programming make sure to subscribe to the channel that way you will not miss the new content that is being uploaded solid is not one but combination of five different principles let me start by explaining each one of them with a real world example first principle that we have is single responsibility principle single responsibility defines that each class or entity should only be responsible for one thing and it should not be populated with other functionalities that are not related to the main purpose of that entity to explain that with a real world example I was on Amazon the other day and I noticed that they had some of the iPhone cases with a bottle opener behind it and as soon as I saw that I'm like that's odd why would anyone ever need a bottle opener in an iPhone case and as soon as I looked at that it reminded me that hey this is breaking the single responsibility principle now why does it break the single responsibility principle because personally if I was buying an iPhone case I will never imagine that or imagine a need to have a bottle opener inside there in order to make that Implement single responsibility principle and iPhone case should just be an iPhone case maybe a slim or a fancy case maybe with some card holders but purely an iPhone case and bottle opener should be a bottle opener and not an iPhone case along with that now that is very basic example but if I also search for something called a stuck boat on Google there are some duckboard tools in tourist attraction it is basically like a car on the road and when it goes in water it can act as a boat now they are good for entertainment purpose but in real life if an individual has a boat and a car they will never go and buy a duck boat to replace their car and boat in this case duck Port also breaks the single responsibility principle to fix that it should be responsible into two product which are responsible for their individual task being a boat or being a car so that is a real world example of entities that are breaking the single responsibility principle after that we have the open closed principle that basically means that entity or class should be open for extension but closed for modification a real world example is when developers build applications for iPhone or Android phones both the operating system exposes some of their base features which applications can extend like when they have to capture an image or analyze an incoming call using the base features that are provided new applications can be built which extend the base features and add more items but they cannot edit the built-in classes they are managed by Apple or Google team and they are closed for outside modification when it comes to programming language as well if we are using the.net framework we can always extend the built-in classes that are there in the.net framework but we cannot go and toggle the framework directly they are closed for modification that is a straightforward example of the open closed principle third principle that we have is a list of substitution principle this example basically says that subclass or child class can be substituted for their parent or Base Class to give you a real world example of that let's say we are making a sweet dessert and unfortunately we are out of sugar now if we have liquid sugar or cane sugar we can of course use them as a replacement but can we replace that with salt of course not and that is what liskov principle is another example of that is if you are going on a vacation and if you have rented a seven-seater minivan now if the rental car company does not have a minivan they can substitute that with an 8-seater full-size SUV or some other car that will fit seven people but they cannot substitute that with a five-seater sedan now this is again a basic example but that is to explain you how the list of substitution principle can be applied in real world fourth principle that we have is interface segregation principle now this is an interesting one here we are saying that unrelated interfaces should be separated out the reason behind that if something is not related to a particular thing an interface is responsible for then if a client that implements that interface will be forced to implement that extra feature even if they do not want it to give you a real world example we have an interesting scenario here in the presentation I have a car company one without I I basically is the interface segregation principle in this example car company 1 gets a base card with many features like remote start wireless phone charger leather interior all-wheel drive and much more built in but since everything is built in in the base model the price is also higher about 70 000 in this case but main point and notice here is there are only a few configuration that a user can select like color of the car wheel size and car range now if you are on a low budget and you do not want all the fancy features that comes in the base car model you still cannot get a car since the base model by default has everything in it and you are forced to take all the base features and pay the higher price now if in future the company wanted to reduce the base price and take some of the features out from the base feature and add them to the add-on feature it is very difficult in this scenario since imaginary let us assume that it is set in stone that base car will always have these features this is an example that violates the interface segregation principle because here you are forcing the end user to get all of the base features and also pay the price for those base feature now the question would be what could have been done different that way it follows the interface segregation principle to answer that let's say we have another car company that follows the interface segregation principle in this particular example you can see base feature is only standard car Option and because of that the base price of the car is also less now when a user is building a car they can decide on if they want some of the features or not that way they can extend the built-in car and add new features but because they are not inside the base feature they are not required to add all of that into their price so what is the benefit here because everything can be customized you only get the base features that are critical everything else you can add as an extension to build the perfect car based on your requirement and you are not forcing the client to take everything that is there in the base class so interface aggregation principle basically says that unrelated interfaces should be separated out so that client are not forced to implement something that they do not require last one that we have is dependency inversion principle the basic idea behind that is a classes and entities should depend on the extraction and not on the concrete classes one example would be if you are booking a car from rental company for your vacation what you expect is basically when you get there to pick up your car it will be ready for you you do not need to clean the car or fill the gas everything related to that car will be handled by the rental company on top of that if you cancel the reservation and change to some other rental company you will still get everything ready and configured when you go to pick up the car so in this example you are not responsible for handling anything related to the car you get the rental car and everything is configured in there another example could be let's say you have a simple toy that operates on a triple a battery the relation is that the toy is dependent on the battery but the toy does not care if you replace the battery with a brand new battery of a different brand or maybe just replace the set altogether toy is Loosely coupled with the brand of the battery like in this scenario if a toy was dependent that no AAA Battery of only a certain brand will work then that is a tight coupling so I hope with that in this short video you have a better understanding of all the solid principles with real world example if you enjoyed the video make sure to like the video and leave a comment on what you think about the examples or if you have some better examples to explain these principles you can also check out more free videos on.netmatchedway.com until then Happy coding
Info
Channel: DotNetMastery
Views: 16,303
Rating: undefined out of 5
Keywords:
Id: OFEvNV-M6M8
Channel Id: undefined
Length: 11min 7sec (667 seconds)
Published: Wed Oct 05 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.