Location>code7788 >text

[Java Review and Learn New Series] Basic Knowledge-05 Object Oriented

Popularity:444 ℃/2025-01-19 14:34:04

1. Overview of object-oriented

Object-Oriented (OO for short) is a programming idea. The core idea is to abstract things in the real world into "objects" in the program, and solve problems through the interaction between objects.

object

  • Object is the basic unit of object-oriented programming, includingdata(properties)andbehavior (method)

  • For example, a "car" object can have properties (color, make) and methods (start, stop).

kind

  • A class is a template or blueprint for an object that defines its properties and methods.

  • For example, the "car" class can define properties and behaviors common to all cars, and specific cars are instances of the class.

2. Object-oriented characteristics

Object-oriented has four major characteristics: encapsulation, inheritance, polymorphism, and abstraction; there are also three major characteristics that usually refer to: encapsulation, inheritance, and polymorphism.

1. Encapsulation is to hide the information of a class inside the class and not allow direct access by external programs. Instead, the hidden information can be operated and accessed through the methods of the class. Good encapsulation reduces coupling.

2. Inheritance is to derive a new class from an existing class. The new class inherits the attributes and behaviors of the parent class and can expand new capabilities, greatly increasing the reusability and ease of maintenance of the program. In Java, there is single inheritance, which means that a subclass has only one parent class.

  • Subclasses have non-private properties and methods of the parent class.
  • Subclasses can have their own properties and methods, that is, subclasses can extend parent classes.
  • Subclasses can implement the methods of the parent class in their own way.

3. Polymorphism is the ability of the same behavior to have multiple different manifestations. Change the code that binds the program at runtime without modifying the program code. Three elements to achieve polymorphism: inheritance, rewriting, and upward transformation (parent class reference points to child class object). Only when the above three conditions are met, can we use unified logic implementation code to process different objects in the same inheritance structure, so as to perform different behaviors. Reference variables defined by parent classes or interfaces can point to instance objects of subclasses or concrete implementation classes. Improved program scalability. For Java, its polymorphic implementation mechanism follows one principle: when a superclass object reference variable refers to a subclass object, the type of the referenced object rather than the type of the reference variable determines whose member method is called, but this is called The method must be defined in the super class, that is, the method is overridden by the subclass.

  • Static polymorphism: realized through method overload (overload), which is compile-time polymorphism (also called front binding). The same method has different parameter lists, and different parameters can be made based on the different parameters. deal with.
  • Dynamic polymorphism: also called runtime polymorphism (also called post-binding). Which class instance object will a reference variable point to? Which class implements the method call issued by the reference variable? It must be decided during the running of the program to override the method inheritance and interface of the parent class in multiple subclasses. (implementing the interface and overriding the same method in the interface) can achieve polymorphism. During runtime, the actual type of the referenced object is determined and the corresponding method is called according to its actual type.

4. Abstraction. Abstract objective things using code.

3. Basic principles of object-oriented
  • Single Responsibility Principle SRP (Single Responsibility Principle)

The function of the class must be single and the responsibilities must be clear. It cannot be all-encompassing and should be a universal object (such as a product class. The relevant attributes and methods must be related to the product, and unrelated content such as orders cannot appear. The class here can be a module or class library. , assembly, not just class).

  • Open-Close Principle OCP (Open-Close Principle)

A module is open for expansion and closed for modification.

  • Law of Demeter (LoD)

High cohesion, low coupling. Try not to rely on details. An object should know as little as possible about other objects and only interact with direct friends (i.e. member variables, method parameters, return values, etc.).

  • The Liskov Substitution Principle LSP (the Liskov Substitution Principle LSP)

Subclasses can completely replace parent classes, but not vice versa. Usually used when implementing interfaces. This is also an essential element of polymorphism. Because the subclass can completely replace the base (parent) class, the parent class will have many subclasses, which will be easy to expand in subsequent program expansion. The program can be expanded without modification at all.

  • The Dependency Inversion Principle DIP (the Dependency Inversion Principle DIP)

High-level modules should not depend on low-level modules; both should depend on abstractions; abstractions should not depend on details; details should depend on abstractions

Programming towards abstraction. That is, parameter transfer or return value can use parent class type or interface type. Broadly speaking: based on interface programming, design the interface framework in advance.

  • The Interface Segregation Principle ISP (the Interface Segregation Principle ISP)

It is better to design with multiple interfaces related to specific client classes than to use one general interface. For example, a mobile phone has functions such as making calls, watching videos, and playing games. It is much better to split these functions into different interfaces than to integrate them into one interface.

4. The idea of ​​object-oriented (OOP) in the Spring Boot framework is reflected in

Encapsulation:
Spring Boot defines the role of a class by using annotations such as @Component, @Service, @Repository and @Controller, and encapsulates functionally related code together.
Use the private keyword to restrict direct access to class properties, and provide getter and setter methods to control access to these properties.
Inheritance:
Inheritance can be used in Spring Boot to create components with common behaviors or properties.
You can customize the behavior of your application by extending abstract classes or implementing interfaces provided by the Spring framework.
Polymorphism:
Polymorphism allows you to use a reference of the parent class type to point to an object of the subclass. This can be achieved through dependency injection in Spring Boot. For example, you can inject the implementation of an interface and change the specific implementation at runtime.
Spring Boot supports method overriding, which allows you to customize or modify the behavior of existing methods as needed.
Abstract:
Abstraction is hiding complex implementation details and exposing only the necessary parts to the user.
In Spring Boot, many complex configurations are reduced to simple annotations or automatic configurations, such as the @SpringBootApplication annotation, which hides a lot of underlying initialization work.
Dependency Injection (DI):
Although dependency injection is not a core principle of OOP, it is closely related to OOP and is an important feature of the Spring framework.
With dependency injection, objects are no longer responsible for creating the objects they depend on, but the Spring container is responsible for managing and providing these dependencies. This helps improve code testability and loose coupling.
Programming to an Interface:
The recommended approach is to rely on interfaces rather than specific implementation classes, which makes the code more flexible and easier to extend and maintain.
Spring Boot encourages developers to use interfaces to define service layers, data access layers, etc., and then choose the appropriate implementation at runtime.
Composition:
Composition is the combining of objects into more complex structures without the use of inheritance. Composition is often used in Spring Boot applications to build functional modules, such as by combining different services to complete business logic.
Application of design patterns:
Spring Boot widely applies various design patterns, such as factory pattern, singleton pattern, template method pattern, etc., which are the specific embodiment of OOP design principles.