import java.util.HashMap; import java.util.Map; public class Test1 { /** * 我在思考 为什么要用策略模式,只是对行为做一些封装,调用 还是需要根据条件来判断选择哪种行为,经过写一段代码后有了一点小小的心得 * 优点:行为封装 模块化,支持开闭原则,调用很简洁 * 缺点:需要写一大堆行为实现类,并自行选择那种实现类 * 如果结合map或者枚举能实现很优雅的code * 下面有利用策略模式实现的3个小例子分享给大家 * main 常用策略模式 * main1 策略模式与map结合后的 * main2 策略模式与枚举结合后的 */ public static void main1(String[] args) { BookMark b1 = new BookMark(new MenberVVip()); BookMark b2 = new BookMark(new MenberVip()); BookMark b3 = new BookMark(new MenberNoVip()); System.out.println("hello i want bug some book"); System.out.println("hello i am vvip:"+b1.safe(100D,2)); System.out.println("hello i am vip:"+b2.safe(100D,2)); System.out.println("hello i am novip:"+b3.safe(100D,2)); } public static void main2(String[] args) { BookMarkplus b = new BookMarkplus(); System.out.println("hello i want bug some bookplus"); System.out.println("hello i am vvip:"+b.safe("vvip",100D,2)); System.out.println("hello i am vip:"+b.safe("vip",100D,2)); System.out.println("hello i am novip:"+b.safe("novip",100D,2)); } public static void main(String[] args) { System.out.println("hello i want bug some bookplus"); System.out.println("hello i am vvip:"+SafeBookEnum.getEnumByType("vvip").safeBook.safe(100D,2)); System.out.println("hello i am vip:"+SafeBookEnum.getEnumByType("vip").safeBook.safe(100D,2)); System.out.println("hello i am novip:"+SafeBookEnum.getEnumByType("novip").safeBook.safe(100D,2)); } } class BookMark{ SafeBook safeBook ; BookMark(){} BookMark(SafeBook safeBook){ this.safeBook = safeBook ; } public Double safe(Double price, Integer count){ return this.safeBook.safe(price,count); } } class BookMarkplus{ public static Map<String,SafeBook> c = new HashMap<>(); static { c.put("vvip",new MenberVVip()); c.put("vip",new MenberVip()); c.put("novip",new MenberNoVip()); } public Double safe( String type, Double price, Integer count){ return c.get(type).safe(price,count) ; } } class BookMarkplusplus{ public static Map<String,SafeBook> c = new HashMap<>(); static { c.put("vvip",new MenberVVip()); c.put("vip",new MenberVip()); c.put("novip",new MenberNoVip()); } public Double safe( String type, Double price, Integer count){ return c.get(type).safe(price,count) ; } } abstract class SafeBook{ abstract Double safe(Double price ,Integer count); } class MenberVVip extends SafeBook { @Override Double safe(Double price, Integer count) { return price*count*0.8; } } class MenberVip extends SafeBook { @Override Double safe(Double price, Integer count) { return price*count*0.9; } } class MenberNoVip extends SafeBook { @Override Double safe(Double price, Integer count) { return price*count; } } //枚举类的处理 enum SafeBookEnum{ VVIP("vvip",new MenberVVip()), VIP("vip",new MenberVip()), NOVIP("novip",new MenberNoVip()) ; public String menberType ; public SafeBook safeBook ; SafeBookEnum(String menberType ,SafeBook safeBook){ this.menberType = menberType ; this.safeBook = safeBook ; } public String getMenberType() { return menberType; } public void setMenberType(String menberType) { this.menberType = menberType; } public SafeBook getSafeBook() { return safeBook; } public void setSafeBook(SafeBook safeBook) { this.safeBook = safeBook; } public static SafeBookEnum getEnumByType(String type){ for(SafeBookEnum entity : SafeBookEnum.values()){ if(type.equals(entity.getMenberType())){ return entity; } } return null; } }
浅写策略模式,及map、枚举结合小例子
于 2023-01-29 15:29:53 首次发布