![]() This example might also be a good candidate for usage of Traits in PHP, for what it's worth. Using a specific interface that represents discussions, you can makes sure that the entities you want to have discussions, are compatible with the client code that will be managing those discussions. A forum topic, user profile, and news article aren't even remotely conceptually the same thing, thus trying to get them to inherit the discussion behaviors leads to strange coupling to an unrelated parent. This is also a good example of a situation where an abstract base class is not a good idea. So by giving each content type that you want to have discussions for, a Discussable interface, you are using the interface segregation principle. It ONLY cares about the behaviors it needs, regardless of what those behaviors are associated with. This way, DiscussionManager does not care about any of the unrelated behaviors of the various content types that it uses. Let's have a look at example code in absence of Interface segregation. But still it has to provide implementation in absence of interface segregation principle. Some of the methods may not be applicable to that concrete class. This, prevents B from knowing more than it should.Īssume that you have one fat interface with many methods to be implemented.Īny class, that implements that fat interface has to provide implementation for all these methods. ![]() That interface will only declare the two methods that are used by B, and B will depend on that Interface, instead of directly on A. For that reason, you may insert an interface, call it C, between the two classes (segregation). The more you expose, the more you create the chance for coupling. Now, does B need to know about all ten methods of A? Probably not - the principle of Information hiding. Declaring methods in an interface that the client doesn’t need pollutes the interface and leads to a bulky or fat interface. This eventually helps us to follow the Single Responsibility Principle as well. Adhering to this principle helps to avoid bloated interfaces with multiple responsibilities. Suppose, class A has ten methods, of which only two are used by B. What Is the Interface Segregation Principle The Interface Segregation Principle (ISP) states that a client should not be exposed to methods it doesn’t need. The Interface Segregation Principle is an important concept while designing and developing applications. Say, you have a class A and a class B, which is the client of class A. So, what is ISP? Let me explain it with an example. By doing so, we can ensure that implementing classes only need to be concerned about the methods that are of interest to them. Each class, interface or abstract class should be designed that way. Interface Segregation The I in SOLID stands for interface segregation, and it simply means that larger interfaces should be split into smaller ones. Because, that goes without saying or, at least it should go without saying. Based on that, I don't think ISP is about an interface being "focused" on one logical, coherent group of things. Robert Martin has a very good explanation of Interface segregation principle (ISP), in his book "UML for Java Programmers".
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |