Mission impossible? (babysloth, wangwenyou, jimjxr, abcabcabc please come in)

ajoo 2002-05-12 04:28:05
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?
...全文
52 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangwenyou 2002-05-13
  • 打赏
  • 举报
回复
呵呵,是啊,最大的挑战
我可不敢想了--脑细胞死光了怎么办啊:)
ajoo 2002-05-13
  • 打赏
  • 举报
回复
Seems, we could get some code reuse using C++'s template approach. (with template specialization)
I'm now writing it.
ajoo 2002-05-13
  • 打赏
  • 举报
回复
Sigh.
I thought step 2 already qualifies to be a challenge.
:)

And this cannot be a challenge, because I don't have good answer myself. :-<
wangwenyou 2002-05-13
  • 打赏
  • 举报
回复
我的反射,敬请提出宝贵意见,谢谢:)
http://www.csdn.net/expert/topic/717/717381.xml?temp=.949093
wangwenyou 2002-05-12
  • 打赏
  • 举报
回复
Impossible!
如果说以前是热身的话,这算是一个很好的挑战
:P
wangwenyou 2002-05-12
  • 打赏
  • 举报
回复
暂无时间,呵呵,先顶一下吧:)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