Advertisement
MareCK_ste

Strategy_example

May 27th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. // Strategy pattern example
  2. //by Jozef Pazurik
  3. public class Polozka {
  4.        
  5.         private int velkost;
  6.         private int hmotnost;
  7.         private String nazov;
  8.        
  9.         public Polozka(String n, int v, int h){
  10.                 nazov = n;
  11.                 velkost = v;
  12.                 hmotnost = h;
  13.         }
  14.        
  15.         public int getVelkost(){
  16.                 return velkost;
  17.         }
  18.        
  19.         public int getHmotnost(){
  20.                 return hmotnost;
  21.         }
  22.        
  23.         public void info(){
  24.                 System.out.println(this.getClass().getName() + " " + this.nazov);
  25.         }
  26.  
  27. }
  28.  
  29.  
  30. public interface testovanie {
  31.         public boolean test(int hodnota);
  32. }
  33.  
  34.  
  35.  
  36. public class TestVelkostMensiaAkoSto implements testovanie{
  37.  
  38.         @Override
  39.         public boolean test(int hodnota) {
  40.                 return (hodnota < 100);
  41.         }
  42. }
  43.  
  44.  
  45. public class TestVelkostVacsiaAkoSto implements testovanie{
  46.  
  47.         @Override
  48.         public boolean test(int hodnota) {
  49.                 return (hodnota > 100);
  50.         }
  51.  
  52. }
  53.  
  54. public class Main {
  55.  
  56.         public static void main(String[] args) {
  57.                 ArrayList<Polozka> polozky = new ArrayList<Polozka>();
  58.                
  59.                 polozky.add(new Polozka("prva",50,200));
  60.                 polozky.add(new Polozka("druha",200,50));
  61.                 polozky.add(new Polozka("tretia",200,200));
  62.                 polozky.add(new Polozka("stvrta",50,50));
  63.                
  64.                 testovanie filter;
  65.                
  66.                 //filter = new TestVelkostMensiaAkoSto();
  67.                 filter = new TestVelkostVacsiaAkoSto();
  68.                
  69.                 for (Polozka p : polozky){
  70.                         if (filter.test(p.getVelkost())) {
  71.                                 p.info();
  72.                         }
  73.                 }
  74.         }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement