- 浏览: 178187 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (321)
- eclipse (4)
- idea (2)
- Html (8)
- Css (14)
- Javascript (8)
- Jquery (6)
- Ajax Json (4)
- Bootstrap (0)
- EasyUI (0)
- Layui (0)
- 数据结构 (0)
- Java (46)
- DesPattern (24)
- Algorithm (2)
- Jdbc (8)
- Jsp servlet (13)
- Struts2 (17)
- Hibernate (11)
- Spring (5)
- S2SH (1)
- SpringMVC (4)
- SpringBoot (11)
- WebService CXF (4)
- Poi (2)
- JFreeChart (0)
- Shiro (6)
- Lucene (5)
- ElasticSearch (0)
- JMS ActiveMQ (3)
- HttpClient (5)
- Activiti (0)
- SpringCloud (11)
- Dubbo (6)
- Docker (0)
- MySQL (27)
- Oracle (18)
- Redis (5)
- Mybatis (11)
- SSM (1)
- CentOS (10)
- Ant (2)
- Maven (4)
- Log4j (7)
- XML (5)
最新评论
2. 结构型模式 (7) 享元模式
1. 享元模式
享元模式(Flyweight Pattern)主要用于减少创建对象的数量,以减少内存占用和提高性能。 享元模式尝试重用现有的同类对象,如果未找到匹配的对象,则创建新对象。
1. 享元模式
package com.andrew.pattern0207.flyweight.model01; public interface Shape { void draw(); }
package com.andrew.pattern0207.flyweight.model01; public class Circle implements Shape { private String color; private int x; private int y; private int radius; public Circle(String color) { this.color = color; } public void setX(int x) { this.x = x; } public void setY(int y) { this.y = y; } public void setRadius(int radius) { this.radius = radius; } @Override public void draw() { System.out.println("Circle: Draw() [Color : " + color + ", x : " + x +", y :" + y +", radius :" + radius); } }
package com.andrew.pattern0207.flyweight.model01; import java.util.HashMap; public class ShapeFactory { private static final HashMap<String, Shape> circleMap = new HashMap<>(); public static Shape getCircle(String color) { Circle circle = (Circle) circleMap.get(color); if (circle == null) { circle = new Circle(color); circleMap.put(color, circle); System.out.println("Creating circle of color : " + color); } return circle; } }
package com.andrew.pattern0207.flyweight.model01; /** * 1. 享元模式 * * @author andrew * @date 2018/07/21 */ public class Client { private static final String colors[] = {"Red", "Green", "Blue", "White", "Black"}; public static void main(String[] args) { for (int i = 0; i < 20; i++) { Circle circle = (Circle) ShapeFactory.getCircle(getRandomColor()); circle.setX(getRandomX()); circle.setY(getRandomY()); circle.setRadius(100); circle.draw(); } } private static String getRandomColor() { return colors[(int)(Math.random()*colors.length)]; } private static int getRandomX() { return (int)(Math.random()*100 ); } private static int getRandomY() { return (int)(Math.random()*100); } } 运行结果: Creating circle of color : Green Circle: Draw() [Color : Green, x : 88, y :19, radius :100 Creating circle of color : Black Circle: Draw() [Color : Black, x : 6, y :6, radius :100 Creating circle of color : White Circle: Draw() [Color : White, x : 46, y :71, radius :100 Circle: Draw() [Color : Green, x : 60, y :31, radius :100 Circle: Draw() [Color : Green, x : 49, y :99, radius :100 Circle: Draw() [Color : Green, x : 73, y :98, radius :100 Circle: Draw() [Color : Green, x : 10, y :46, radius :100 Creating circle of color : Red Circle: Draw() [Color : Red, x : 32, y :15, radius :100 Circle: Draw() [Color : Red, x : 71, y :28, radius :100 Creating circle of color : Blue Circle: Draw() [Color : Blue, x : 3, y :23, radius :100 Circle: Draw() [Color : Green, x : 39, y :24, radius :100 Circle: Draw() [Color : Blue, x : 67, y :69, radius :100 Circle: Draw() [Color : Blue, x : 64, y :87, radius :100 Circle: Draw() [Color : Green, x : 70, y :42, radius :100 Circle: Draw() [Color : Black, x : 74, y :89, radius :100 Circle: Draw() [Color : Blue, x : 54, y :47, radius :100 Circle: Draw() [Color : White, x : 15, y :0, radius :100 Circle: Draw() [Color : Green, x : 10, y :22, radius :100 Circle: Draw() [Color : Black, x : 34, y :26, radius :100 Circle: Draw() [Color : Green, x : 93, y :98, radius :100
发表评论
-
3. 行为型模式 (11) 解释器模式
2018-11-08 10:39 3733. 行为型模式 (11) 解释器模式 解释器模式(In ... -
3. 行为型模式 (10) 中介者模式
2018-11-08 09:56 3423. 行为型模式 (10) 中介者模式 中介者模式(Me ... -
3. 行为型模式 (9) 访问者模式
2018-11-07 14:30 3473. 行为型模式 (9) 访问者模式 访问者模式(Vis ... -
3. 行为型模式 (8) 状态模式
2018-11-07 09:13 4243. 行为型模式 (8) 状态模式 状态模式(State ... -
3. 行为型模式 (7) 备忘录模式
2018-11-06 14:27 4083. 行为型模式 (7) 备忘录模式 备忘录模式(Mem ... -
3. 行为型模式 (6) 命令模式
2018-11-06 11:40 3553. 行为型模式 (6) 命令模式 命令模式(Comma ... -
3. 行为型模式 (5) 责任链模式
2018-11-06 09:40 4073. 行为型模式 (5) 责任链模式 责任链模式(Cha ... -
3. 行为型模式 (4) 迭代子模式
2018-11-05 14:23 3253. 行为型模式 (4) 迭代 ... -
3. 行为型模式 (3) 观察者模式
2018-11-02 16:17 3983. 行为型模式 (3) 观察者模式 当对象间存在一对多 ... -
3. 行为型模式 (2) 模板方法模式
2018-11-02 09:26 3803. 行为型模式 (2) 模板方法模式 在模板模式(Te ... -
3. 行为型模式 (1) 策略模式
2018-11-01 09:23 4193. 行为型模式 (1) 策略模式 在策略模式(Stra ... -
2. 结构型模式 (3) 代理模式
2018-10-29 11:26 3492. 结构型模式 (3) 代理模式 代理模式(Proxy ... -
2. 结构型模式 (6) 组合模式
2018-10-30 09:50 3422. 结构型模式 (6) 组合模式 组合模式(Compo ... -
2. 结构型模式 (2) 装饰者模式
2018-10-29 09:30 3912. 结构型模式 (2) 装饰器模式 装饰器模式(Dec ... -
2. 结构型模式 (5) 桥接模式
2018-10-30 09:11 3752. 结构型模式 (5) 桥接模式 桥接(Bridge) ... -
2. 结构型模式 (4) 外观模式
2018-10-29 14:20 3312. 结构型模式 (4) 外观模式 外观模式(Fac ... -
2. 结构型模式 (1) 适配器模式
2018-10-26 15:35 3662. 结构型模式 (1) 适配器模式 将一个类的接口转换 ... -
1. 创建型模式 (2) 原型模式
2018-09-12 13:46 4251. 创建型模式 (2) 原型模式 用原型实例指定创建对 ... -
1. 创建型模式 (3) 建造者模式
2018-10-25 16:28 3461. 创建型模式 (3) 建造 ... -
1. 创建型模式 (1) 单例模式
2018-09-12 13:42 4081. 创建型模式 (3) 单例模式 单例模式是为确保一个 ...
相关推荐
电子商务网站常有这样的功能:发送消息通知,比如订货发货通知等,从业务上看,消息分为普通消息、加急消息和特急消息多种不同的消息类型,其业务处理是不一样的,比如加急消息是在消息上添加加急标记.....2....
JAVA-设计模式-结构型模式-享元模式
c++设计模式-结构型模式-享元模式;qt工程;c++简单源码; 享元(Flyweight)模式的定义:运用共享技术来有效地支持大量细粒度对象的复用。它通过共享已经存在的对象来大幅度减少需要创建的对象数量、避免大量相似类...
2.结构型模式:适配器模式、桥接模式、装饰模式、组合模式、外观模式、享元模式、代理模式。 4.行为型模式:模板方法模式、命令模式、迭代器模式、观察者模式、中介者模式、备忘录模式、解释器模式、状态模式、策略...
享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 13. 模板方法(Template Method) 14. 命令模式(Command Pattern) 15. 迭代器模式(Iterator Pattern) 行为型: 16. 观察者模式(Observer Pattern...
设计模式_结构型_享元模式
在这里与各位分享本人从网络上下载的C#面向对象设计模式纵横谈系列视频,共有25节,除了第一节需要各位贡献一点资源分以作为对本人上传资源的回馈,后面的其他资源均不需要... 这是第12节:结构型模式Flyweight享元模式
C#面向对象设计模式纵横谈(12):Flyweight 享元模式(结构型模式) (Level 300)
设计模式面面观(14):享元模式(Facade Pattern)-结构型模式 http://blog.csdn.net/fanweiwei/archive/2008/04/25/2326692.aspx
11.享元模式...................................................64 12.代理模式...................................................71 三.行为型模式....................................................75...
桥接模式之消息发送、适配器模式之购物车商品推荐、装饰者模式之相亲(男人的描述)、组合模式之对购物网站、享元模式之网店模板
软件设计模式2021秋-实验2-结构型模式实验报告附源码和类图.zip 桥接模式、适配器模式、装饰者模式、组合模式、享元模式
11.享元模式...................................................64 12.代理模式...................................................71 三.行为型模式....................................................75...
桥接模式之消息发送、适配器模式之购物车商品推荐、装饰者模式之相亲(男人的描述)、组合模式之对购物网站、享元模式之网店模板
包含5个实验,桥接模式(电子商务网站常有这样的功能:发送消息通知,比如订货...),享元模式(在天猫商城里存在着成天上万的网店,但是天猫所提供的网站模板是一样的,存在许多天猫网店使用同一个网店模板的情况)
原型模式(Prototype)结构型: 6. 适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式...
享元模式(Flyweight Pattern) 12. 代理模式(Proxy Pattern) 13. 模板方法(Template Method) 14. 命令模式(Command Pattern) 15. 迭代器模式(Iterator Pattern) 行为型: 16. 观察者模式(Observer Pattern...
NULL 博文链接:https://jacky-dai.iteye.com/blog/2302888