《Java与模式》重点总结04
更新时间:2024-05-19 02:17:01 阅读量: 综合文库 文档下载
- java设计模式推荐度:
- 相关推荐
八、代理(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
正在阅读:
《Java与模式》重点总结0405-19
东方村尾学校心理咨询记录表906-09
管理会计概念集锦07-18
如何处理PEP小学英语教材中的语音教学08-07
农村中小金融机构风险管理机制建设指引07-03
2010学校各科计划总结05-25
社科类文章如何提取关键词06-03
帮助大家整体理解课文内容啊05-24
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 重点
- 模式
- 总结
- Java
- 中国烙饼机行业市场前景分析预测年度报告(目录) - 图文
- 高一化学必修1知识点综合总结整理版精心整理
- 船闸液压启闭系统-操作说明书(电气)
- 钢结构施工方案修改版-中化二建
- 机械设计吕宏北京大学出版社课后习题答案
- 化工设计概论复习题(1)
- 2.5厚涂料层EPS项目施工设计方案 - 图文
- 《水工程实验技术》实验指导书(修订201110)
- 羽毛球教程巧发力
- 空气温湿度检测报警系统 - 图文
- 山东大学学生会、研究生会大型活动
- 扬州LED产业情况概览
- 冀教版小学科学二年级上册11、磁极 教案
- 液压凿岩机液压系统设计说明书
- 自定义货币资金表过程
- 科瑞达餐饮2012(酒楼)使用手册 - 图文
- 15秋西电《财务会计》在线作业 答案
- 生命科学导论复习题与答案
- 中级财务管理阶段二
- 带电作业竞赛题库