标签:android style blog color io os ar 使用 java
里氏代换原则是指拥有相同基类的两个子类,在使用子类A的地方(方法/属性)可以用子类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 !"); } }
因为枝叶基本继承了抽象类,在一个只支持抽象方法的类中,可以替换成另一个枝叶;比如在安全模式中,叶子类可以替换成枝/叶类;在透明模式中可以互相替换;这就符合了里氏代换原则;
***********************************************************
标签:android style blog color io os ar 使用 java
原文地址:http://blog.csdn.net/qq285016127/article/details/40299589