9/25/2023 0 Comments Interface segregation![]() ![]() If we are going to apply it more than is necessary it will result a code containing a lot of interfaces with single methods, so applying should be done based on experience and common sense in identifying the areas where extension of code are more likely to happens in the future. Like every principle Interface Segregation Principle is one principle which require additional time and effort spent to apply it during the design time and increase the complexity of code. If the design is already done fat interfaces can be segregated using the Adapter pattern. interface segregation principle - bad example interface IWorker In our case the IWorker interface should be split in 2 different interfaces. We can write a dummy class which does nothing(let's say a launch break of 1 second daily), and can have undesired effects in the application(For example the reports seen by managers will report more lunches taken than the number of people).Īccording to the Interface Segregation Principle, a flexible design will not have polluted interfaces. If we keep the present design, the new Robot class is forced to implement the eat method. This is why in this case the IWorker is considered a polluted interface. On the other side, the don't have to implement it because they don't eat. One on side the new Robot class need to implement the IWorker interface because robots works. But now some robots came in the company they work as well, but they don't eat so they don't need a launch break. Both types of workers works and they need a daily launch break to eat. And we have 2 types of workers some average and some very efficient workers. We have a Manager class which represent the person which manages the workers. Exampleīelow is an example which violates the Interface Segregation Principle. IntentĬlients should not be forced to depend upon interfaces that they don't use. Instead of one fat interface many small interfaces are preferred based on groups of methods, each one serving one submodule. The Interface Segregation Principle states that clients should not be forced to implement interfaces they don't use. Having an interface pollution is not a good solution and might induce inappropriate behavior in the system. Such an interface is named fat interface or polluted interface. In other words, an interface should be designed in a way that it only exposes the methods and properties that a client needs, rather than a larger set of. It states that clients should not be forced to depend on interfaces they do not use. But if we want to extend our application adding another module that contains only some of the submodules of the original system, we are forced to implement the full interface and to write some dummy methods. The Interface Segregation principle is one of the SOLID principles of object-oriented software design. ![]() Considering the module implemented by a class, we can have an abstraction of the system done in an interface. When we design an application we should take care how we are going to make abstract a module which contains several submodules. Interface Segregation Principle (ISP) Motivation The interface segregation principle states that an entity should never be forced to implement an interface that contains elements which it will never use.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |