Home > Java > javaTutorial > body text

Design pattern practice of interfaces and abstract classes in Java

WBOY
Release: 2024-05-01 09:15:01
Original
422 people have browsed it

Interfaces and abstract class design patterns in Java Interface: Mark interface: Indicates that the class has specific attributes and does not declare methods. Functional interface: declares only one abstract method, available for Lambdas and Streams. Service interface: defines business logic, and implementation class provides specific implementation. Abstract class: Abstract class pattern: defines methods and attributes that cannot be instantiated, and subclasses need to implement abstract methods. Template method pattern: Define operation steps, and subclasses can override some steps.

Java 中接口和抽象类的设计模式实践

Design Pattern Practice of Interfaces and Abstract Classes in Java

Introduction

Interfaces and abstract classes are two important abstraction mechanisms in Java. They can be utilized effectively by following specific design patterns. This article will delve into the different design patterns of interfaces and abstract classes and provide practical code examples.

Interface

##1. Marker Interface

The Marker Interface does not declare any methods and is used to indicate the class Have specific properties. For example:

public interface Vehicle {
}
Copy after login

2. Functional Interface

Functional interface declares only one abstract method. They can be used in Lambdas expressions and the Streams API. For example:

@FunctionalInterface
public interface Calculate {
    int calculate(int a, int b);
}
Copy after login

3. Service Interface

The service interface defines the business logic, while other implementation classes provide the actual implementation. For example:

public interface UserService {
    User getUserById(int id);
    void createUser(User user);
}
Copy after login

Abstract class

1. Abstract class pattern

Abstract classes can define methods and properties, but not be instantiated. Subclasses must implement abstract methods to use abstract classes. For example:

public abstract class Animal {
    public abstract void makeSound();
}
Copy after login

2. Template method pattern

The template method pattern defines the steps of an operation, some of which can be overridden by subclasses. For example:

public abstract class Template {
    public final void execute() {
        step1();
        step2();
        step3();
    }

    protected abstract void step1();
    protected abstract void step2();
    protected abstract void step3();
}
Copy after login

Practical Case

Let us use a practical case to show how to apply the design patterns of interfaces and abstract classes.

Specific Product Question:

Design an application to manage various types of vehicles, each with different functionality.

Solution:

  • Tag interface: Create a Vehicle tag interface to identify all vehicle types.
  • Functional interface: Create a CalculateMileage functional interface to calculate the mileage of the vehicle.
  • Abstract class: Create AbstractVehicle Abstract class to define common functions of the vehicle, such as make and model.
  • Service interface: Create a VehicleService service interface to define the business logic of vehicle management.

Code example

// 标记接口
public interface Vehicle {}

// 功能接口
@FunctionalInterface
public interface CalculateMileage {
    double calculate(Vehicle vehicle);
}

// 抽象类
public abstract class AbstractVehicle implements Vehicle {
    protected String make;
    protected String model;

    public String getMake() {
        return make;
    }

    public String getModel() {
        return model;
    }
}

// 服务接口
public interface VehicleService {
    List<Vehicle> getAllVehicles();
    Vehicle getVehicleById(int id);
    void createVehicle(Vehicle vehicle);
    void deleteVehicle(int id);
}
Copy after login

Conclusion

By utilizing the design patterns of interfaces and abstract classes, you can Implement flexible and scalable solutions in applications. This article provides an overview of the different patterns and their practical applications to help developers better understand and use these abstraction mechanisms.

The above is the detailed content of Design pattern practice of interfaces and abstract classes in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!