写点什么

设计模式的艺术 第十四章享元设计模式练习(开发一个多功能文档编辑器,在文本文档中可以插入图片、动画、视频等多媒体资料。为了节省系统资源,相同的图片、动画和视频在同一个文档中只需保存一份,但是可以多次重复出现,而且它们每次出现时位置和大小均可不同)

作者:代廉洁
  • 2022 年 9 月 06 日
    浙江
  • 本文字数:2342 字

    阅读完需:约 8 分钟

Sunny 软件公司欲开发一个多功能文档编辑器,在文本文档中可以插入图片、动画、视频等多媒体资料。为了节省系统资源,相同的图片、动画和视频在同一个文档中只需保存一份,但是可以多次重复出现,而且它们每次出现时位置和大小均可不同。试使用享元模式设计该文档编辑器。


一、类结构图



二、典型实现代码

抽象多媒体享元类:抽象享元类
// 抽象多媒体享元类:抽象享元类public abstract class MultimediaFlyweight {    private String name;    protected String resource;
public MultimediaFlyweight(String name, String resource) { this.name = name; this.resource = resource; }
public String getName() { return name; }
public abstract String getResource();
public void display(PosSize posSize) { System.out.println("展示" + this.name + ",位置行数:" + posSize.getLineNumber() + ", 大小:高" + posSize.getHeight() + ",宽" + posSize.getWidth()); }}
复制代码


具体图片享元类:具体享元类
// 具体图片享元类:具体享元类public class ImageFlyweight extends MultimediaFlyweight{    public ImageFlyweight(String name, String resource) {        super(name, resource);    }
@Override public String getResource() { return this.resource; }}
复制代码


具体动画享元类:具体享元类
// 具体动画享元类:具体享元类public class AnimationFlyweight extends MultimediaFlyweight{    public AnimationFlyweight(String name, String resource) {        super(name, resource);    }
@Override public String getResource() { return this.resource; }}
复制代码


具体视频享元类:具体享元类
// 具体视频享元类:具体享元类public class VideoFlyweight extends MultimediaFlyweight{    public VideoFlyweight(String name, String resource) {        super(name, resource);    }
@Override public String getResource() { return this.resource; }}
复制代码


多媒体享元工厂类:具体享元工厂类
// 多媒体享元工厂类:具体享元工厂类public class FlyweightFactory {    private Hashtable flyweights = new Hashtable();
public MultimediaFlyweight getFlyweight(String key) { if (flyweights.containsKey(key)) { return (MultimediaFlyweight) flyweights.get(key); } return null; }
public void addFlyweight(String key, String resource, String type) { if (!this.flyweights.containsKey(key)) { MultimediaFlyweight flyweight = null; if (type == "image") { flyweight = new ImageFlyweight(key, resource); } if (type == "animation") { flyweight = new AnimationFlyweight(key, resource); } if (type == "video") { flyweight = new VideoFlyweight(key, resource); } this.flyweights.put(key, flyweight); } }}
复制代码


具体位置大小类:外部状态类
// 具体位置大小类:外部状态类public class PosSize {    private Integer lineNumber;    private Integer height;    private Integer width;
public Integer getLineNumber() { return lineNumber; }
public Integer getHeight() { return height; }
public Integer getWidth() { return width; }
public void setLineNumber(Integer lineNumber) { this.lineNumber = lineNumber; }
public void setHeight(Integer height) { this.height = height; }
public void setWidth(Integer width) { this.width = width; }}
复制代码


客户端代码:
public class Client {    public static void main(String[] args) {        MultimediaFlyweight image, animation1, animation2, video;        PosSize posSize;        FlyweightFactory flyweightFactory = new FlyweightFactory();        flyweightFactory.addFlyweight("xiaolonglv.png", "图片内容", "image");        flyweightFactory.addFlyweight("张无忌.gif", "动片内容", "animation");        flyweightFactory.addFlyweight("段誉.rmvb", "视频内容", "video");
image = flyweightFactory.getFlyweight("xiaolonglv.png"); animation1 = flyweightFactory.getFlyweight("张无忌.gif"); animation2 = flyweightFactory.getFlyweight("张无忌.gif"); video = flyweightFactory.getFlyweight("段誉.rmvb");
posSize = new PosSize(); posSize.setLineNumber(100); posSize.setHeight(500); posSize.setWidth(800); image.display(posSize);
posSize.setLineNumber(200); animation1.display(posSize);
posSize.setLineNumber(300); animation2.display(posSize);
posSize.setLineNumber(400); video.display(posSize); }}
复制代码


编译并运行程序,输出如下结果:
展示xiaolonglv.png,位置行数:100, 大小:高500,宽800展示张无忌.gif,位置行数:200, 大小:高500,宽800展示张无忌.gif,位置行数:300, 大小:高500,宽800展示段誉.rmvb,位置行数:400, 大小:高500,宽800
复制代码


发布于: 刚刚阅读数: 4
用户头像

代廉洁

关注

还未添加个人签名 2019.10.15 加入

还未添加个人简介

评论

发布
暂无评论
设计模式的艺术 第十四章享元设计模式练习(开发一个多功能文档编辑器,在文本文档中可以插入图片、动画、视频等多媒体资料。为了节省系统资源,相同的图片、动画和视频在同一个文档中只需保存一份,但是可以多次重复出现,而且它们每次出现时位置和大小均可不同)_设计模式的艺术_代廉洁_InfoQ写作社区