概念:减少创建对象的数量,以减少内存占用和提高性能。 属于结构型模式
何时使用:1.系统中有大量对象
2.这些对象可以被一个对象代替使用。
3.系统中这些对象是不可分辨的
4.对象的状态属性大部分可以外部化
创建一个能代表大部分类的一个类
public class Line {
private String name;
private int x1;
private int y1;
private int x2;
private int y2;
public void draw(){
System.out.println("划线:"+name+"======>"+x1+":"+y1+"--------"+x2+":"+y1);
}
public Line(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setX1(int x1) {
this.x1 = x1;
}
public void setY1(int y1) {
this.y1 = y1;
}
public void setX2(int x2) {
this.x2 = x2;
}
public void setY2(int y2) {
this.y2 = y2;
}
}
***创建一个工厂池子 来放统一new出来的类***
class LineFactory {
private static final Map<String,Line> map =new HashMap<>();
public static Line getLine(String key) {
if(map.get(key)==null){
Line line=new Line(key);
map.put(key,line);
}
return map.get(key);
}
}
***测试***
class Test{
private static final String colors[] =
{ "Red", "Green", "Blue", "White", "Black" };
public static void main(String[] args) {
for(int i=0;i<20;i++){
Line line=LineFactory.getLine(getRandomColor());
line.setX1(getRandomX1());
line.setX2(getRandomX2());
line.setY1(getRandomY1());
line.setY1(getRandomY2());
line.draw();
}
}
private static String getRandomColor() {
return colors[(int)(Math.random()*colors.length)];
}
private static int getRandomX1() {
return (int)(Math.random()*100 );
}
private static int getRandomY1() {
return (int)(Math.random()*100);
}
private static int getRandomX2() {
return (int)(Math.floor(Math.random()*100)+100 );
}
private static int getRandomY2() {
return (int)(Math.floor(Math.random()*100)+100);
}
总结:享元模式最重要的就是 hashmap (且 final static),
便于创建出来的对象不会被修改 还能公用
然后自己自定义设置属性值。
测试结果
享元模式变种
享元模式变种
import java.util.*;
//围棋棋子类:抽象享元类
abstract class IgoChessman {
public abstract String getColor();
public void display() {
System.out.println("棋子颜色:" + this.getColor());
}
}
//黑色棋子类:具体享元类
class BlackIgoChessman extends IgoChessman {
public String getColor() {
return "黑色";
}
}
//白色棋子类:具体享元类
class WhiteIgoChessman extends IgoChessman {
public String getColor() {
return "白色";
}
}
//围棋棋子工厂类:享元工厂类,使用单例模式进行设计
class IgoChessmanFactory {
private static IgoChessmanFactory instance = new IgoChessmanFactory();
private static Hashtable ht; //使用Hashtable来存储享元对象,充当享元池
private IgoChessmanFactory() {
ht = new Hashtable();
IgoChessman black,white;
black = new BlackIgoChessman();
ht.put("b",black);
white = new WhiteIgoChessman();
ht.put("w",white);
}
//返回享元工厂类的唯一实例
public static IgoChessmanFactory getInstance() {
return instance;
}
//通过key来获取存储在Hashtable中的享元对象
public static IgoChessman getIgoChessman(String color) {
return (IgoChessman)ht.get(color);
}
}class Client {
public static void main(String args[]) {
IgoChessman black1,black2,black3,white1,white2;
IgoChessmanFactory factory;
//获取享元工厂对象
factory = IgoChessmanFactory.getInstance();
//通过享元工厂获取三颗黑子
black1 = factory.getIgoChessman("b");
black2 = factory.getIgoChessman("b");
black3 = factory.getIgoChessman("b");
System.out.println("判断两颗黑子是否相同:" + (black1==black2));
//通过享元工厂获取两颗白子
white1 = factory.getIgoChessman("w");
white2 = factory.getIgoChessman("w");
System.out.println("判断两颗白子是否相同:" + (white1==white2));
//显示棋子
black1.display();
black2.display();
black3.display();
white1.display();
white2.display();
}
}
//坐标类:外部状态类
class Coordinates {
private int x;
private int y;
public Coordinates(int x,int y) {
this.x = x;
this.y = y;
}
public int getX() {
return this.x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return this.y;
}
public void setY(int y) {
this.y = y;
}
}
//围棋棋子类:抽象享元类
abstract class IgoChessman {
public abstract String getColor();
public void display(Coordinates coord){
System.out.println("棋子颜色:" + this.getColor() + ",棋子位置:" + coord.getX() + "," + coord.getY() );
}
}class Client {
public static void main(String args[]) {
IgoChessman black1,black2,black3,white1,white2;
IgoChessmanFactory factory;
//获取享元工厂对象
factory = IgoChessmanFactory.getInstance();
//通过享元工厂获取三颗黑子
black1 = factory.getIgoChessman("b");
black2 = factory.getIgoChessman("b");
black3 = factory.getIgoChessman("b");
System.out.println("判断两颗黑子是否相同:" + (black1==black2));
//通过享元工厂获取两颗白子
white1 = factory.getIgoChessman("w");
white2 = factory.getIgoChessman("w");
System.out.println("判断两颗白子是否相同:" + (white1==white2));
//显示棋子,同时设置棋子的坐标位置
black1.display(new Coordinates(1,2));
black2.display(new Coordinates(3,4));
black3.display(new Coordinates(1,3));
white1.display(new Coordinates(2,5));
white2.display(new Coordinates(2,4));
}
}