fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static void computeWithPrototype(Calculator proto) {
  11. Calculator calc = proto.copy();
  12. calc.setA(123);
  13. calc.setB(321);
  14. System.out.println(calc.compute());
  15. }
  16. public static void main(String[] args) throws Exception {
  17. computeWithPrototype(new Adder());
  18. computeWithPrototype(new Multiplier());
  19. }
  20. }
  21.  
  22. interface Calculator {
  23. void setA(int a);
  24. void setB(int b);
  25. int compute();
  26. Calculator copy();
  27. }
  28. class Adder implements Calculator {
  29. private int a,b;
  30. public void setA(int a) {this.a=a;}
  31. public void setB(int b) {this.b=b;}
  32. public int compute() {return a+b;}
  33. public Calculator copy() {
  34. Adder res = new Adder();
  35. res.a = a;
  36. res.b = b;
  37. return res;
  38. }
  39. }
  40. class Multiplier implements Calculator {
  41. private int a,b;
  42. public void setA(int a) {this.a=a;}
  43. public void setB(int b) {this.b=b;}
  44. public int compute() {return a*b;}
  45. public Calculator copy() {
  46. Multiplier res = new Multiplier();
  47. res.a = a;
  48. res.b = b;
  49. return res;
  50. }
  51. }
Success #stdin #stdout 0.08s 380160KB
stdin
Standard input is empty
stdout
444
39483