Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 60e1753

Browse files
Optz flyweight
1 parent 8b4a14a commit 60e1753

File tree

11 files changed

+336
-70
lines changed

11 files changed

+336
-70
lines changed

‎src/cn/ucaner/pattern/Main.java‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/**
22
* <html>
33
* <body>
4-
* <P> Copyright 1994 JsonInternational</p>
4+
* <P> Copyright 1994 JasonInternational </p>
55
* <p> All rights reserved.</p>
66
* <p> Created on 19941115</p>
7-
* <p> Created by Jason</p>
7+
* <p> Created by Jason https://github.com/Jasonandy/patterns </p>
88
* </body>
99
* </html>
1010
*/
@@ -13,17 +13,32 @@
1313
/**
1414
* @Package:cn.ucaner.pattern
1515
* @ClassName:Main
16-
* @Description: <p> example </p>
17-
* @Author: - Jason
18-
* @CreatTime:
16+
* @Description: <p> example - https://github.com/Jasonandy/patterns</p>
17+
* @Author: - Jason
18+
* @CreatTime:2018年10月18日 上午10:02:50
1919
* @Modify By:
20-
* @ModifyTime:
20+
* @ModifyTime: 2018年10月18日
2121
* @Modify marker:
2222
* @version V1.0
2323
*/
2424
public class Main {
2525

2626
public static void main(String[] args) {
27-
System.out.println("Hello World!");
27+
System.out.println("********************************************* https://github.com/Jasonandy/patterns *****************************************");
28+
System.out.println("********************************************* GitHub 开源项目,欢迎Fork关注学习! ***********************************************");
29+
System.out.println("******************************************************************************************************************************");
30+
System.out.println(" ___ ___ ___ ___ ___ ___ \n" +
31+
" / /\\ / /\\ ___ ___ / /\\ / /\\ /__/\\ / /\\ \n" +
32+
" / /::\\ / /::\\ / /\\ / /\\ / /:/_ / /::\\ \\ \\:\\ / /:/_ \n" +
33+
" / /:/\\:\\ / /:/\\:\\ / /:/ / /:/ / /:/ /\\ / /:/\\:\\ \\ \\:\\ / /:/ /\\ \n" +
34+
" / /:/-/:/ / /:/-/::\\ / /:/ / /:/ / /:/ /:/_ / /:/-/:/ _____\\__\\:\\ / /:/ /::\\ \n" +
35+
" /__/:/ /:/ /__/:/ /:/\\:\\ / /::\\ / /::\\ /__/:/ /:/ /\\ /__/:/ /:/___ /__/::::::::\\ /__/:/ /:/\\:\\\n" +
36+
" \\ \\:\\/:/ \\ \\:\\/:/__\\/ /__/:/\\:\\ /__/:/\\:\\ \\ \\:\\/:/ /:/ \\ \\:\\/:::::/ \\ \\:\\--\\--\\/ \\ \\:\\/:/-/:/\n" +
37+
" \\ \\::/ \\ \\::/ \\__\\/ \\:\\ \\__\\/ \\:\\ \\ \\::/ /:/ \\ \\::/--- \\ \\:\\ --- \\ \\::/ /:/ \n" +
38+
" \\ \\:\\ \\ \\:\\ \\ \\:\\ \\ \\:\\ \\ \\:\\/:/ \\ \\:\\ \\ \\:\\ \\__\\/ /:/ \n" +
39+
" \\ \\:\\ \\ \\:\\ \\__\\/ \\__\\/ \\ \\::/ \\ \\:\\ \\ \\:\\ /__/:/ \n" +
40+
" \\__\\/ \\__\\/ \\__\\/ \\__\\/ \\__\\/ \\__\\/ ");
41+
System.out.println("------------------------------------------------------------------------------------------------------------------------------");
42+
System.out.println("-------------------------------------------------- jasonandy@hotmail.com -----------------------------------------------------");
2843
}
2944
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* <html>
3+
* <body>
4+
* <P> Copyright JasonInternational</p>
5+
* <p> All rights reserved.</p>
6+
* <p> Created on 2018年10月18日 下午2:23:26</p>
7+
* <p> Created by Jason </p>
8+
* </body>
9+
* </html>
10+
*/
11+
package cn.ucaner.pattern.structure.flyweight;
12+
13+
import cn.ucaner.pattern.structure.flyweight.flyweightAbs.Shape;
14+
15+
/**
16+
* @Package:cn.ucaner.pattern.structure.flyweight
17+
* @ClassName:Circle
18+
* @Description: <p> Circle </p>
19+
* @Author: - Jason
20+
* @CreatTime:2018年10月18日 下午2:23:26
21+
* @Modify By:
22+
* @ModifyTime: 2018年10月18日
23+
* @Modify marker:
24+
* @version V1.0
25+
*/
26+
public class Circle extends Shape{
27+
28+
private String color;
29+
30+
public Circle(String color){
31+
this.color = color;
32+
}
33+
34+
@Override
35+
public void draw() {
36+
System.out.println("Draw a Circle Which Color is " + color +".");
37+
}
38+
39+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* <html>
3+
* <body>
4+
* <P> Copyright 1994 JsonInternational</p>
5+
* <p> All rights reserved.</p>
6+
* <p> Created on 19941115</p>
7+
* <p> Created by Jason</p>
8+
* </body>
9+
* </html>
10+
*/
11+
package cn.ucaner.pattern.structure.flyweight;
12+
13+
14+
import java.util.Random;
15+
16+
import cn.ucaner.pattern.structure.flyweight.flyweightAbs.Flyweight;
17+
18+
/**
19+
* @Package:cn.ucaner.pattern.structure.flyweight
20+
* @ClassName:ConcreateFlyweight
21+
* @Description: <p> 享元模式 -- 具体享元对象 </p>
22+
* @Author: - Jason
23+
* @CreatTime:2017年10月26日 下午6:10:48
24+
* @Modify By:
25+
* @ModifyTime: 2018年10月18日
26+
* @Modify marker:
27+
* @version V1.0
28+
*/
29+
public class ConcreateFlyweight extends Flyweight{
30+
31+
/**
32+
* ConcreateFlyweight. 传入外部状态
33+
* @param extrinsic 外部状态
34+
*/
35+
public ConcreateFlyweight(String extrinsic) {
36+
super(extrinsic);
37+
}
38+
39+
/**
40+
* 根据外部状态进行逻辑处理
41+
*/
42+
@Override
43+
public void operate() {
44+
System.out.println(Extrinsic + "订单,订单编号"+new Random().nextInt(99999));
45+
}
46+
47+
@Override
48+
public void show() {
49+
System.out.println("内部状态"+getIntrinsic());
50+
}
51+
}

‎src/cn/ucaner/pattern/structure/flyweight/ConcreateFlyweight_1.java‎

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* <html>
3+
* <body>
4+
* <P> Copyright JasonInternational</p>
5+
* <p> All rights reserved.</p>
6+
* <p> Created on 2018年10月18日 下午2:24:40</p>
7+
* <p> Created by Jason </p>
8+
* </body>
9+
* </html>
10+
*/
11+
package cn.ucaner.pattern.structure.flyweight;
12+
13+
import java.util.HashMap;
14+
15+
import cn.ucaner.pattern.structure.flyweight.flyweightAbs.Shape;
16+
17+
/**
18+
* @Package:cn.ucaner.pattern.structure.flyweight
19+
* @ClassName:DrawFactory
20+
* @Description: <p> DrawFactory </p>
21+
* @Author: - Jason
22+
* @CreatTime:2018年10月18日 下午2:24:40
23+
* @Modify By:
24+
* @ModifyTime: 2018年10月18日
25+
* @Modify marker:
26+
* @version V1.0
27+
*/
28+
public class DrawFactory {
29+
30+
/**
31+
* 定义一个池容器 - 享元池
32+
*/
33+
private static HashMap<String,Shape> colorsPool = new HashMap<String,Shape>();
34+
35+
/**
36+
* @Description: 获取对象
37+
* @param color
38+
* @return Shape
39+
* @Autor: Jason
40+
*/
41+
public static Shape getShape(String color){
42+
43+
/**
44+
* 需要返回的对象
45+
*/
46+
Shape shape = null;
47+
48+
if(colorsPool.containsKey(color)){
49+
shape = colorsPool.get(color); //外部状态
50+
}else {
51+
shape = new Circle(color); //如果不存在的话创建 放入池子中
52+
colorsPool.put(color,shape);
53+
}
54+
return shape;
55+
}
56+
57+
/**
58+
* @Description: 获取池的大小
59+
* @return int 池子大小
60+
* @Autor: Jason
61+
*/
62+
public static int getPoolSize(){
63+
return colorsPool.size();
64+
}
65+
66+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* <html>
3+
* <body>
4+
* <P> Copyright JasonInternational</p>
5+
* <p> All rights reserved.</p>
6+
* <p> Created on 2018年10月18日 下午2:28:01</p>
7+
* <p> Created by Jason </p>
8+
* </body>
9+
* </html>
10+
*/
11+
package cn.ucaner.pattern.structure.flyweight;
12+
13+
import cn.ucaner.pattern.structure.flyweight.flyweightAbs.Shape;
14+
15+
/**
16+
* @Package:cn.ucaner.pattern.structure.flyweight
17+
* @ClassName:DrawMain
18+
* @Description: <p> DrawMain </p>
19+
* @Author: - Jason
20+
* @CreatTime:2018年10月18日 下午2:28:01
21+
* @Modify By:
22+
* @ModifyTime: 2018年10月18日
23+
* @Modify marker:
24+
* @version V1.0
25+
*/
26+
public class DrawMain {
27+
28+
public static void main(String[] args) {
29+
30+
Shape shape1 = DrawFactory.getShape("RED");
31+
Shape shape2 = DrawFactory.getShape("GREY");
32+
Shape shape3 = DrawFactory.getShape("GREEN");
33+
Shape shape4 = DrawFactory.getShape("RED");
34+
Shape shape5 = DrawFactory.getShape("GREY");
35+
Shape shape6 = DrawFactory.getShape("GREY");
36+
37+
shape1.draw();
38+
shape2.draw();
39+
shape3.draw();
40+
shape4.draw();
41+
shape5.draw();
42+
shape6.draw();
43+
44+
System.out.println("一共绘制了" + DrawFactory.getPoolSize() + "种颜色的圆形.");
45+
}
46+
47+
}

‎src/cn/ucaner/pattern/structure/flyweight/FlyweightFactory.java‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,37 @@
2626
* @version V1.0
2727
*/
2828
public class FlyweightFactory {
29-
//定义一个池容器
29+
30+
/**
31+
* 定义一个池容器 - 享元池
32+
*/
3033
private static HashMap<String,Flyweight> pool=new HashMap<>();
3134

35+
/**
36+
* @Description: 获取 Flyweight 对象
37+
* @param Extrinsic 外部状态
38+
* @return Flyweight
39+
* @Autor: Jason
40+
*/
3241
public static Flyweight getFlyweight(String Extrinsic){
33-
//需要返回的对象
34-
Flyweight flyweight=null;
42+
/**
43+
* 需要返回的对象
44+
*/
45+
Flyweight flyweight = null;
3546
if(pool.containsKey(Extrinsic)){
36-
flyweight=pool.get(Extrinsic);
47+
flyweight=pool.get(Extrinsic);//外部状态
3748
}else {
38-
flyweight=new ConcreateFlyweight_1(Extrinsic);
49+
flyweight=new ConcreateFlyweight(Extrinsic);//如果不存在的话创建 放入池子中
3950
pool.put(Extrinsic,flyweight);
4051
}
4152
return flyweight;
4253
}
4354

44-
//获取池的大小
55+
/**
56+
* @Description: 获取池的大小
57+
* @return int 池子大小
58+
* @Autor: Jason
59+
*/
4560
public static int getPoolSize(){
4661
return pool.size();
4762
}

‎src/cn/ucaner/pattern/structure/flyweight/FlyweightMain.java‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
/**
1616
* @Package:cn.ucaner.pattern.structure.flyweight
1717
* @ClassName:FlyweightMain
18-
* @Description: <p> 享元模式 --- 享元模式执行类</p>
18+
* @Description: <p> 享元模式 --- 享元模式执行类 - https://www.cnblogs.com/chenssy/p/3330555.html</p>
1919
* @Author: -
2020
* @CreatTime:2017年10月26日 下午6:09:45
2121
* @Modify By:
@@ -25,8 +25,8 @@
2525
*/
2626
public class FlyweightMain {
2727

28-
2928
public static void main(String[] args) {
29+
3030
Flyweight fly1;
3131
Flyweight fly2;
3232
Flyweight fly3;
@@ -35,15 +35,17 @@ public static void main(String[] args) {
3535
Flyweight fly6;
3636

3737
//根据类型创建订单对象
38-
fly1 = FlyweightFactory.getFlyweight("图书");
39-
fly2 = FlyweightFactory.getFlyweight("图书");
40-
fly3 = FlyweightFactory.getFlyweight("图书");
41-
fly4 = FlyweightFactory.getFlyweight("图书");
42-
fly5 = FlyweightFactory.getFlyweight("你懂得");
43-
fly6 = FlyweightFactory.getFlyweight("女神娃娃");
38+
fly1 = FlyweightFactory.getFlyweight("CAR");
39+
fly2 = FlyweightFactory.getFlyweight("BOOK");
40+
fly3 = FlyweightFactory.getFlyweight("HOUSE");
41+
fly4 = FlyweightFactory.getFlyweight("BOOK");
42+
fly5 = FlyweightFactory.getFlyweight("CAR");
43+
fly6 = FlyweightFactory.getFlyweight("AUDI");
4444

4545
//调用
4646
fly1.operate();
47+
//fly1.show();
48+
4749
fly2.operate();
4850
fly3.operate();
4951
fly4.operate();

‎src/cn/ucaner/pattern/structure/flyweight/flyweight.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
| 类名 | 描述 |
2222
| -------------------- | ------- |
2323
| Flyweight | 抽象享元对象 |
24-
| ConcreateFlyweight_1 | 具体享元对象 |
24+
| ConcreateFlyweight | 具体享元对象 |
2525
| FlyweightFactory | 享元工厂 |
2626
| FlyweightMain | 享元模式执行类 |
2727

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /