the question may stupid 1 ask but, kindly me on this. need override behavior of class, object of it. following code sample.
i want use method util.newchildcomponent(string id) in places.
class parentcomponent{ public parentcomponent(string id){} protected void somebehavior(){} } class childcomponent extends parentcomponent{ public childcomponent(string id){ super(id); } protected void childsbehavior(){} } public class util { public static parentcomponent newchildcomponent(string id) { parentcomponent fc = new childcomponent(id); // initialize object, , performs common configuration. // performs different operations on generated child object. // child class has many members apart childsbehavior(). return fc; } } // above classes belongs jar, cannot edit code. // need use util.newchildcomponent(string id) // need override behavior mentioned below. public void f1() { // todo: how override childsbehavior() method using object // have in pc? possible? // util.newchildcomponent(id) method decorates childcomponent // need use same object , override childsbehavior() method only. parentcomponent pc = util.newchildcomponent("childid"); // need achieve result below /* parentcomponent pc = new childcomponent(id){ @override protected void childsbehavior(){ super.somebehavior(); // stuff here. } }; */ } thanks in advance.
sounds looking dynamicobjectadaptorfactory.
here's use of excellent object.
static class parentcomponent { protected void somebehavior() { system.out.println("parentcomponent somebehavior"); } } static class childcomponent extends parentcomponent { private childcomponent(string id) { } protected void childsbehavior() { system.out.println("childsbehavior"); } } public static class util { public static parentcomponent newchildcomponent(string id) { parentcomponent fc = new childcomponent(id); // initialize object, , performs common configuration. return fc; } } interface parent { void somebehavior(); } // adaptor. public static class adapter implements parent { final parentcomponent parent; private adapter(parentcomponent pc) { this.parent = pc; } // override parent behaviour. public void somebehavior() { system.out.println("adapter invoke somebehaviour()"); parent.somebehavior(); system.out.println("adapter finished somebehaviour()"); } } public void test() { parentcomponent pc = util.newchildcomponent("childid"); parent adapted = dynamicobjectadapterfactory.adapt(pc, parent.class, new adapter(pc)); adapted.somebehavior(); } prints:
adapter invoke somebehaviour() somebehavior adapter finished somebehaviour()
Comments
Post a Comment