《Java与模式》重点总结04

更新时间:2024-05-19 02:17:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

八、代理(Proxy)模式

1、代理模式

(1)示例类图

(2)示例代码清单 /* * 创建日期 2007-3-13 * * TODO 要更改此生成的文件的模板,请转至 * 窗口 - 首选项 - Java - 代码样式 - 代码模板 */ package com.langsin.proxy; // 抽象角色 abstract public class Subject { } abstract public void request(); package com.langsin.proxy; //真实角色:实现了Subject的request()方法 public class RealSubject extends Subject { public void request() { System.out.println(\); public RealSubject() { } } } package com.langsin.proxy; //代理角色 public class ProxySubject extends Subject { } private void postRequest() { } // something you want to do after requesting private void preRequest() { } // something you want to do before requesting } realSubject.request(); // 此处执行真实对象的request方法 postRequest(); if (realSubject == null) { } realSubject = new RealSubject(); public void request() // 该方法封装了真实对象的request方法 { preRequest(); public ProxySubject() { } private RealSubject realSubject; // 以真实角色作为代理角色的属性 package com.langsin.proxy; //客户端调用 public class Client { public static void main(String[] args) { Subject sub = new ProxySubject(); } } sub.request(); 2、动态代理模式

(1)示例类图

(2)示例代码清单

package com.langsin.dynamicproxy; //抽象角色(之前是抽象类,此处应改为接口): public interface Subject { } abstract public void request(); package com.langsin.dynamicproxy; //具体角色 public class RealSubject implements Subject { } public RealSubject() { } public void request() { } System.out.println(\); package com.langsin.dynamicproxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; //代理处理器 /** * 该代理类的内部属性为Object类,实际使用时通过该类的构造函数DynamicSubject(Object obj)对其赋值; * 此外,在该类还实现了invoke方法,该方法中的 method.invoke(sub,args); * 其实就是调用被代理对象的将要被执行的方法,方法参数sub是实际的被代理对象, args为执行被代理对象相应操作所需的参数。 * 通过动态代理类,我们可以在调用之前或之后执行一些相关操作 */ public class DynamicSubject implements InvocationHandler { } } return object; System.out.println(\ + method); public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println(\ + method); Object object=method.invoke(sub, args); public DynamicSubject(Object obj) { } sub = obj; public DynamicSubject() { } private Object sub; package com.langsin.dynamicproxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; //客户端 public class Client { } static public void main(String[] args) throws Throwable { } Subject rs = new RealSubject(); // 在这里指定被代理类 InvocationHandler ds = new DynamicSubject(rs); Class cls = rs.getClass(); // 以下是一次性生成代理 Subject subject = (Subject) Proxy.newProxyInstance( cls.getClassLoader(), cls.getInterfaces(), ds); subject.request();

(3)其它示例类图

九、享元(Flyweight)模式

1、单纯享元模式

(1)示例类图

(2)示例代码清单 package com.javapatterns.flyweight.simple; abstract public class Flyweight { } abstract public void operation(String state); package com.javapatterns.flyweight.simple; public class ConcreteFlyweight extends Flyweight { public void operation(String state) { System.out.print(\ + intrinsicState + \ + state); public ConcreteFlyweight(Character state) { } this.intrinsicState = state; private Character intrinsicState = null; } } package com.javapatterns.flyweight.simple; import java.util.Map; import java.util.HashMap; import java.util.Iterator; public class FlyweightFactory { } public void checkFlyweight() { } Flyweight fly; int i = 0; for (Iterator it = flies.entrySet().iterator(); it.hasNext();) { } System.out.println(\); Map.Entry e = (Map.Entry) it.next(); System.out.println(\ + (++i) + \ + e.getKey()); public synchronized Flyweight factory(Character state) { } if (flies.containsKey(state)) { } return (Flyweight) flies.get(state); Flyweight fly = new ConcreteFlyweight(state); flies.put(state, fly); return fly; } else { public FlyweightFactory() { } private HashMap flies = new HashMap(); /** * @link aggregation * @directed * @clientRole Flyweights */ private Flyweight lnkFlyweight; System.out.println(\); package com.javapatterns.flyweight.simple; public class Client { } } // intrinsic Flyweight factory.checkFlyweight(); fly = factory.factory(new Character('a')); fly.operation(\); fly = factory.factory(new Character('b')); fly.operation(\); Flyweight fly = factory.factory(new Character('a')); fly.operation(\); static public void main(String[] args) { factory = new FlyweightFactory(); private static FlyweightFactory factory; 输出结果: Intrinsic State = a, Extrinsic State = First Call Intrinsic State = b, Extrinsic State = Second Call Intrinsic State = a, Extrinsic State = Third Call ==========checkFlyweight()============= Item 1 : b Item 2 : a

==========checkFlyweight()=============

2、复合享元模式

(1)示例类图

