Java 享元模式应用

需求:JavaFX在场景中创建四种不同颜色每种颜色要创建很多个小立方体

使用享元模式,占用一个对象的内存空间便可以创建无数个小立方体

相关代码如下:

抽象享元类

package sample.xiangyuanmoshi;

import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;

/**
 * 享元模式,立方体抽象享元类
 */
public abstract class BoxAbstract {

    /**
     * 设置立方体相关属性的公共方法
     *
     * @return
     */
    public abstract double getWidth();

    public abstract double getHeight();

    public abstract double getDepth();

    public abstract PhongMaterial getMaterial();

    // 获取立方体
    public Box getBox() {
        Box box = new Box();
        box.setWidth(this.getWidth());
        box.setHeight(this.getHeight());
        box.setDepth(this.getDepth());
        // 设置材质
        box.setMaterial(this.getMaterial());
        return box;
    }

}

具体立方体享元类。总共需要四种这里只是其中一种的小立方体,其他种类类似创建。(这里考虑带状态管理的享元模式,以便将这四种小立方体的创建更符合享元模式的思想

package sample.xiangyuanmoshi;


import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;

/**
 * 具体立方体享元类
 */
public class XiangYuanDarkBox extends BoxAbstract {
    @Override
    public double getWidth() {
        return 20;
    }

    @Override
    public double getHeight() {
        return 20;
    }

    @Override
    public double getDepth() {
        return 20;
    }

    @Override
    public PhongMaterial getMaterial() {
        return new PhongMaterial(Color.DARKRED);
    }
}

工厂类(这里使用到了工厂模式和单例模式)

package sample.xiangyuanmoshi;

import java.util.Hashtable;

/**
 * 生产立方体的享元工厂类,使用单例模式进行设计
 */
public class BoxFactory {
    // 单例模式
    private static BoxFactory instance = new BoxFactory();
    // 存储享元对象的享元池
    private static Hashtable hashtable;

    // 实例化
    public BoxFactory() {
        hashtable = new Hashtable();
        // 实例化五种立方体
        BoxAbstract darkBox = new XiangYuanDarkBox();
        BoxAbstract grayBox = new XiangYuanGrayBox();
        BoxAbstract greenBox = new XiangYuanGreenBox();
        BoxAbstract greenYellowBox = new XiangYuanGreenYellowBox();
        BoxAbstract orangeRedBox = new XiangYuanOrangeRedBox();
        // 将物种立方体放入享元池,以便工厂化生产时,获取它们
        hashtable.put("darkBox", darkBox);
        hashtable.put("greenBox", greenBox);
        hashtable.put("grayBox", grayBox);
        hashtable.put("greenYellowBox", greenYellowBox);
        hashtable.put("orangeRedBox", orangeRedBox);
    }

    /**
     * 单例模式,返回本对象实例
     *
     * @return
     */
    public static BoxFactory getInstance() {
        return instance;
    }

    public BoxAbstract getBox(String key) {
        return (BoxAbstract) hashtable.get(key);  //返回向上转型对象(BoxAbstract,立方体抽象享元类)
    }
}

 

测试:

package sample.xiangyuanmoshi;


import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Box;
import javafx.stage.Stage;

/**
 * 测试
 */
public class Client extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        // 实例化一个生产享元立方体对象
        BoxFactory boxFactory = new BoxFactory();
        // 通过享元工厂类生产五种立方体每种各生产三个
        BoxAbstract darkBox = boxFactory.getBox("darkBox");
        BoxAbstract grayBox = boxFactory.getBox("greenBox");
        BoxAbstract greenBox = boxFactory.getBox("grayBox");
        BoxAbstract greenYellowBox = boxFactory.getBox("greenYellowBox");
        BoxAbstract orangeRedBox = boxFactory.getBox("orangeRedBox");
        // 通过上面的享元立方体对象拿到立方体
        Box dark = darkBox.getBox();
        Box dark1 = darkBox.getBox();
        Box dark2 = darkBox.getBox();
        Box gray = grayBox.getBox();
        Box gray1 = grayBox.getBox();
        Box gray2 = grayBox.getBox();
        Box green = greenBox.getBox();
        Box green1 = greenBox.getBox();
        Box green2 = greenBox.getBox();
        Box greenYellow = greenYellowBox.getBox();
        Box greenYellow1 = greenYellowBox.getBox();
        Box greenYellow2 = greenYellowBox.getBox();
        Box orangeRed = orangeRedBox.getBox();
        Box orangeRed1 = orangeRedBox.getBox();
        Box orangeRed2 = orangeRedBox.getBox();
        HBox hBox = new HBox(dark, dark1, dark2, gray, gray1, gray2, green, green1, green2, greenYellow, greenYellow1, greenYellow2, orangeRed, orangeRed1, orangeRed2);
        hBox.setSpacing(10);
        hBox.setAlignment(Pos.CENTER);
        hBox.setVisible(true);
        final Scene scene = new Scene(hBox, 900, 900, Color.GRAY);
        scene.setCursor(Cursor.CLOSED_HAND);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

运行结果:

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值