![]() ![]() Violation of the Interface Segregation Principle also leads to violation of the complementary Open Closed Principle. Not exactly the behavior you want for a toy house, is it? Imagine that you are writing the ToyHouse class and the IntelliSense feature of your IDE pops up the fly() method for autocomplete. Such violations affect code readability and confuse programmers. This is a violation of the Interface Segregation Principle. Toy.java public interface Toy Īs you can see in the code, ToyHouse needs to provide implementation of the move() and fly() methods, even though it does not require them. An interface to define the behaviors of toys is this. Some toys, such as a toy car or toy train can additionally move, while some toys, such as a toy plane can both move and fly. Interface Segregation Principle Violation (Bad Example)Ĭonsider the requirements of an application that builds different types of toys. Thus clients, instead of implementing a “fat interface”, can implement only those “role interfaces” whose methods are relevant to them. Each “role interface” declares one or more methods for specific behavior. The Interface Segregation Principle advocates segregating a “fat interface” into smaller and highly cohesive interfaces, known as “role interfaces”. An addition of a method or change to a method signature requires modifying all the implementation classes even if some of them don’t use the method. In addition, the implementing classes are subject to change when the interface changes. For such interfaces, also called “fat interfaces”, implementing classes are unnecessarily forced to provide implementations (dummy/empty) even for those methods that they don’t need. What the Interface Segregation Principle says is that your interface should not be bloated with methods that implementing classes don’t require. Here, the term “Clients” refers to the implementing classes of an interface. This principle states that “Clients should not be forced to depend on methods that they do not use”. Martin while consulting for Xerox, which he mentioned in his 2002 book, Agile Software Development: Principles, Patterns and Practices. This principle was first used by Robert C. The Interface Segregation Principle represents the “I” of the five SOLID principles of object-oriented programming to write well-designed code that is more readable, maintainable, and easier to upgrade and modify. As a Java programmer, you must have written a large number of interfaces, but the critical question is- have you written them while keeping design principles in mind? A design principle to follow while writing interfaces is the Interface Segregation Principle. Other classes can use that interface with the implements keyword, and then provide implementations of the declared methods. You use the interface keyword to create an interface and declare methods in it. From a coding perspective, writing an interface is simple. Interfaces form a core part of the Java programming language and they are extensively used in enterprise applications to achieve abstraction and to support multiple inheritance of type- the ability of a class to implement more than one interfaces. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |