









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Challenging major OOP exercises
Typology: Exercises
1 / 15
This page cannot be seen from the preview
Don't miss anything!










SOLID principles are object-oriented design concepts relevant to software development. SOLID is an acronym for five other class-design principles: S ingle Responsibility Principle, O pen-Closed Principle, L iskov Substitution Principle, I nterface Segregation Principle, and D ependency Inversion Principle. Principle Description Single Responsibility Principle Each class should be responsible for a single part or functionality of the system. Một lớp chỉ nên có 1 vai trò/1 chức năng/1 phần trong tổng thể hệ thống Open-Closed Principle Software components should be open for extension, but not for modification. Khi muốn tạo ra sự thay đổi thì
interface: dùng để mở rộng hệ thống Dependency Inversion Principle High-level modules should not depend on low-level modules, both should depend on abstractions. SOLID is a structured design approach that ensures your software is modular and easy to maintain, understand, debug, and refactor. Following SOLID also helps save time and effort in both development and maintenance. SOLID prevents your code from becoming rigid and fragile, which helps you build long-lasting software.
Every class in Java should have a single job to do. To be precise, there should only be one reason to change a class. Here’s an example of a Java class that does not follow the single responsibility principle (SRP): The Vehicle class has three separate responsibilities: reporting, calculation, and database. By applying SRP, we can separate the above class into three classes with separate responsibilities.
Software entities (e.g., classes, modules, functions) should be open for an extension, but closed for modification. Consider the below method of the class VehicleCalculations: Suppose we now want to add another subclass called Truck. We would have to modify the above class by adding another if statement, which goes against the Open-Closed Principle. A better approach would be for the subclasses Car and Truck to override the calculateValue method:
The Interface Segregation Principle (ISP) states that clients should not be forced to depend upon interface members they do not use. In other words, do not force any client to implement an interface that is irrelevant to them. Suppose there’s an interface for vehicle and a Bike class: As you can see, it does not make sense for a Bike class to implement the openDoors() method as a bike does not have any doors! To fix this, ISP proposes that the interfaces be broken down into multiple, small cohesive interfaces so that no class is forced to implement any interface, and therefore methods, that it does not need.
The Dependency Inversion Principle (DIP) states that we should depend on abstractions (interfaces and abstract classes) instead of concrete implementations (classes). The abstractions should not depend on details; instead, the details should depend on abstractions. Consider the example below. We have a Car class that depends on the concrete Engine class; therefore, it is not obeying DIP.
The code will work, for now, but what if we wanted to add another engine type, let’s say a diesel engine? This will require refactoring the Car class. However, we can solve this by introducing a layer of abstraction. Instead of Car depending directly on Engine, let’s add an interface: Now we can connect any type of Engine that implements the Engine interface to the Car class:
Chiến lược sắp xếp dãy được “tiêm” (inject) vào cho MyDataStructure.
Một bãi giữ xe có quy tắc hoạt động như sau:
Thông tin ghi nhận lúc xe ra khỏi bãi: