Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. class OldCode {
  2.     private static final int K = 5;
  3.     private static final int Q = 15;
  4.     private static final int M = 25;
  5.  
  6.     class SomeClass {
  7.         public int doSomething() {
  8.             return OldCode.K * 5;
  9.         }
  10.     }
  11.  
  12.     class SomeOtherClass {
  13.         public int doSomething() {
  14.             return OldCode.Q * OldCode.M * 3;
  15.         }
  16.     }
  17. }
  18.  
  19. //refactored variant, name "AlgorithmParamters" so something more useful maybe
  20. class NewCode {
  21.     class AlgorithmParameters {
  22.         private final int k;
  23.         private final int q;
  24.         private final int m;
  25.  
  26.         AlgorithmParameters(int k, int q, int m) {
  27.             this.k = k;
  28.             this.q = q;
  29.             this.m = m;
  30.         }
  31.  
  32.         public int getK() {
  33.             return k;
  34.         }
  35.  
  36.         public int getQ() {
  37.             return q;
  38.         }
  39.  
  40.         public int getM() {
  41.             return m;
  42.         }
  43.     }
  44.  
  45.     class SomeClass {
  46.         private final AlgorithmParameters parameters;
  47.  
  48.         SomeClass(AlgorithmParameters parameters) {
  49.             this.parameters = parameters;
  50.         }
  51.  
  52.         public int doSomething() {
  53.             return parameters.getK() * 5;
  54.         }
  55.  
  56.         //another variant which may be more elegant
  57.         public int doSomething(AlgorithmParameters parameters) {
  58.             return parameters.getK() * 5;
  59.         }
  60.     }
  61.  
  62.     class SomeOtherClass {
  63.         private final AlgorithmParameters parameters;
  64.  
  65.         SomeOtherClass(AlgorithmParameters parameters) {
  66.             this.parameters = parameters;
  67.         }
  68.  
  69.         public int doSomething() {
  70.             return parameters.getQ() * parameters.getM() * 3;
  71.         }
  72.  
  73.         //another variant which may be more elegant
  74.         public int doSomething(AlgorithmParameters parameters) {
  75.             return parameters.getQ() * parameters.getM() * 3;
  76.         }
  77.     }
  78.    
  79.     public void exampleRun() {
  80.         for(int i = 0;i<10;++i) {
  81.             AlgorithmParameters parameters = new AlgorithmParameters(i,i,i);
  82.             SomeClass someClass = new SomeClass(parameters);
  83.             SomeOtherClass someOtherClass = new SomeOtherClass(parameters);
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement