Context
/** * @author Lean @date:2014-10-17 */ public class Context { public static Strategy strategy; public static void main(String[] args) { strategy=new ConcreteStrategyA(); strategy.calculate(); } } abstract class Strategy{ public abstract void calculate(); } class ConcreteStrategyA extends Strategy{ @Override public void calculate() { System.out.println("ConcreteStrategyA is called !"); } } class ConcreteStrategyB extends Strategy{ @Override public void calculate() { System.out.println("ConcreteStrategyB is called !"); }
IChoiceStrategy
/** * 选择策略 * * @author Lean @date:2014-10-17 */ public abstract class IChoiceStrategy { /** * @return 返回列表名字 */ public abstract String[] getNames(); /** * @return 返回相应码 */ public abstract int getCode(String name); }
/** * @author Lean @date:2014-10-17 */ public class SortChoiceStrategy extends IChoiceStrategy { private HashMap<String, Integer> mSortMap; public String name; public SortChoiceStrategy() { initSortMap(); } private void initSortMap() { mSortMap=new HashMap<String, Integer>(); mSortMap.put("最新上架", 0); mSortMap.put("销量最高", 1); mSortMap.put("价格最高", 2); mSortMap.put("价格最低", 3); name="最新上架"; } @Override public String[] getNames() { Set<String> set=mSortMap.keySet(); Object[] tempObj=set.toArray(); String[] result=new String[tempObj.length]; for (int i = 0; i < tempObj.length; i++) { result[i]=(String) tempObj[i]; } return result; } @Override public int getCode(String name) { return mSortMap.get(name); } }
/** * @author Lean @date:2014-10-17 */ public class StatusChoiceStrategy extends IChoiceStrategy { private HashMap<String, Integer> mStatusMap; public String name; public StatusChoiceStrategy() { initStatusMap(); } private void initStatusMap() { mStatusMap=new HashMap<String, Integer>(); mStatusMap.put("定制中", 1); mStatusMap.put("已完毕", 2); name="定制中"; } @Override public String[] getNames() { Set<String> set=mStatusMap.keySet(); Object[] tempObj=set.toArray(); String[] result=new String[tempObj.length]; for (int i = 0; i < tempObj.length; i++) { result[i]=(String) tempObj[i]; } return result; } @Override public int getCode(String name) { return mStatusMap.get(name); } }*********************************************************** 代理模式:代理和被代理对象相同拥有一样的行为,我们把它封装成一个接口,那么,在被
拦截,如打印日志;(像这样的利用中间层来达到目的的模式还有适配器模式)
/** * @author Lean @date:2014-10-17 */ public abstract class IPerson { public abstract void buyTicket(); }
/** * @author Lean @date:2014-10-17 */ public class RealSeePerson extends IPerson { @Override public void buyTicket() { System.out.println("RealSeePerson get the ticket !"); } }
/** * @author Lean @date:2014-10-17 */ public class BuyTicketPerson extends IPerson{ public RealSeePerson target; private void preBuyTicket(){ //TODO do th. before buy ticket target=new RealSeePerson(); } @Override public void buyTicket() { preBuyTicket(); if (target!=null) { target.buyTicket(); } postBuyTicket(); } public void postBuyTicket(){ //TODO do th. after buy thicket } }
>代理和被代理对象实现共同接口,代理对象被调用时调用被代理对象的托付;
动态代理实现监听:
/** * @author Lean @date:2014-10-17 */ public class VectorProxy implements InvocationHandler { private Object proxyobj; public VectorProxy(Object obj) { proxyobj=obj; } public static Object factor(Object obj){ Class cls=obj.getClass(); return Proxy.newProxyInstance(cls.getClassLoader(),cls.getInterfaces(),new VectorProxy(obj)); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("method:"+method.getName()); if (args!=null) { for (int i = 0; i < args.length; i++) { System.out.println(args[i]+""); } } //反射调用 Object obj=method.invoke(proxyobj, args); System.out.println("*********"); return obj; } public static void main(String[] args) { List v=null; //返回代理对象,并调用代理对象的add方法 v=(List) factor(new Vector(10)); v.add("New"); } }*********************************************************** 合成模式:上面两中模式都是里氏代换原则在方法方面的应用.合成模式使用树结果描写叙述
合成模式分为透明式和安全式
透明:指抽象接口声明了枝叶全部的全部接口方法,在叶子类中。对该方法进行空实现;
/** * @author Lean @date:2014-10-20 */ public interface Component { void sampleOperation(); Composite getComposite(); void add(Component component); void remove(Component component); Enumeration<Component> components(); }
/** * @author Lean @date:2014-10-20 */ public class Composite implements Component { private Vector<Component> componentVector=new Vector<Component>(); @Override public Composite getComposite() { return this; } @Override public void sampleOperation() { Enumeration<Component> enumeration=components(); while (enumeration.hasMoreElements()) { Component component = (Component) enumeration.nextElement(); component.sampleOperation(); } } @Override public void add(Component component) { componentVector.addElement(component); } @Override public void remove(Component component) { componentVector.removeElement(component); } @Override public Enumeration<Component> components() { return componentVector.elements(); } }
/** * @author Lean @date:2014-10-20 */ public class Leaf implements Component { @Override public Composite getComposite() { return null; } @Override public void sampleOperation() { System.out.println(" call leaf here !"); } @Override public void add(Component component) { } @Override public void remove(Component component) { } @Override public Enumeration<Component> components() { return null; } }
安全:指抽象接口仅仅声明叶子全部的方法,树枝类除了继承还包含了自己的管理叶子类方法;典型应用:Android的View,ViewGroup
/** * @author Lean @date:2014-10-20 */ public interface Component { Composite getComposite(); void sampleOperation(); }
/** * @author Lean @date:2014-10-20 */ public class Composite implements Component { private Vector componentVector=new Vector(); @Override public void sampleOperation() { Enumeration enumeration=components(); while (enumeration.hasMoreElements()) { ((Component) enumeration.nextElement()).sampleOperation(); } } @Override public Composite getComposite() { return this; } public Enumeration components(){ return componentVector.elements(); } public void add(Component component){ componentVector.addElement(component); } public void remove(Component component){ componentVector.removeElement(component); } }
/** * @author Lean @date:2014-10-20 */ public class Leaf implements Component { @Override public Composite getComposite() { return null; } @Override public void sampleOperation() { System.out.println("leaf is called !"); } }
由于分支机构基本继承抽象类,在抽象方法只支持类,那里可以换成一个叶子。例如,在安全模式。叶类可以换成棒/多叶;在透明模式下可以互换。这是与里氏代换原则;
***********************************************************