用最简单的例子说明设计模式(三)之责任链、建造者、适配器、代理模式、享元模式...
public class Chain {public abstract class Handler {private Handler nextHandler;// 当前领导能审批通过的最多天数public int maxDay;protected Handler(int maxDay) {this.maxDay = maxDay;}//设置责任链中下一个处理请求的对象public void setNextHandler(Handler handler) {nextHandler = handler;}protected void handleRequest(int day) {if (day <= maxDay) {reply(day);} else {if (nextHandler != null) {//审批权限不够,继续上报nextHandler.handleRequest(day);} else {System.out.println("没有更高的领导审批了");}}}protected abstract void reply(int day);}class ProjectManager extends Handler {public ProjectManager(int day) {super(day);}@Overrideprotected void reply(int day) {System.out.println(day + "天请假,项目经理直接审批通过");}}class DepartmentManager extends Handler {public DepartmentManager(int day) {super(day);}@Overrideprotected void reply(int day) {System.out.println(day + "天请假,部门经理审批通过");}}class GeneralManager extends Handler {public GeneralManager(int day) {super(day);}@Overrideprotected void reply(int day) {System.out.println(day + "天请假,总经理直接审批通过");}}public static void main(String[] strings) {Chain chain = new Chain();Handler projectManager = chain.new ProjectManager(3);Handler departmentManager = chain.new DepartmentManager(5);Handler generalManager = chain.new GeneralManager(15);//创建职责链projectManager.setNextHandler(departmentManager);departmentManager.setNextHandler(generalManager);//发起请假请求projectManager.handleRequest(4);}
}
public class AlerDialog {private String title;private String message;public AlerDialog(Builder builder) {
// View.inflate()this.title = builder.title;this.message = builder.message;}public static class Builder {private String title;private String message;public Builder setTitle(String title) {this.title = title;return this;}public Builder setMessage(String message) {this.message = message;return this;}public AlerDialog build() {return new AlerDialog(this);}}
}new AlerDialog.Builder().setTitle("").setMessage("").build();
public interface Target {void sampleOperation1();void sampleOperation2();
}
public class Adaptee {public void sampleOperation1() {System.out.println("sampleOperation1");}
}
public class Adapter extends Adaptee implements Target {@Overridepublic void sampleOperation2() {System.out.println("sampleOperation2");}}
public class MyClass {public static void main(String[] args) {Adapter adapter = new Adapter();adapter.sampleOperation1();adapter.sampleOperation2();}
}
public interface Target {void sampleOperation1();void sampleOperation2();
}public class Adaptee {public void sampleOperation1() {System.out.println("sampleOperation1");}
}public class Adapter implements Target {private Adaptee mAdaptee;public Adapter(Adaptee adaptee) {mAdaptee = adaptee;}@Overridepublic void sampleOperation1() {mAdaptee.sampleOperation1();}@Overridepublic void sampleOperation2() {System.out.println("sampleOperation2");}}public class MyClass {public static void main(String[] args) {Adapter adapter =new Adapter(new Adaptee());adapter.sampleOperation1();adapter.sampleOperation2();}
}
这里使用到编程中的一个思想:不要随意去修改别人已经写好的代码或者方法,如果需改修改,可以通过代理的方式来扩展该方法。
/*** subject(抽象主题角色):* 真实主题与代理主题的共同接口。*/
interface Subject {void sellBook();
}/*** ReaISubject(真实主题角色):* 定义了代理角色所代表的真实对象。*/
public class RealSubject implements Subject {@Overridepublic void sellBook() {System.out.println("出版社卖书");}
}/*** Proxy(代理主题角色):* 含有对真实主题角色的引用,代理角色通常在将客* 户端调用传递给真实主题对象之前或者之后执行某些* 操作,而不是单纯返回真实的对象。*/
public class ProxySubject implements Subject {private RealSubject realSubject;@Overridepublic void sellBook() {if (realSubject == null) {realSubject = new RealSubject();}sale();realSubject.sellBook();give();}public void sale() {System.out.println("打折");}public void give() {System.out.println("送优惠券");}
}public class Main {public static void main(String[] args) {//静态代理(我们自己静态定义的代理类)ProxySubject proxySubject = new ProxySubject();proxySubject.sellBook();//动态代理(通过程序动态生成代理类,该代理类不是我们自己定义的。而是由程序自动生成)RealSubject realSubject = new RealSubject();MyHandler myHandler = new MyHandler();myHandler.setProxySubject(realSubject);Subject subject = (Subject) Proxy.newProxyInstance(realSubject.getClass().getClassLoader(),realSubject.getClass().getInterfaces(), myHandler);subject.sellBook();}
}public class MyHandler implements InvocationHandler {private RealSubject realSubject;public void setProxySubject(RealSubject realSubject) {this.realSubject = realSubject;}/*** @param proxy 指代我们所代理的那个真实对象* @param method 指代的是我们所要调用真实对象的某个方法的Method对象* @param args 指代的是调用真实对象某个方法时接受的参数* @return* @throws Throwable*/@Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {sale();proxy = method.invoke(realSubject, args);give();return proxy;}public void sale() {System.out.println("打折");}public void give() {System.out.println("送优惠券");}
}
public class FlyWeight {static class MyString {private String myChar;public MyString(String myChar) {this.myChar = myChar;}public void display() {System.out.println(myChar);}}static class MyCharacterFactory {private Map<String, MyString> pool;public MyCharacterFactory() {pool = new HashMap<>();}public MyString getMyCharacte(String strig) {MyString myString = pool.get(strig);if (myString == null) {myString = new MyString(strig);pool.put(strig, myString);}return myString;}}public static void main(String[] args) {MyCharacterFactory myCharacterFactory = new MyCharacterFactory();MyString a = myCharacterFactory.getMyCharacte("a");MyString b = myCharacterFactory.getMyCharacte("b");MyString a1 = myCharacterFactory.getMyCharacte("a");MyString d = myCharacterFactory.getMyCharacte("d");if (a == a1) {System.out.println("true");}}
}
相关源码:https://github.com/peiniwan/DesignPattern.git
转载于:https://www.cnblogs.com/sixrain/p/9021641.html
用最简单的例子说明设计模式(三)之责任链、建造者、适配器、代理模式、享元模式...相关推荐
- 软件设计模式--第三章 结构型模式--享元模式
目录 第二章 结构型模式 1.结构型模式概述 2.享元模式 (1)模式的定义与特点 (2)模式的结构与实现(Flyweight ) (3)应用场景 (4)扩展 第二章 结构型模式 1.结构型模式概述 ...
- 设计模式-04.02-结构型设计模式-门面模式组合模式享元模式
文章目录 门面模式(外观模式)[不常用] 门面模式的原理与实现 Demo案例-影院管理 传统方案 门面模式代码 TheaterLight Stereo Screen Projector Popcorn ...
- Java设计模式(装饰者模式-组合模式-外观模式-享元模式)
Java设计模式Ⅳ 1.装饰者模式 1.1 装饰者模式概述 1.2 代码理解 2.组合模式 2.1 组合模式概述 2.2 代码理解 3.外观模式 3.1 外观模式概述 3.2 代码理解 4.享元模式 ...
- Day314.外观模式享元模式 -Java设计模式
外观模式 外观类就是总控开关,去管理聚合的所有类 一.影院管理项目 组建一个家庭影院: DVD 播放器.投影仪.自动屏幕.环绕立体声.爆米花机,要求完成使用家庭影院的功能,其过程为: 直接用遥控器:统 ...
- 设计模式(C++实现)(二十三)——享元模式
示例问题: 一个制造杯子的厂商,主要生产大.中.小圆柱形杯子(这3种规格的杯子上印有各种各样的文字),以及特殊造型杯子1,特殊造型杯子2.现需要定制模具来生产杯子,用代码解决该问题. 分析: 由于大. ...
- 【Java设计模式】五、5.7 结构型模式——享元模式
5.7 享元模式 5.7.1 概述 定义: 运用共享技术来有效地支持大量细粒度对象的复用.它通过共享已经存在的对象来大幅度减少需要创建的对象数量.避免大量相似对象的开销,从而提高系统资源的利用率. ...
- 设计模式 — 结构型模式 — 享元模式
目录 文章目录 目录 享元模式 应用场景 代码示例 享元模式 享元,可理解为 Python 中的元类.最小粒度的类,系统中存在大量的相似对象时,可以选择享元模式提高资源利用率. 享元具有两种状态: 内 ...
- 【设计模式】 - 结构型模式 - 享元模式
目录标题 前言 享元模式 概述 结构 实现:俄罗斯方块 优缺点和使用场景 JDK源码解析:Integer 前言 结构型模式描述如何将类或对象按某种布局组成更大的结构.它分为类结构型模式和对象结构型模式 ...
- JavaScript设计模式(三):结构型设计模式-外观模式、适配器模式、代理模式、装饰者模式、桥接模式、组合模式、享元模式
JavaScript设计模式 - 结构型设计模式 套餐服务-外观模式 外观模式(Facade) 水管弯弯-适配器模式 适配器模式(Adapter) 适配异类框架 参数适配 牛郎织女-代理模式 代理模式 ...
- 享元模式 FlyWeight 结构型 设计模式(十五)
享元模式(FlyWeight) "享"取"共享"之意,"元"取"单元"之意. 意图 运用共享技术,有效的支持大量细粒度 ...
最新文章
- Windows Phone开发(39):漫谈关键帧动画上篇 转:http://blog.csdn.net/tcjiaan/article/details/7550506...
- CF438E The Child and Binary Tree(有意思的生成函数 + 多项式求逆 + 多项式开方)
- 【网络流24题】解题报告:E 、圆桌问题(最大流求二分图多重匹配)
- R语言dataframe数据列中的缺失值NA的个数统计实战:特定数据列的NA值统计、所有特征的NA值统计
- 中北大学c语言程序设计作业答案,2016年中北大学软件学院程序设计基础考研复试题库...
- 大众点评开源监控CAT概览
- 半吊子菜鸟学Web开发 -- PHP学习 4 --异常
- PON:EPON/GPON/10G PON/XG-PON/NG-PON2—Vecloud微云
- 《数字短片创作(修订版)》——第一部分 剧本创作 第1章 数字短片创意技法 剧本创作的构思...
- Python爬虫(二十一)_Selenium与PhantomJS
- Latex学习笔记 (8) 字体样式之衬线字体与无衬线体字体
- 高效率16KW三相PFC程序方案
- vue实现可拖拽div
- tiny4412移植U-Boot 2020.07
- 2021年广东专精特新中小企业补助及小巨人企业补贴
- 采云端采云链:从订单协同到采购供应链,让采购供应链互联互通
- 解决easyui-tabs 适应ie窗口大小显示不全的问题
- ThinkPHP3.2短信验证码 (创蓝253短信验证码)
- rtthread + STM32F407VE + esp8266 +SD卡 从网络下载文件存到SD卡中
- TRS_WCM(拓尔思信息技术有限公司)内容协作平台平台置标经验攻略