Java 在这种情况下如何使用装饰设计?
下面是我的测试代码。案例是:我尝试创建一个视图,允许许多装饰器附加在上面,比如滚动条、额外子视图、背景色、此视图的特殊效果等等。问题是我发现Java 在这种情况下如何使用装饰设计?,java,algorithm,design-patterns,Java,Algorithm,Design Patterns,下面是我的测试代码。案例是:我尝试创建一个视图,允许许多装饰器附加在上面,比如滚动条、额外子视图、背景色、此视图的特殊效果等等。问题是我发现i1和i2不是我真正需要的,我需要的是whatINeed1和whatINeed2。在这种情况下,拥有i1和i2有什么意义。在这种情况下,它们不会变得像发电机吗 语法在很多地方可能是错误的,因为我使用objective-c进行了测试,但在线复制和定制了java示例代码 如果我从一开始就错了,请你指出 public class Decorator { abst
i1
和i2
不是我真正需要的,我需要的是whatINeed1
和whatINeed2
。在这种情况下,拥有i1
和i2
有什么意义。在这种情况下,它们不会变得像发电机吗
语法在很多地方可能是错误的,因为我使用objective-c进行了测试,但在线复制和定制了java示例代码
如果我从一开始就错了,请你指出
public class Decorator {
abstract class I implements DefaultView { I render(); }
static class A implements I { public I render() { System.out.print( 'A' ); } }
static abstract class D implements I {
private I core;
public D( I inner ) { core = inner; }
public I render() { return core.render(); }
}
static class X extends D {
public X( I inner ) { super( inner ); }
public I render() {
I i = super.render();
i.setBackgroundColor();
}
}
static class Y extends D {
public Y( I inner ) { super( inner ); }
public I render() {
I i = super.render();
View v = new View();
//This is objective-c similar syntax,
//I don't know what's equivalent to Java
i.addSubview(v);
}
}
public static void main( String[] args ) {
I i1 =new X( new A() )
I i2= new Y( new X( new A() ) )};
I whatINeed1 = i1.render();
I whatINeed2 = i2.render;
}
谈论装饰设计就是在不修改现有代码的情况下添加额外的特性。如何实现下面所示的模型?如何在不修改源代码的情况下绘制边框和滚动条?滚动条需要与原始窗口交互。
Decorator通常用于在不修改X源代码的情况下向类X(比如方法)添加额外的“功能” i、 e 我们想添加一种方法,允许我们获得阶梯中的分数排名,但我们不能/不想修改PlayerScore。。然后:
public class MyPlayerScore extends PlayerScore{
PlayerScore component;
int getRank() { /**some code**/}
void setScore(int x){ component.setScore(x);}
}
从代码的外观来看,您实际上并没有添加功能。基类I已经具有setBackgroundColor()、addSubView()等功能,您只是在子类中调用它们
这看起来像是继承和组合模式的混合,而不是Decorator,即使组合部分(通过将组件传递给构造函数)使其看起来像Decorator
如果代码对您有效,那么它是可以使用的,但是如果您想使用Decorator模式,您将向继承类添加越来越多的方法,因为您需要为这些类获得越来越多的专用行为(例如,类PanelView将添加void scroll())
更多关于decorator的信息:
编辑(1):
要使用decorator向父视图(比如窗口)添加滚动条,父类必须公开某种形式的接口,新修饰的类可以使用该接口实现滚动行为。例如,如果window有一个方法返回某个图形API的句柄,让我们调用句柄“g”,那么装饰类ScrollPane可以执行以下操作:
public class ScrollPane extends Window{
Window component;
void renderScrollBar(){
Graphics g = window.getGraphicsHandle();
g.drawRectangle(/** some coords for where to put the bar **/);
/** some extra code to add logic on the scroll bar,
/** such as shifting the Window content up or down and cull out
/** the portion that is outside of the ScrollPane.
/** For example, openGL could allow you to do this with scissor
/** test **/
}
void render(){
component.render();
this.renderScrollPane();
}
}
如果你对评论的最后部分感兴趣
你可以这样做。显然,实现一个功能良好的滚动窗格可能需要更多的思考,但是在布局类体系结构方面,Decorator模式可以很好地帮助您设计GUI 对于注释,re
Decorator通常用于在不修改X源代码的情况下向类X(比如方法)添加额外的“功能”。
,我编辑了我的问题,你能帮我回答吗?
public class ScrollPane extends Window{
Window component;
void renderScrollBar(){
Graphics g = window.getGraphicsHandle();
g.drawRectangle(/** some coords for where to put the bar **/);
/** some extra code to add logic on the scroll bar,
/** such as shifting the Window content up or down and cull out
/** the portion that is outside of the ScrollPane.
/** For example, openGL could allow you to do this with scissor
/** test **/
}
void render(){
component.render();
this.renderScrollPane();
}
}