java - Override method from assigned class -
i want override method class that's assigned variable.
example:
inventory = new inventory( ); /* code here changes how inventory must behave or whatever */ inventory { @override ... }
is possible ???
maybe think of (instead of null-if implement default strategy make more clean):
public interface strategy { public void dosomething(); } public class inventory { strategy strategy; public inventory() { // ... } public void dosomething() { if (strategy == null) { system.out.println("strategy empty"); } else { strategy.dosomething(); } } public strategy getstrategy() { return strategy; } public void setstrategy(strategy strategy) { this.strategy = strategy; } }
then this
inventory inventory = new inventory(); inventory.dosomething(); inventory.setstrategy(new strategy() { @override public void dosomething() { system.out.println("strategy different"); } }); inventory.dosomething();
shows this:
strategy empty strategy different
for more elaborated version can take @ strategy pattern.
Comments
Post a Comment