Strategy Pattern - dnwls16071/Backend_Summary GitHub Wiki

πŸ“š Strategy Pattern μ„€λͺ…

  • νŠΉμ • μž‘μ—…μ„ μ²˜λ¦¬ν•˜λŠ” 방식듀, 이λ₯Έλ°” μ „λž΅λ“€μ„ μ—¬λŸΏ 두고 ν•„μš”μ— 따라 κ°ˆμ•„λΌμ›Œ μ‚¬μš©ν•˜λŠ” νŒ¨ν„΄
  • μ–΄λ–€ 일을 ν•˜λŠ” 것에 λŒ€ν•œ μ—¬λŸ¬ λͺ¨λ“œλ“€μ„ 각각의 클래슀둜 μ •μ˜ν•˜κ³  이듀을 ν•„μš”μ— 따라 μ„ νƒν•΄μ„œ μ“°λŠ” 것

[Ex1]

// μ§€λΆˆμ„ μœ„ν•œ μΈν„°νŽ˜μ΄μŠ€ μ„ μ–Έ
interface PaymentStrategy {
    void pay(int amount);
}
// μ§€λΆˆ μ „λž΅ μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•œ ν•˜λ‚˜μ˜ 방법인 μ‹ μš©μΉ΄λ“œ μ§€λΆˆ 클래슀
class CreditCardPayment implements PaymentStrategy {
    private String name;
    private String cardNumber;

    public CreditCardPayment(String name, String cardNumber) {
        this.name = name;
        this.cardNumber = cardNumber;
    }

    @Override
    public void pay(int amount) {
        System.out.println(amount + " paid with credit card");
    }
}

// μ§€λΆˆ μ „λž΅ μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•œ 또 ν•˜λ‚˜μ˜ 방법인 νŽ˜μ΄νŒ” μ§€λΆˆ 클래슀
class PayPalPayment implements PaymentStrategy {
    private String email;

    public PayPalPayment(String email) {
        this.email = email;
    }

    @Override
    public void pay(int amount) {
        System.out.println(amount + " paid using PayPal");
    }
}
public class Main {
    public static void main(String[] args) {
        ShoppingCart cart = new ShoppingCart();

        // setter λ©”μ„œλ“œλ₯Ό 톡해 μ „λž΅μ„ 선택
        cart.setPaymentStrategy(
                new CreditCardPayment(
                        "John Doe",
                        "1234567890123456"
                )
        );
        cart.checkout(100);
        //100 paid with credit card

        // setter λ©”μ„œλ“œλ₯Ό 톡해 μ „λž΅μ„ 선택
        cart.setPaymentStrategy(
                new PayPalPayment(
                        "[email protected]"
                )
        );
        cart.checkout(200);
        // 200 paid using PayPal
    }
}

[Ex2]

// λ¬Έμžμ—΄ λ³€ν™˜ μ•Œκ³ λ¦¬μ¦˜μ„ μ„ μ–Έν•œ μΈν„°νŽ˜μ΄μŠ€
interface CompressionStrategy {
    String compress(String data);
}
// λ¬Έμžμ—΄μ˜ 각 λ¬Έμžκ°€ μ—°μ†λ˜λŠ” 횟수 기반으둜 λ³€ν™˜
class RunLengthEncoding implements CompressionStrategy {
    @Override
    public String compress(String data) {
        StringBuilder compressed = new StringBuilder();
        int count = 1;
        for (int i = 1; i <= data.length(); i++) {
            if (i < data.length() && data.charAt(i) == data.charAt(i - 1)) {
                count++;
            } else {
                compressed.append(data.charAt(i - 1));
                compressed.append(count);
                count = 1;
            }
        }
        return compressed.toString();
    }
}

// λ¬Έμžμ—΄μ˜ 각 λͺ¨μŒμ„ μ •ν•΄μ§„ 숫자둜 λ³€ν™˜
class SimpleReplacementCompression implements CompressionStrategy {
    @Override
    public String compress(String data) {
        return data.replace("a", "1")
                   .replace("e", "2")
                   .replace("i", "3")
                   .replace("o", "4")
                   .replace("u", "5");
    }
}
class Compressor {
    private CompressionStrategy strategy;  // μΈν„°νŽ˜μ΄μŠ€λ₯Ό ν•„λ“œλ‘œ μ„ μ–Έ

    // setter λ©”μ„œλ“œμ— μΈν„°νŽ˜μ΄μŠ€λ₯Ό νŒŒλΌλ―Έν„°λ‘œ 전달
    public void setCompressionStrategy(CompressionStrategy strategy) {
        this.strategy = strategy;
    }

    // λ¬Έμžμ—΄ νŒŒλΌλ―Έν„° 전달
    public String compress(String data) {
        return strategy.compress(data);
    }
}
// Client code
public class Main {
    public static void main(String[] args) {
        Compressor compressor = new Compressor();  //
        String data = "aabcccccaaa";

        compressor.setCompressionStrategy(new RunLengthEncoding());
        System.out.println("RLE Compression: " + compressor.compress(data));
        // RLE Compression: a2b1c5a3

        compressor.setCompressionStrategy(new SimpleReplacementCompression());
        System.out.println("Simple Replacement: " + compressor.compress(data));
        // Simple Replacement: 11bccccc111
    }
}