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
}
}