Skip to content

Commit e44909a

Browse files
committed
Update REDME
1 parent 7f469a7 commit e44909a

File tree

1 file changed

+213
-0
lines changed

1 file changed

+213
-0
lines changed

README.md

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,219 @@ public interface DisplayElement {
14041404
* 享元模式有点类似于单例模式,都是只生成一个对象来被共享使用。这里有个问题,那就是对共享对象的修改,为了避免出现这种情况,我们将这些对象的公共部分,或者说是不变化的部分抽取出来形成一个对象。这个对象就可以避免到修改的问题。
14051405
* 享元的目的是为了减少不会要额内存消耗,将多个对同一对象的访问集中起来,不必为每个访问者创建一个单独的对象,以此来降低内存的消耗。
14061406

1407+
```java
1408+
/**
1409+
* <html>
1410+
* <body>
1411+
* <P> Copyright JasonInternational</p>
1412+
* <p> All rights reserved.</p>
1413+
* <p> Created on 2018年10月18日 下午2:22:23</p>
1414+
* <p> Created by Jason </p>
1415+
* </body>
1416+
* </html>
1417+
*/
1418+
package cn.ucaner.pattern.structure.flyweight.flyweightAbs;
1419+
1420+
/**
1421+
* @Package:cn.ucaner.pattern.structure.flyweight.flyweightAbs
1422+
* @ClassName:Shape
1423+
* @Description: <p> Shape </p>
1424+
* @Author: - Jason
1425+
* @CreatTime:2018年10月18日 下午2:22:23
1426+
* @Modify By:
1427+
* @ModifyTime: 2018年10月18日
1428+
* @Modify marker:
1429+
* @version V1.0
1430+
*/
1431+
public abstract class Shape {
1432+
1433+
/**
1434+
* 内部状态
1435+
*/
1436+
private String intrinsic;
1437+
1438+
/**
1439+
* @Description: 假如我们有一个绘图的应用程序,通过它我们可以出绘制各种
1440+
* 各样的形状、颜色的图形,那么这里形状和颜色就是内部状态了
1441+
* 通过享元模式我们就可以实现该属性的共享了
1442+
* @Autor: Jason
1443+
*/
1444+
public abstract void draw();
1445+
1446+
1447+
public String getIntrinsic() {
1448+
return intrinsic;
1449+
}
1450+
1451+
public void setIntrinsic(String intrinsic) {
1452+
this.intrinsic = intrinsic;
1453+
}
1454+
1455+
}
1456+
1457+
1458+
/**
1459+
* <html>
1460+
* <body>
1461+
* <P> Copyright JasonInternational</p>
1462+
* <p> All rights reserved.</p>
1463+
* <p> Created on 2018年10月18日 下午2:23:26</p>
1464+
* <p> Created by Jason </p>
1465+
* </body>
1466+
* </html>
1467+
*/
1468+
package cn.ucaner.pattern.structure.flyweight;
1469+
1470+
import cn.ucaner.pattern.structure.flyweight.flyweightAbs.Shape;
1471+
1472+
/**
1473+
* @Package:cn.ucaner.pattern.structure.flyweight
1474+
* @ClassName:Circle
1475+
* @Description: <p> Circle </p>
1476+
* @Author: - Jason
1477+
* @CreatTime:2018年10月18日 下午2:23:26
1478+
* @Modify By:
1479+
* @ModifyTime: 2018年10月18日
1480+
* @Modify marker:
1481+
* @version V1.0
1482+
*/
1483+
public class Circle extends Shape{
1484+
1485+
private String color;
1486+
1487+
public Circle(String color){
1488+
this.color = color;
1489+
}
1490+
1491+
@Override
1492+
public void draw() {
1493+
System.out.println("Draw a Circle Which Color is " + color +".");
1494+
}
1495+
1496+
}
1497+
1498+
1499+
/**
1500+
* <html>
1501+
* <body>
1502+
* <P> Copyright JasonInternational</p>
1503+
* <p> All rights reserved.</p>
1504+
* <p> Created on 2018年10月18日 下午2:24:40</p>
1505+
* <p> Created by Jason </p>
1506+
* </body>
1507+
* </html>
1508+
*/
1509+
package cn.ucaner.pattern.structure.flyweight;
1510+
1511+
import java.util.HashMap;
1512+
1513+
import cn.ucaner.pattern.structure.flyweight.flyweightAbs.Shape;
1514+
1515+
/**
1516+
* @Package:cn.ucaner.pattern.structure.flyweight
1517+
* @ClassName:DrawFactory
1518+
* @Description: <p> DrawFactory </p>
1519+
* @Author: - Jason
1520+
* @CreatTime:2018年10月18日 下午2:24:40
1521+
* @Modify By:
1522+
* @ModifyTime: 2018年10月18日
1523+
* @Modify marker:
1524+
* @version V1.0
1525+
*/
1526+
public class DrawFactory {
1527+
1528+
/**
1529+
* 定义一个池容器 - 享元池
1530+
*/
1531+
private static HashMap<String,Shape> colorsPool = new HashMap<String,Shape>();
1532+
1533+
/**
1534+
* @Description: 获取对象
1535+
* @param color
1536+
* @return Shape
1537+
* @Autor: Jason
1538+
*/
1539+
public static Shape getShape(String color){
1540+
1541+
/**
1542+
* 需要返回的对象
1543+
*/
1544+
Shape shape = null;
1545+
1546+
if(colorsPool.containsKey(color)){
1547+
shape = colorsPool.get(color); //外部状态
1548+
}else {
1549+
shape = new Circle(color); //如果不存在的话创建 放入池子中
1550+
colorsPool.put(color,shape);
1551+
}
1552+
return shape;
1553+
}
1554+
1555+
/**
1556+
* @Description: 获取池的大小
1557+
* @return int 池子大小
1558+
* @Autor: Jason
1559+
*/
1560+
public static int getPoolSize(){
1561+
return colorsPool.size();
1562+
}
1563+
1564+
}
1565+
1566+
1567+
/**
1568+
* <html>
1569+
* <body>
1570+
* <P> Copyright JasonInternational</p>
1571+
* <p> All rights reserved.</p>
1572+
* <p> Created on 2018年10月18日 下午2:28:01</p>
1573+
* <p> Created by Jason </p>
1574+
* </body>
1575+
* </html>
1576+
*/
1577+
package cn.ucaner.pattern.structure.flyweight;
1578+
1579+
import cn.ucaner.pattern.structure.flyweight.flyweightAbs.Shape;
1580+
1581+
/**
1582+
* @Package:cn.ucaner.pattern.structure.flyweight
1583+
* @ClassName:DrawMain
1584+
* @Description: <p> DrawMain </p>
1585+
* @Author: - Jason
1586+
* @CreatTime:2018年10月18日 下午2:28:01
1587+
* @Modify By:
1588+
* @ModifyTime: 2018年10月18日
1589+
* @Modify marker:
1590+
* @version V1.0
1591+
*/
1592+
public class DrawMain {
1593+
1594+
public static void main(String[] args) {
1595+
1596+
Shape shape1 = DrawFactory.getShape("RED");
1597+
Shape shape2 = DrawFactory.getShape("GREY");
1598+
Shape shape3 = DrawFactory.getShape("GREEN");
1599+
Shape shape4 = DrawFactory.getShape("RED");
1600+
Shape shape5 = DrawFactory.getShape("GREY");
1601+
Shape shape6 = DrawFactory.getShape("GREY");
1602+
1603+
shape1.draw();
1604+
shape2.draw();
1605+
shape3.draw();
1606+
shape4.draw();
1607+
shape5.draw();
1608+
shape6.draw();
1609+
1610+
System.out.println("一共绘制了" + DrawFactory.getPoolSize() + "种颜色的圆形.");
1611+
}
1612+
1613+
}
1614+
1615+
1616+
1617+
1618+
1619+
```
14071620

14081621

14091622
### FAQ

0 commit comments

Comments
 (0)