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.
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 { }
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); }
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); }
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(); }
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(); }
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 to identify all vehicle types.
functional interface to calculate the mileage of the vehicle.
Abstract class to define common functions of the vehicle, such as make and model.
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); }
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!