Interfaces and abstract classes are used in design patterns for decoupling and extensibility. Interfaces define method signatures, abstract classes provide partial implementation, and subclasses must implement unimplemented methods. In the strategy pattern, the interface is used to define the algorithm, and the abstract class or concrete class provides the implementation, allowing dynamic switching of algorithms. In the observer pattern, interfaces are used to define observer behavior, and abstract or concrete classes are used to subscribe and publish notifications. In the adapter pattern, interfaces are used to adapt existing classes. Abstract classes or concrete classes can implement compatible interfaces, allowing interaction with original code.
The application of interfaces and abstract classes in design patterns in Java
In software design, interfaces and abstract classes are the implementation Key components for decoupling and scalability. They allow different modules to be developed and deployed independently while still maintaining compatibility.
Interface
Abstract class
Application in Design Pattern
Interfaces and abstract classes play a vital role in design patterns, improving the flexibility of the code in the following ways , Reusability and testability:
Strategy pattern: Use interfaces to define a set of algorithms, and use abstract classes or concrete implementations to provide implementations. This allows algorithms to be switched dynamically as needed at runtime.
Observer pattern: Use interfaces to define the behavior of observers and subscribers. Abstract classes or concrete implementations can be used as subscription objects, while observers can register and unregister to receive updates.
Adapter pattern: Use interfaces to adapt existing classes to different interfaces. Abstract classes or concrete implementations can implement compatible interfaces, allowing them to interact with legacy code.
Practical case
Strategy mode:
interface SortingAlgorithm { int[] sort(int[] arr); } abstract class AbstractSortingAlgorithm implements SortingAlgorithm { public void swap(int[] arr, int i, int j) { // 交换 arr 中索引为 i 和 j 的元素 } } class BubbleSort extends AbstractSortingAlgorithm { @Override public int[] sort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - i - 1; j++) { if (arr[j] > arr[j + 1]) { swap(arr, j, j + 1); } } } return arr; } } class QuickSort extends AbstractSortingAlgorithm { @Override public int[] sort(int[] arr) { // 快排算法实现 } } // 使用 SortingAlgorithm algorithm = new BubbleSort(); int[] sortedArr = algorithm.sort(arr);
In this example, SortingAlgorithm
interface definition provides sorting behavior, while BubbleSort
and QuickSort
provide specific implementations. Since they both implement the same interface, they can easily be swapped at runtime as needed.
Observer Pattern:
interface Observer { void update(Observable observable); } abstract class Observable { private List<Observer> observers = new ArrayList<>(); public void addObserver(Observer observer) { observers.add(observer); } public void removeObserver(Observer observer) { observers.remove(observer); } protected void notifyObservers() { for (Observer observer : observers) { observer.update(this); } } } class ConcreteObservable extends Observable { private int state; public void setState(int state) { this.state = state; notifyObservers(); } } class ObserverA implements Observer { @Override public void update(Observable observable) { // 收到通知并根据变化的 state 做出反应 } } // 使用 ConcreteObservable observable = new ConcreteObservable(); ObserverA observerA = new ObserverA(); observable.addObserver(observerA); observable.setState(10); // 通知观察者 state 发生变化
In this example, the Observer
interface defines the behavior of the observer, and Observable
Abstract class provides a mechanism for subscribing and publishing notifications. ConcreteObservable
and ObserverA
are concrete implementations, where ConcreteObservable
manages a list of observers and notifies them of state changes, and ObserverA
can execute based on those changes operate.
The above is the detailed content of Application of interfaces and abstract classes in design patterns in Java. For more information, please follow other related articles on the PHP Chinese website!