博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android小白的探索:2D绘图之Android简易版Microsoft Visio学习之路 三、装饰者模式...
阅读量:6910 次
发布时间:2019-06-27

本文共 3074 字,大约阅读时间需要 10 分钟。

hot3.png

    在Microsoft Visio中点击一个图像,会在这个图像边缘出现一个方形的框,现在来实现这个框。

写之前强烈推荐一下别人写的博客,简单明了

    当我们想要对一个类或多个类的功能进行扩充,通常就在这个类或父类中做改变,装饰着模式,就是在不改变原有类的功能属性的情况下,对这个类进行动态的扩展,而这个类什么也不用管,只需用自己做自己的就好了。

    装饰着模式,很重要的一点就是,装饰者类与被装饰者类要实现共同的接口。装饰者类,面向的不是对类,而是接口,通过对接口的操作,来实现对类的扩展。

    被装饰的类的接口

public interface DataInterface {    public void addChild(DataInterface baseData);    public void removeChild(int index);    public void move(float moveX ,float moveY);    public void draw(Canvas canvas);    public List
getChild(); public DataType[] pointIsInside(float moveX , float moveY); public void writer(BufferedWriter bufferedWriter); public DataInterface read(BufferedReader bufferedReader); public float[] getMaxMinXY(boolean checkOrRadio);}

这个接口中,与我上一篇博客,最大的不同,是,传入的参数不再是一个父类,而是一个接口,凡是实现这个接口的类都能传入

    装饰者类的接口

public interface DecorationModeInterface extends DataInterface {}

装饰者类有自己扩展这个类的接口:

private boolean aBoolean ;private float[] floats;private DataInterface dataInterface;public DecorationMode(DataInterface dataInterface){    this.dataInterface = dataInterface;}@Overridepublic void addChild(DataInterface baseData) {    this.dataInterface.addChild(baseData);}@Overridepublic void removeChild(int index) {    this.dataInterface.removeChild(index);}@Overridepublic void move(float moveX, float moveY) {    this.dataInterface.move(moveX , moveY);}@Overridepublic void draw(Canvas canvas) {    this.dataInterface.draw(canvas);    if (isaBoolean()) {        drawRectangle(getFloats(), canvas);        setaBoolean(false);    }}@Overridepublic List
getChild() { return this.dataInterface.getChild();}@Overridepublic DataType[] pointIsInside(float moveX, float moveY) { return this.dataInterface.pointIsInside(moveX,moveY);}@Overridepublic void writer(BufferedWriter bufferedWriter) { this.dataInterface.writer(bufferedWriter);}@Overridepublic DataInterface read(BufferedReader bufferedReader) { return this.dataInterface.read(bufferedReader);}@Overridepublic float[] getMaxMinXY(boolean checkOrRadio) { float[] floats = this.dataInterface.getMaxMinXY(checkOrRadio); float[] floats1 = {floats[0] - 10 ,floats[1] - 10 ,floats[2] + 10,floats[3] + 10}; setFloats(floats1); setaBoolean(checkOrRadio); return getFloats();}public boolean isaBoolean() { return aBoolean;}public void setaBoolean(boolean aBoolean) { this.aBoolean = aBoolean;}public float[] getFloats() { return floats;}public void setFloats(float[] floats) { this.floats = floats;}//float left, float top, float right, float bottomprivate RectangleData rectangleData = new RectangleData();void drawRectangle(float[] floats ,Canvas canvas){ rectangleData.setRectF(new RectF(floats[0] , floats[1],floats[2],floats[3])); rectangleData.setPaintAlpha(100); rectangleData.setPaintStyle(Paint.Style.STROKE); rectangleData.setPaintColor(Color.RED); rectangleData.setWight(10); rectangleData.draw(canvas);}

drawRectangle:为需要画框出来的类,画出来。

    装饰者类,其实就是在 所有需要被装饰的类的外面,嵌套一层 装饰者类,用装饰着类去包裹住被装饰者,调用装饰者,在装饰着中执行扩展的方法,然后执行原有的方法。实现对类的扩展

 

转载于:https://my.oschina.net/zhenghaoLi/blog/1533771

你可能感兴趣的文章
启动时nginx时报错
查看>>
菜鸟学Linux 第063篇笔记 postfix+mysql+courier-authlib
查看>>
【 58沈剑 架构师之路】InnoDB七种锁——共享/排它锁、意向锁、插入意向锁
查看>>
终究未能留下,秦致被动离去,汽车之家已变天
查看>>
wxWidgets第一课 wx/wx.h解决头文件包含问题
查看>>
论Mysql5.7.13架构组成之物理文件
查看>>
C/C++笔试题目大全
查看>>
呼叫转移XCAP log的查看
查看>>
JAVA--------抽象类
查看>>
我的友情链接
查看>>
动画状态切换
查看>>
linux 下的GPT分区
查看>>
线程同步
查看>>
Nginx 配置SSL证书
查看>>
每日总结复习
查看>>
iPhone 开发过程中的一些小技术的总结
查看>>
android 资料
查看>>
ThreadLocal 那点事儿
查看>>
Spark源码分析调试环境搭建
查看>>
手把手教你搭建LyncServer2013之命令行持久聊天室和聊天室BUG(十五)
查看>>