GoF之享元模式、组合模式详解

享元(Flyweight Pattern)模式
它使用共享物件,用来尽可能减少内存使用量以及分享资讯给尽可能多的相似物件;它适合用于只是因重复而导致使用无法令人接受的大量内存的大量物件。通常物件中的部分状态是可以分享。常见做法是把它们放在外部数据结构,当需要使用时再将它们传递给享元。
优点:
大大减少对象的创建,降低系统的内存,使效率提高。(用HashMap存储这些重用对象)
缺点:
提高了系统的复杂度,需要分离出外部状态和内部状态,而且外部状态具有固有化的性质,不应该随着内部状态的变化而变化,否则会造成系统的混乱。

优使用场景:
1、系统有大量相似对象。
2、需要缓冲池的场景,如String常量池、数据库连接池、缓冲池等等。

具体实现:创建10个不同效果的圆,但相同颜色的圆只需要创建一次便可,相同颜色的只需要引用原有对象,改变其坐标值便可。此种模式下,同一颜色的圆虽然位置不同,但其地址都是同一个,所以说此模式适用于结果注重单一结果的情况。
//创建一个接口
public interface Shape {
void draw();
}

//创建实现接口的实体类
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);
}

}

//创建一个工厂,生成基于给定信息的实体类的对象
public class ShapeFactory{
private static final HashMap<String, Shape> circleMap = new HashMap<>();

public static Shape getCircle(String color) {

//如果找到Circle对象,则返回该对象。
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;
}

}

//使用该工厂,通过传递颜色信息来获取实体类的对象,随机画10个圆。
public class Client {
private static final Stringcolors[] = {“Red”, “Green”, “Blue”, “White”, “Black”};

public staticvoid main(String[]args) {
    for (int i = 0; i < 10; ++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);
}

}
/*output
Creatingcircle of color : Black
Circle:Draw() [Color : Black, x : 36, y :71, radius :100
Creatingcircle of color : Green
Circle:Draw() [Color : Green, x : 27, y :27, radius :100
Creatingcircle of color : White
Circle:Draw() [Color : White, x : 64, y :10, radius :100
Creatingcircle of color : Red
Circle:Draw() [Color : Red, x : 15, y :44, radius :100
Circle:Draw() [Color : Green, x : 19, y :10, radius :100
Circle:Draw() [Color : Green, x : 94, y :32, radius :100
*/

状态(Composite Pattern)模式
将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性。组合就是在一个对象中包含其他对象。
优点:
1、高层模块调用简单。
2、节点自由增加。
缺点:
在使用组合模式时,其叶子和树枝的声明都是实现类,而不是接口,违反了依赖倒置原则。
使用场景:
1、部分、整体场景,如树形菜单,文件、文件夹的管理。

具体实现:展示一个组织中员工的层次结构。创建一个雇员类Employee,该类当作组合模型类。使用 Employee 类来添加部门层次结构,并打印所有员工。
//创建 Employee 类,该类带有 Employee 对象的列表。
public class Employee {
private String name;
private String dept;
private int salary;
private List subordinates;

//构造函数,包含了一个下属对象集合
public Employee(String name, Stringdept, int sal) {
    this.name = name;
    this.dept = dept;
    this.salary = sal;
    subordinates = new ArrayList<Employee>();
}

public void add(Employee e) {
    subordinates.add(e);
}

public void remove(Employee e) {
    subordinates.remove(e);
}

public List<Employee>getSubordinates() {
    return subordinates;
}

public String toString() {
    return ("Employee :[ Name : " + name
            + ", dept : " + dept + ", salary :"
            + salary + " ]");
}

}

//使用 Employee 类来创建和打印员工的层次结构。
public class Clent {
public static void main(String[] args) {
Employee CEO= new Employee(“John”, “CEO”, 30000);

    Employee headSales = newEmployee("Robert", "HeadSales", 20000);

    Employee headMarketing = newEmployee("Michel", "HeadMarketing", 20000);

    Employee clerk1 = new Employee("Laura", "Marketing", 10000);
    Employee clerk2 = new Employee("Bob", "Marketing", 10000);

    Employee salesExecutive1 = newEmployee("Richard", "Sales", 10000);
    Employee salesExecutive2 = newEmployee("Rob", "Sales", 10000);

    CEO.add(headSales);
    CEO.add(headMarketing);

    headSales.add(salesExecutive1);
    headSales.add(salesExecutive2);

    headMarketing.add(clerk1);
    headMarketing.add(clerk2);

    //打印该组织的所有员工
    System.out.println(CEO);
    for (Employee headEmployee : CEO.getSubordinates()) {
        System.out.println(headEmployee);
        for (Employee employee : headEmployee.getSubordinates()) {
           System.out.println(employee);
        }
    }
}

}
/*output
Employee :[Name : John, dept : CEO, salary :30000 ]
Employee :[Name : Robert, dept : Head Sales, salary :20000 ]
Employee :[Name : Richard, dept : Sales, salary :10000 ]
Employee :[Name : Rob, dept : Sales, salary :10000 ]
Employee :[Name : Michel, dept : Head Marketing, salary :20000 ]
Employee :[Name : Laura, dept : Marketing, salary :10000 ]
Employee :[Name : Bob, dept : Marketing, salary :10000 ]
*/

更多技术分享,微信关注“专注一行代码”

https://mp.weixin.qq.com/s/pcNXtDO8pU6snk3iWyTbEg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值