java:interface_implementation

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Last revision Both sides next revision
java:interface_implementation [2017/03/31 13:33]
gthanos [StartStopTimer]
java:interface_implementation [2017/03/31 15:10]
gthanos [LinkedStack]
Line 5: Line 5:
 Σε συνέχεια του προηγούμενου παραδείγματος θα επιχειρήσουμε να δημιουργήσουμε δύο διαφορετικές κλάσεις που υλοποιούν το συγκεκριμένο //​interface//​. Οι κλάσεις αυτές είναι **SimpleTimer** και **StartStopTimer**. Και οι δύο κλάσεις παρέχουν την ίδια λειτουργικότητα. Σε συνέχεια του προηγούμενου παραδείγματος θα επιχειρήσουμε να δημιουργήσουμε δύο διαφορετικές κλάσεις που υλοποιούν το συγκεκριμένο //​interface//​. Οι κλάσεις αυτές είναι **SimpleTimer** και **StartStopTimer**. Και οι δύο κλάσεις παρέχουν την ίδια λειτουργικότητα.
  
-===== SimpleTimer ​=====+===== ArrayStack ​=====
  
-<code java SimpleTimer.java> +<code java ArrayStack.java> 
-class SimpleTimer ​implements ​Timer +public ​class ArrayStack ​implements ​Stack 
-  ​private long start_time, duration+  ​public int capacity
-  ​private boolean running = false; +  ​public Object []array
- +  public int size;
-  public ​void setTimer(int seconds) { +
-    duration = seconds * 1000; +
-  }+
   ​   ​
-  public ​boolean startTimer() { +  public ​ArrayStack() { 
-    ​Date now = new Date()+    ​this(256);
-    if(duration > 0) { +
-      start_time = now.getTime();​ +
-      running = true; +
-    } +
-    else { +
-      running = false; +
-    } +
-    return running;+
   }   }
   ​   ​
-  public ​void stopTimer() { +  public ​ArrayStack(int capacity) { 
-    ​Date now = new Date()    +    ​array = new Object[capacity]
-    ​if( running && now.getTime() > start_time) +    ​this.capacity ​capacity
-      duration -now.getTime() - start_time+    ​size 0;
-    ​running ​false;+
   }   }
   ​   ​
-  public ​boolean isRunning() { +  public ​int size() { 
-    return ​running;+    return ​size;
   }   }
   ​   ​
-  public ​boolean hasExpired() { +  public ​void push(Object o) { 
-    Date now = new Date(); +   
-    if( (running && now.getTime() - start_time ​>= duration) || duration <= 0 ) {       +    if( size >= capacity-1 ​) { 
-      ​start_time ​0L+      capacity *= 2; 
-      ​duration ​0L+      ​Object []newArray ​new Object[capacity]
-      ​running ​false+      ​for(int i=0; i<​array.length;​ i++) 
-      ​return true;+        newArray[i] ​array[i]
 +      ​array newArray
 +      ​newArray = null;
     }     }
-    return ​false;+    ​array[size++] = o; 
 +  } 
 +   
 +  public Object pop() { 
 +    ​return ​array[--size];​ 
 +  } 
 +   
 +  public Object top() { 
 +    return array[size-1];
   }   }
   ​   ​
 +  public String toString() {
 +    String str = "​@@@@@@@@ - Stack - @@@@@@@@\n";​
 +    for(int i=size-1; i>=0; i--)
 +      str += array[i].toString()+"​\n";​
 +    return str + "​@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";​
 +  }  ​
 } }
 </​code>​ </​code>​
  
-===== StartStopTimer ​=====+===== LinkedStack ​=====
  
-<code java StartStopTimer.java> +<code java ArrayListStack.java> 
-class StartStop ​implements ​Timer +import java.util.*;​ 
-  private ​long start_time, duration; +public ​class ArrayListStack ​implements ​Stack 
-  ​ArrayList ​starts = new ArrayList(); +  private ArrayList ​list;   
-  ArrayList stops = new ArrayList();​ +   
-  ​private boolean running = false; +  public ​ArrayListStack() { 
- +    ​list new ArrayList();
-  public ​void setTimer(int seconds) { +
-    ​duration ​seconds * 1000;+
   }   }
-  public ​boolean startTimer() { +  ​ 
-    ​long now = new Date().getTime()+  ​public ​int size() { 
-    if(!running) { +    ​return list.size();
-      starts.add(now);​ +
-      running = true; +
-    } +
-    return running;+
   }   }
-  public void stopTimer() {     +  ​ 
-    ​long now = new Date().getTime();​ +  ​public void push(Object o) {   
-    if(running) { +    ​list.add(o);
-      stops.add(now); +
-      running = false; +
-    }+
   }   }
-  public ​boolean isRunning() { +  ​ 
-    return ​running;+  ​public ​Object pop() { 
 +    return ​list.remove(list.size()-1);
   }   }
   ​   ​
-  public ​boolean hasExpired() { +  public ​Object top() { 
-    ​if( !running ) +    return ​list.get(list.size()-1);
-      ​return ​running; +
-    long runtime = 0L; +
-    for(int i=0; i<​stops.size();​ i++) { +
-      runtime += (long)((Long)stops.get(i) - (Long)starts.get(i)); +
-    } +
-    long now = new Date().getTime();​ +
-    runtime += now (Long)starts.get(stops.size());​ +
-    if( running && runtime > duration ) { +
-      start_time = 0; +
-      duration = 0; +
-      running = false; +
-      return true; +
-    } +
-    return false;+
   }   }
 +  ​
 +  public String toString() {
 +    String str = "​@@@@@@@@ - Stack - @@@@@@@@\n";​
 +    for(Object o : list)
 +      str += o.toString()+"​\n";​
 +    return str + "​@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";​
 +  }  ​
 } }
 </​code>​ </​code>​
  
-Παραπάνω έχουμε υλοποιήσει το συγκεκριμένο //​interface//​ μέσα από δύο διαφορετικές κλάσεις. Αν και η υλοποίηση των κλάσεων διαφέρει σημαντικά,​ η λειτουργικότητα που παρέχουν ​είναι ισοδύναμη. Μπορείτε να χρησιμοποιήσετε οποιαδήποτε από της δύο κλάσεις για να εξυπηρετήσετε τη λειτουργικότητα του //​interface//​ **Timer** σε ένα πρόγραμμα.+Παραπάνω έχουμε υλοποιήσει το συγκεκριμένο //​interface//​ μέσα από δύο διαφορετικές κλάσεις. Αν και η υλοποίηση των κλάσεων διαφέρει σημαντικά,​ η λειτουργία τους είναι ​κοινή. Μπορείτε να χρησιμοποιήσετε οποιαδήποτε από της δύο κλάσεις για να εξυπηρετήσετε τη λειτουργικότητα του //​interface//​ **Stack** σε ένα πρόγραμμα.
  
-| Προηγούμενο : [[ java:​interface_definition ]] | [[ :toc | Περιεχόμενα ]] | Επόμενο:​ [[ :​java:​interface_as_data_type | Το interface ως τύπος δεδομένων ]]  |+| Προηγούμενο : [[ :java:​interface_definition ​| Δήλωση του interface  ​]] | [[ :toc | Περιεχόμενα ]] | Επόμενο:​ [[ :​java:​interface_as_data_type | Το interface ως τύπος δεδομένων ]]  |
  
java/interface_implementation.txt · Last modified: 2019/04/05 17:20 by gthanos