当前位置: 技术问答>java相关
Mission impossible? (babysloth, wangwenyou, jimjxr, abcabcabc please come in)
来源: 互联网 发布时间:2015-05-28
本文导语: Now we have Array interface, Sequence interface, 4 decorator classes RotateSequence, ReverseSequence, SliceSequence, ConcatSequence, 1 bridge class Array2Sequence (people who don't know the background of this question, please refer http://www.csdn.net/expe...
Now we have Array interface, Sequence interface, 4 decorator classes RotateSequence, ReverseSequence, SliceSequence, ConcatSequence, 1 bridge class Array2Sequence
(people who don't know the background of this question, please refer
http://www.csdn.net/expert/topic/690/690270.xml?temp=.7524835 and
http://www.csdn.net/expert/topic/700/700322.xml?temp=.7635614)
we use state pattern, responsibility chain, bridge pattern, decorator pattern in those classes.
And that pretty much solves our nested decorator problem.
Wonderful world, isn't it?
In my practice, I also have read-only array, write-only array, typed array etc.
public interface ArrayR
{
public int getLength();
public Object get(int i);
}
public interface ArrayW
{
public int getLength();
public void set(int i, Object val);
}
public interface Array extends ArrayR, ArrayW
{
public int getLength();
public Object get(int i);
public void set(int i, Object val);
}
public interface TypedArray extends Array
{
public Class getComponentType();
}
So that, in a method like ArrayUtil.binarySearch(ArrayR, Object, Comparator), I can search any ArrayR. (it does not have to be writable).
In a method like ArrayUtil.copyArray(ArrayR, ArrayW), the source array only needs to be readable, the dest array only needs to be writable.
OK. since we have slice, rotate, reverse, concat operations on Array, there's no reason we don't have them for ArrayR, ArrayW, TypedArray.
If I implement RotateSequenceR, ReverseSequenceR, RotateSequenceW etc, the impl will be pretty much the same as what we do for Array and Sequence. I can do copy&paste+minor changes, and I'll be all set.
That means, I'll need to write (or copy) similar code for 3*5=15 times. And if I also introduce TypedArrayR, TypedArrayW, the number will become 25.
Hooray! We are done!
But, wait a minute! Is that so great though? COPY&PASTE! Bad smell, isn't it?
How can I avoid copy&paste and reuse code as much as possible?
I don't have a solution in Java. Could not figure out a neat way even with GJ's parametric polymorphism. But C++'s template approach might work for this case. (not sure though)
Guys, your expertise is called for!
Babysloth: your ideas?
(people who don't know the background of this question, please refer
http://www.csdn.net/expert/topic/690/690270.xml?temp=.7524835 and
http://www.csdn.net/expert/topic/700/700322.xml?temp=.7635614)
we use state pattern, responsibility chain, bridge pattern, decorator pattern in those classes.
And that pretty much solves our nested decorator problem.
Wonderful world, isn't it?
In my practice, I also have read-only array, write-only array, typed array etc.
public interface ArrayR
{
public int getLength();
public Object get(int i);
}
public interface ArrayW
{
public int getLength();
public void set(int i, Object val);
}
public interface Array extends ArrayR, ArrayW
{
public int getLength();
public Object get(int i);
public void set(int i, Object val);
}
public interface TypedArray extends Array
{
public Class getComponentType();
}
So that, in a method like ArrayUtil.binarySearch(ArrayR, Object, Comparator), I can search any ArrayR. (it does not have to be writable).
In a method like ArrayUtil.copyArray(ArrayR, ArrayW), the source array only needs to be readable, the dest array only needs to be writable.
OK. since we have slice, rotate, reverse, concat operations on Array, there's no reason we don't have them for ArrayR, ArrayW, TypedArray.
If I implement RotateSequenceR, ReverseSequenceR, RotateSequenceW etc, the impl will be pretty much the same as what we do for Array and Sequence. I can do copy&paste+minor changes, and I'll be all set.
That means, I'll need to write (or copy) similar code for 3*5=15 times. And if I also introduce TypedArrayR, TypedArrayW, the number will become 25.
Hooray! We are done!
But, wait a minute! Is that so great though? COPY&PASTE! Bad smell, isn't it?
How can I avoid copy&paste and reuse code as much as possible?
I don't have a solution in Java. Could not figure out a neat way even with GJ's parametric polymorphism. But C++'s template approach might work for this case. (not sure though)
Guys, your expertise is called for!
Babysloth: your ideas?
|
暂无时间,呵呵,先顶一下吧:)
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。