在需要对不同类型的数据集进行排序时,可以使用策略模式来选择不同的排序算法。
// 策略接口
public interface SortingStrategy {
<T extends Comparable<T>> void sort(List<T> items);
}
// 具体策略类:快速排序
public class QuickSortStrategy implements SortingStrategy {
public <T extends Comparable<T>> void sort(List<T> items) {
// 实现快速排序逻辑
System.out.println("QuickSort strategy is used.");
}
}
// 具体策略类:冒泡排序
public class BubbleSortStrategy implements SortingStrategy {
public <T extends Comparable<T>> void sort(List<T> items) {
// 实现冒泡排序逻辑
System.out.println("BubbleSort strategy is used.");
}
}
// 上下文
public class SortedList {
private SortingStrategy strategy;
public void setSortingStrategy(SortingStrategy strategy) {
this.strategy = strategy;
}
public <T extends Comparable<T>> void sort(List<T> items) {
strategy.sort(items);
}
}
// 客户端代码
public class StrategyPatternExample {
public static void main(String[] args) {
SortedList list = new SortedList();
list.setSortingStrategy(new QuickSortStrategy());
list.sort(new ArrayList<>(Arrays.asList(5, 3, 2, 4, 1)));
list.setSortingStrategy(new BubbleSortStrategy());
list.sort(new ArrayList<>(Arrays.asList(5, 3, 2, 4, 1)));
}
}
电子商务应用程序中,可以根据用户的支付方式选择不同的支付策略。
// 策略接口
public interface PaymentStrategy {
void pay(int amount);
}
// 具体策略类:信用卡支付
public class CreditCardPayment implements PaymentStrategy {
private String cardNumber;
public CreditCardPayment(String cardNumber) {
this.cardNumber = cardNumber;
}
public void pay(int amount) {
System.out.println(amount + " paid with credit card.");
}
}
// 具体策略类:PayPal支付
public class PayPalPayment implements PaymentStrategy {
private String emailId;
public PayPalPayment(String email) {
this.emailId = email;
}
public void pay(int amount) {
System.out.println(amount + " paid using PayPal.");
}
}
// 上下文
public class ShoppingCart {
private PaymentStrategy paymentStrategy;
public void setPaymentStrategy(PaymentStrategy paymentStrategy) {
this.paymentStrategy = paymentStrategy;
}
public void checkout(int amount) {
paymentStrategy.pay(amount);
}
}
// 客户端代码
public class StrategyPatternExample {
public static void main(String[] args) {
ShoppingCart cart = new ShoppingCart();
cart.setPaymentStrategy(new CreditCardPayment("1234567890123456"));
cart.checkout(100);
cart.setPaymentStrategy(new PayPalPayment("myemail@example.com"));
cart.checkout(200);
}
}
在需要压缩文件的应用程序中,可以根据文件类型或用户的选择应用不同的压缩策略。
// 策略接口
public interface CompressionStrategy {
void compressFiles(List<File> files);
}
// 具体策略类:ZIP压缩
public class ZipCompressionStrategy implements CompressionStrategy {
public void compressFiles(List<File> files) {
// 使用ZIP压缩
System.out.println("Using ZIP compression strategy.");
}
}
// 具体策略类:RAR压缩
public class RarCompressionStrategy implements CompressionStrategy {
public void compressFiles(List<File> files) {
// 使用RAR压缩
System.out.println("Using RAR compression strategy.");
}
}
// 上下文
public class CompressionContext {
private CompressionStrategy strategy;
public void setCompressionStrategy(CompressionStrategy strategy) {
this.strategy = strategy;
}
public void createArchive(List<File> files) {
strategy.compressFiles(files);
}
}
// 客户端代码
public class StrategyPatternExample {
public static void main(String[] args) {
CompressionContext ctx = new CompressionContext();
ctx.setCompressionStrategy(new ZipCompressionStrategy());
ctx.createArchive(Collections.singletonList(new File("file1.txt")));
ctx.setCompressionStrategy(new RarCompressionStrategy());
ctx.createArchive(Collections.singletonList(new File("file2.txt")));
}
}
这些示例展示了策略模式在不同场景下的应用。策略模式允许在运行时选择合适的算法或行为,同时也方便未来添加新的策略而不影响现有代码。
更多【策略模式-设计模式:策略模式示例】相关视频教程:www.yxfzedu.com