Java avoid wrapper methods in Decorator -
if want add new behaviour existing class in java can subclass or use decorator pattern. if using decorator pattern end having implement methods in interface , call through decorated object:
class decoratedlist<t> extends list<t> { list<t> baselist; int size() { return baselist.size(); } //..like method above, every method on list.. //the added behaviour t secondthing() { return baselist.get(1); } }
whereas subclassing don't need boilerplate method passing.
class decoratedlist<t> extends arraylist<t> { //the added behaviour t secondthing() { return list.get(1); } }
the problem this can used need arraylist, , have access construction of arraylist (i know in example copy elements arraylist decoratedlist create yourself, works lists, , wouldn't suitable).
is there method can add class if method not found on decoratedlist class, forward baselist, in order avoid having bunch of 1 liner methods don't new:
class decoratedlist<t> extends list<t> { list<t> baselist; //pseudocode methodnotfound(method, arguments) { method.invokeon(baselist, arguments); } //the added behaviour t secondthing() { return baselist.get(1); } }
Comments
Post a Comment