![]() |
The fundamental goal of dealing with the complexity of building modern software naturally gives rise to several sub-goals. These sub-goals are directed at the production of quality software, including good implementations of data structures and algorithms. The article focuses on discussing design goals and principles of Object Oriented Programming. OOP GoalsThe three goals of Object Oriented Programming are Robustness, Adaptability, and Reusability. 1. RobustnessEvery good programmer wants to produce software that is correct, which means that a program produces the right output for all the anticipated inputs in the program’s application. In addition, we want the software to be robust, that is, capable of handling unexpected inputs that are not explicitly defined for its application.
The goal of robustness goes beyond the need to handle unexpected inputs, however. Software should produce correct solutions, even given the well-known limitations of computers. For example, if a user wishes to store more elements in a data structure than originally expected, then the software should expand the capacity of this structure to handle more elements. This philosophy of robustness is present, for example, in the vector class in C++’s Standard Template Library, which defines in the expandable array. In addition, if an application calls for numerical computations, those numbers should be represented fully and should not overflow or under-flow Indeed, software should achieve correctness for its full range of possible inputs, including boundary cases, such as when an integer value is 0 or 1 or the maximum or minimum possible values. Robustness and correctness do not come automatically, however, they must be designed from the start. 2. AdaptabilityModern software projects, such as word processors, web browsers, and Internet Search engines, typically involve large programs that are expected to last for many years. Therefore, software needs to evolve over time in response to changing conditions in its environment.
3. ReusabilityGoing hand in hand with adaptability is the desire that software is reusable, that is, code should be usable as a component of different systems in various applications. Developing quality software can be an expensive enterprise, and its cost can be offset somewhat if the software is designed in a way that makes it easily reusable in future applications. Such reuse should be done with care, however. One of the major sources of software errors in the Therac-25 arose from the inappropriate reuse of software from the Therac-20 (which was not designed for the hardware platform used with the Theme-25). So, for software to be truly reusable, we must be clear about what it does and does not do. Given this clarity, however, software reuse can be a significant cost-saving and time-saving technique. OOP PrinciplesThese principles are guidelines intended for programmers to apply while working on software to remove buggy code. 1. Single Responsibility Principle (SRP)The Single Responsibility Principle (SRP) states that there should never be more than one reason for a class to change. This means that every class, or similar structure, in your code should have only one job to do. Simply, a class should have only a single responsibility.
2. Open-Closed Principle (OCP)The Open-Closed Principle states that classes should be open for extension but closed for modification. “Closed for modification” means that once you have developed a class you should never modify it, except to correct bugs. “Open to extension” means that you should design your classes in such a way that new functions will be generated as per new requirements are generated. Simply, “software entities like classes, modules, functions, should be open for extension, but closed for modification”.
3. Liskov Substitution Principle (LSP)The principle was introduced by Barbara Liskov in 1987 and according to this principle “Derived or child classes must be substitutable for their base or parent classes“. It applies to inheritance hierarchies, specifying that you should design your classes so that client dependencies can be substituted with subclasses without the client knowing about the change.
Generally, if a subtype of the supertype does something that the client of the supertype does not expect, then this is in violation of LSP. 4. Interface Segregation Principle (ISP)This principle is the first principle that applies to Interfaces instead of classes in SOLID and it is similar to the single responsibility principle. It states that clients should not be forced to depend upon interface members they do not use. This means that the interface should have the minimal set of methods required to ensure functionality and be limited to only one functionality.
5. Dependency Inversion Principle (DIP)DIP principle The DIP requires that high-level modules should not depend on low-level modules, both should depend on abstraction. Also, abstraction should not depend on details, details should depend on abstractions. Secondly, abstractions should not depend upon details; details should depend upon abstractions. The idea is that we isolate our class behind a boundary formed by the abstractions it depends on. If all the details behind those abstractions change, then our class is still safe. This helps keep coupling low and makes our design easier to change. DIP also allows us to test things in isolation.
6. KISS – Keep It Simple, Stupid PrincipleThis principle suggests not involving complexity in the code and trying to avoid it as much as you can. This is because the more complex code is written, the more difficult it becomes to modify at any later point in time. Other acronyms are: Keep it short and simple, Keep it simple and smart, and Keep it simple and straightforward.
7. DRY PrincipleThe DRY principle stands for the “Don’t Repeat Yourself” principle. Programmers tend to write lots of duplicate code intentionally or unintentionally. This principle forces us to avoid this habit.
Below is the Java program to demonstrate the DRY principle: Java
Though both cat and dogs have common functionality eat(), they speak differently. So we put common functionality eating in the parent class Animal and then extend the parent class to child classes Dog and Cat. Repeating the code is a violation of the DRY principle. 8. YAGNI – You Ain’t Gonna Need It PrincipleYAGNI stands for You Ain’t Gonna Need It. It simply means, don’t use or write code for something, until you really find value in doing it. The programmer usually implements so many things that they really don’t need. They write so many lines of code that are actually not required or may be useless. This leads to a waste of time and energy for the programmer and can even financial losses in the company product. In short, this principle is against the building of any features which are not required at present. So that developer can save time and focus on other pieces or components of a code. 9. DI – Dependency Inversion or Dependency InjectionThis principle revolves around the coupling. Coupling is the degree of connectivity among things, that is how your piece of code is connected to each other. Simply, if your code is talking about another piece of code, there is coupling. A great example could be traditional class-based inheritance. Inheritance actually increases coupling quite a bit. Now as per this principle, either remove or minimize dependency to the extent it could be. If you can’t remove all dependency then at least minimize it. In the context of object-oriented design, depending on a class is called tight coupling, whereas depending on an interface, is called loose coupling. 10. Composition Over Inheritance PrincipleThis principle helps us to implement flexible and maintainable code. It states that we should have to implement interfaces rather than extending the classes. We implement the inheritance when the class need to implement all the functionalities and the child class can be used as a substitute for our parent class. In Java, there are different types of Inheritance: Single inheritance, Multilevel inheritance, Hierarchical inheritance, Multiple inheritances, and Hybrid inheritance. Learn more about inheritance. Java
Following these principles and OOP principles, any developer can build good and efficient software and products which can help the community. |
Reffered: https://www.geeksforgeeks.org
Programming Language |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |