2. 结构型模式 (7) 享元模式

阅读更多
2. 结构型模式 (7) 享元模式

享元模式(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 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

你可能感兴趣的:(java,设计模式)