(2)示例代码清单 package com.javapatterns.flyweight.composite; abstract public class Flyweight { } abstract public void operation(String state); package com.javapatterns.flyweight.composite; public class ConcreteFlyweight extends Flyweight { public void operation(String state) { } System.out.print(\ + intrinsicState + \ + state); public ConcreteFlyweight(Character state) { } this.intrinsicState = state; private Character intrinsicState = null; } package com.javapatterns.flyweight.composite; import java.util.Map; import java.util.HashMap; import java.util.Iterator; public class ConcreteCompositeFlyweight extends Flyweight { } } } fly.operation(extrinsicState); for (Iterator it = flies.entrySet().iterator(); it.hasNext();) { Map.Entry e = (Map.Entry) it.next(); fly = (Flyweight) e.getValue(); public void operation(String extrinsicState) { Flyweight fly = null; public void add(Character key, Flyweight fly) { } flies.put(key, fly); public ConcreteCompositeFlyweight() { } /** * @link aggregation * @directed * @clientRole Composite */ private Flyweight flyweight; private HashMap flies = new HashMap(10); package com.javapatterns.flyweight.composite; import java.util.Map; import java.util.HashMap; import java.util.Iterator;

public class FlyweightFactory { System.out.println(\); public void checkFlyweight() { Flyweight fly; int i = 0; public Flyweight factory(Character state) { } if (flies.containsKey(state)) { } return (Flyweight) flies.get(state); Flyweight fly = new ConcreteFlyweight(state); flies.put(state, fly); return fly; } else { } for (int i = 0; i < length; i++) { } return compositeFly; state = new Character(compositeState.charAt(i)); System.out.println(\ + state + \); compositeFly.add(state, this.factory(state)); int length = compositeState.length(); Character state = null; public Flyweight factory(String compositeState) { ConcreteCompositeFlyweight compositeFly = new public FlyweightFactory() { } private HashMap flies = new HashMap(); /** * @link aggregation * @directed * @clientRole Flyweights */ private Flyweight lnkFlyweight; ConcreteCompositeFlyweight(); } for (Iterator it = flies.entrySet().iterator(); it.hasNext();) { } Map.Entry e = (Map.Entry) it.next(); System.out.println(\ + (++i) + \ + e.getKey()); System.out.println(\); } package com.javapatterns.flyweight.composite; public class Client { } } // intrinsic Flyweight factory.checkFlyweight(); public static void main(String[] args) { factory = new FlyweightFactory(); Flyweight fly; /* * fly = factory.factory(new Character('a')); fly.operation(); * * fly = factory.factory(new Character('b')); fly.operation(); * * fly = factory.factory(new Character('a')); fly.operation(); */ fly = factory.factory(\); fly.operation(\); private static FlyweightFactory factory;

十、门面(Facade)模式

(1)示例类图

(2)示例代码清单

package com.javapatterns.facade.securityfacade; public class SecurityFacade { private Alarm alarm; public SecurityFacade(){ } public void activate() { camera1=new Camera(); camera2=new Camera(); light1=new Light(); light2=new Light(); light3=new Light(); sensor=new Sensor(); alarm=new Alarm(); private Sensor sensor; private Light light1, light2, light3; private Camera camera1, camera2; } } camera1.turnOn(); camera2.turnOn(); light1.turnOn(); light2.turnOn(); light3.turnOn(); sensor.activate(); alarm.activate(); public void deactivate() { } camera1.turnOff(); camera2.turnOff(); light1.turnOff(); light2.turnOff(); light3.turnOff(); sensor.deactivate(); alarm.deactivate(); package com.javapatterns.facade.securityfacade; public class Alarm { public void stopRing() { System.out.println(\); } public void ring() { } System.out.println(\); public void deactivate() { } System.out.println(\); public void activate() { } System.out.println(\); } package com.javapatterns.facade.securityfacade; public class Camera { } public void rotate(int degrees) { } System.out.println(\ + degrees + \public void turnOff() { } System.out.println(\); public void turnOn() { System.out.println(\); } degrees.\); package com.javapatterns.facade.securityfacade; public class Light { } public void changeBulb() { } System.out.println(\); public void turnOff() { System.out.println(\); } public void turnOn() { } System.out.println(\); package com.javapatterns.facade.securityfacade; public class Sensor { public void activate() { } System.out.println(\);

} public void trigger() { } System.out.println(\); public void deactivate() { } System.out.println(\); package com.javapatterns.facade.securityfacade; public class Client { } public static void main(String[] args) { security = new SecurityFacade(); security.activate(); } /** * @link aggregation * @directed */ private static SecurityFacade security; 十一、桥梁(Bridge)模式:又称桥接模式或柄体模式或接口模式

(1)示例类图

(2)示例代码清单

package com.javapatterns.bridge.imageviewer; abstract public class ImageAbstraction { } /** * @link aggregation * @directed * @supplierRole imp */ protected ImageImpl imp; public abstract void show(); public abstract void load();

package com.javapatterns.bridge.imageviewer; public class BmpImageAbstraction extends ImageAbstraction { } public void show() { } // some code here to display the image onto screen public void load() { } // some code here to load the image into memory

package com.javapatterns.bridge.imageviewer; public class JpgImageAbstraction extends ImageAbstraction { } public void show() { } // some code here to display the image onto screen public void load() { } // some code here to load the image into memory

package com.javapatterns.bridge.imageviewer; abstract public class ImageImpl { } public long width; public long height; public byte[] data; public abstract void paint(); public abstract void init();

package com.javapatterns.bridge.imageviewer; public class WindowImageImpl extends ImageImpl { } public void paint() { } // display the image onto screen. public void init() { } // load the info of the image

package com.javapatterns.bridge.imageviewer; public class LinuxImageImpl extends ImageImpl { } public void paint() { } // display the image onto screen. public void init() { } // load the info of the image

本文来源:https://www.bwwdw.com/article/oym7.html

Top