Home > Java > javaTutorial > body text

Java Design Patterns: Detailed explanation of strategy patterns and implementation methods

WBOY
Release: 2023-04-24 22:55:06
forward
1366 people have browsed it

1. What is the Strategy Pattern

Define a series of algorithms, encapsulate them one by one, and make them interchangeable.

In layman’s terms, there are multiple methods or strategies to achieve the same function. These methods or strategies can be extracted and encapsulated. Whichever method needs to be used, just pass in the corresponding object.

2. How to implement

1) Define a strategy interface, which defines several abstract methods.

2) Specific strategies, implement the strategy interface, and implement the methods defined in the interface.

3) Encapsulate the calling class and hold a reference to the strategy interface. When the user needs to complete a certain function, he only needs to pass the corresponding strategy object to this class and call the method.

3. Code implementation

Take file encryption and decryption as an example.

1) Strategy interface, define the strategy name

/**
 * 策略接口
 */
public interface EncodeStrategy {
    /**
     * 加密算法
     * @param file
     */
    public abstract void encryptFile(File file);
    /**
     * 解密算法
     * @param file
     * @return
     */
    public abstract String decryptFile(File file);
}
Copy after login

2) Specific strategy class 1

/**
 * 具体策略
 */
public class AESEncode implements EncodeStrategy {
    @Override
    public void encryptFile(File file) {
        //省略具体细节
    }
    @Override
    public String decryptFile(File file) {
        //省略具体细节
        return null;
    }
}
Copy after login

Specific strategy class 2

public class Base64Encode implements EncodeStrategy {
    @Override
    public void encryptFile(File file) {
        //省略具体细节
    }
    @Override
    public String decryptFile(File file) {
        //省略具体细节
        return null;
    }
}
Copy after login

3) Encapsulation calling class

/**
 * 封装调用类,需要用到具体策略,只需传入其对象即可
 */
public class FileEncode{
    /**
     * 策略接口的引用
     */
    EncodeStrategy strategy;
    public FileEncode(EncodeStrategy strategy){
        this.strategy = strategy;
    }
    //根据传入的具体策略对象,调用其方法
    public void encryptFile(File file) {
        strategy.encryptFile(file);
    }
    //根据传入的具体策略对象,调用其方法
    public String decryptFile(File file) {
        return strategy.decryptFile(file);
    }
}
Copy after login

4) Test code

public class TestStrategy {
    public static void main(String args []){
        File file = new File("d:\test.txt");
        //调用策略1 实现加密
        FileEncode fileEncode = new FileEncode(new AESEncode());
        fileEncode.encryptFile(file);
        //调用策略2 实现加密
        fileEncode = new FileEncode(new Base64Encode());
        fileEncode.encryptFile(file);
    }
}
Copy after login

The above is the detailed content of Java Design Patterns: Detailed explanation of strategy patterns and implementation methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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