The course is part of this learning path
In this course, we explore the SOLID principles one by one and look at how they work.
Learning Objectives
- Understand what SOLID principles are
Intended Audience
- Beginner Java developers
- Anyone with a desire to learn about SOLID principles
Prerequisites
- Basic Java knowledge
Hello, dear friends. In this video, we will examine the Interface Segregation Principle. So, let's begin. The Interface Segregation Principle states that no client code object should be forced to depend on methods it does not use. Basically, each code object should only implement what it needs, and not be required to implement anything else. For instance, if you have a phone and need to charge it, you don't truly need this adapter, but you got it anyway. Right now, you'll only use one connection, the rest are unnecessary. Alternatively, let's say you have a dishwasher with a USB port. Why though
do we not use a USB connection for the dishwasher? This principle tells us to only use interfaces that are truly necessary. Otherwise, our code will not conform to the single responsibility rule. Now, let's see this in a code. Let's say, for example, we have a market that sells food and clothes. Now, let's create an interface and name it Product. Now, add some properties name: ProductMark, Salary, Size for clothes like small, large, and Calorie for foods. Now, let's derive first class for Foods. For Foods, we don't need size, so don't override.
Now, as you see, we have an error. See, solution is implementing interface methods. Implement interface methods. Now, no error. Let's add another class for Clothes. Calorie is unnecessary for this class. Now, see we have an error again so we have to override Calorie. So, there is no error now, but it's not a good structure because size is unnecessary in the Food class, and Calorie property is unnecessary for Clothes. As a result, we must re-design the structure. First, we will create a new interface and name it Base. Now, the property's name, Mark and Salary are necessary for all products. Now, if you want, you can only use this and additionally add calorie and size in class. But for large projects, there will likely be many properties.
So, let's create them using interfaces. New interface named Food, add Calorie. Now, add another interface and name it Clothes, and add Size. Now, if you want to find a Meal class derived from Base and Food interface, implement interface. Now, add another one and name it Trousers derived from Base and Clothes interface. Implement interface. Now, look again. As you see, we don't have any unnecessary properties in classes. So, I think you got it. That's all for the interface segregation design pattern. It's a really important design pattern. I just want to mention, just like the adapter design pattern. In the next video, we will examine the dependency inversion principle. So, I'll see you there.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.