In our third angular coding session Angular Coding Session Learn Dependency Injection | Practical | 3/8 we will see how to use http logger as a service in our project. Here we will learn very interesting principle of Object Oriented Programming.
Till now we have done coding for Hierarchical DI, Console Logger as a service in our project. Now suppose our requirement changed and we are asked to use http logger instead of Console logger. While doing impact analysis for this change we came to know a very big designing flaw that we introduced while coding our application. I.e. we should not have specifically used Console logger, rather than we should have used abstract of it named as logger that can behave as any type like console, http, and web-socket logger during implementation time. So here we should use open-close concept in our designing i.e. application should be open for extension and closed for changes. It is nothing but polymorphism principle of OOPs that gives us this open-closed concept in designing. As per this principle, we should have a base class and from this base class we will derive our specialized classes.
For example here we can create base class Logger as an abstraction and then it could be any logger like console, http etc. during implementation time. Hence whenever we want to protect our code against new type, we will use polymorphism principle. We will create base class as an abstract and will use this base class everywhere.
All this is explained in detail with coding in our demo session, snapshot below: