Program to Interface, not Implementation - Beginner's Tutorial for Understanding Interface, Abstract class and Concrete class. In this article we will look into the details of Interfaces, Abstract class and Concrete class in C#. We will try to what each of them are and what should be used when to have a better design for the application. Introduction. In this article we will look into the details of Interfaces, Abstract class and Concrete class in C#. We will try to understand what. Background Every once in a while I see questions on . Most of the time its the beginner's who are asking the question. The reason such questions are being asked again and again is that the answers to the previous questions(a lot of very good answers) are getting lost in time and number of questions. And thus, I thought about creating this small article on some of the points related to these topics so that this could be used as a reference point. Using the code. Let us start the discussion by looking at the . Once we understand this, we can dive into the . Meaningful. entity can be thought of as any real world entities or business entities that need to be represented in our application. A blueprint to represent all the objects that have same attributes and behavior. It has single behavior i. Retrieving the. age(in years) of the student on a given date. The class also contains 4 member variables which are used as backing fields. In newer versions of C# we can completely do away with the backing fields if there is no implementation. We can do this by implementing these properties as automatic properties. Student. . Deciding the public interface. Encapsulation on the other hand deals with the. This would include whatever code we write inside our properties and methods. This code is actually hidden from the users of this class. This is called encapsulation. So in a way what we are saying is that the exposed public properties and methods are the abstraction provided by the class. What if we. have a class that does not know how to implement all the behavior.
Suppose our student class also need to provide a function Get. Fee(). But this class does not know how to implement this because the fee will depend on the type of student (assuming the students could be of multiple. Regular and Online). So to represent this let us change the student class in cush a way that it has one method without any implementation. AStudent. . Since one of our function is. What this essentially says is that this class defines a public interface i. So if someone needs to inherit from this class they need to provide the. We cannot use abstract class directly because marking a class abstract indicates that this class is meant as a desired abstraction(contract so to say) and whoever wants to inherit from this will have to provide the implementation of the abstract methods. This class exposes the same abstraction like AStudent since it is derived from Astudent but it also provides implementation for the abstract methods of AStudent. Open, comprehensive, centralized, standardized interface and engine to access metadata from the federation of providers and hosts. Installing the 'Stalker' portal Necessary services & packets apache2 nginx memcached mysqld >= 5 php5 >= 5.3.0 php-soap * php5-intl *. C# Program Against InterfacesNow let us take this concept of abstract class to an extreme and think about a class which has all the methods marked as abstract. This means that, this class only provides the abstraction one should provide and it comes with no implementation. Before talking on pure abstract class, let us look at the interfaces first then we will be in a better position to understand all the concepts in a better way. What is an Interface Interface is a language construct that let us define a set of methods and properties in it. This looks very similar to the concept of. IShape. . So to implement the interface we need to provide the concrete class with the implementation of the interface. Square : IShape. . All the members of the interface. Its time to understand the . Let us try to understand it in a step by step manner: The interface and abstract classes are a way of enforcing a contract on the concrete class. So we have to either inherit from an abstract class or implement interface if. We need multiple classes to behave in a polymorphic way. Let us look at some of the points that could help us answer this question. A class can have only one immediate base class but it can implement more then one interface. Which is not. possible if our contract is specified as an abstract class rather than an interface. Whoever will implement this interface will provide an implementation of the methods. Abstract class: When we need to implement a base type that knows how to implement some part of the abstraction i. It leaves the responsibility of implementing remaining methods to the classes deriving from this. To Illustrate this let us revisit the Shape problem. IShape interface is only a contract that says that whoever will implement it will have to. Get. Area and Get. Perimeter function. If we need to use this class, we need to implement the remaining part of the contract. The users of the Rectangle class should still work with the IShape handle. The handle. will just be pointing to the concrete implementation of the Rectangle class. Program to an Interface, not an implementation. Now that we have talked about interfaces, abstract classes and classes, we can summarize the discussion below: Concrete class is the actual implementation. Interface is the contract, it specifies the. Abstract class is a trade off between the two i. Why can't we simply remove the interface and abstract classes and. We are anyways free to create any number of versions for our concrete classes. Well to. find the answer to this question, we need to look at a best practice . There are several benefits of this approach like maintainability, extensibility and testablility. Anyone. can write code that would make the application to work but the differentiating factor is whether or not this code is maintainable, extensible. Let us try to look at these three concepts in details and understand how programming to an interface is always. Let us think of a simple component which provides us with a Logger class. Logger. Bad. . An Admin console also uses this class to get the recent 1. One problem exist at the Logger component's end i. Second problem lies at the usage end. The user code is actually using the concrete. The moment we take this changed code, we need to make a code change in all. Get. Last. 10. Messages method(it could be in thousands of places). So it is safe to say that the. If we want our application to be maintainable then we should have programmed it to an interface i. Now instead of programming to. Also, when the function start. List instead of array it will work too because we have programmed our application to an interface and not the implementation. So the code to use the Logger. Bad class should look like: class Some. Class. . Thus making our application more maintainable. Extensibility. Now we have solved only a part of the problem. If we were the designer of the component then we should have specified an interface. Logger class and implemented our component in this way. ILogger. . If we need to add one more class Log. To. Cloud then. we simply need to implement the contract and our new class is ready for consumption from the application. Log. To. Clound : ILogger. But let us again look at the usage. Some. Class. . If I need to use selectively use Log. To. Cloud, it would be possible for. To fix this let us program to interface and not to implementation. Some. Class. Two. If we want to use the Log. To. Cloud we just need. Log. To. Cloud. If we want to use our old Logger. Good, we can pass its concrete instance. Some. Class. Two userclass. Some. Class. Two(). Some. Action(new Logger. Good()). userclass. Some. Action(new Log. To. Cloud()). Colourised in 1ms. Testability. Having unit testable applications also mean extensive use of interfaces rather than implementation. This is needed because we. MOCK classes while unit testing our components. It will not depend on the concrete class. The actual object of the concrete class will be passed to this class from the outside (Dependency Injection. We have seen when should we use. We have seen how interfaces are used to provide an abstraction contract. We have. also seen how interfaces benefit us in creating Maintainable, extensible and testable applications. This article has been written from. One of the motivating factor in writing this article are the frequent questions related to interfaces and. Code. Project and other forums. I hope this article was somewhat informative. History. 2. 6 December 2. First version. Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
October 2016
Categories |