This shows you the differences between two versions of the page.
Next revision | Previous revision Next revision Both sides next revision | ||
java:interface_implementation [2017/02/16 14:30] gthanos created |
java:interface_implementation [2017/03/31 13:13] gthanos |
||
---|---|---|---|
Line 3: | Line 3: | ||
Μία κλάση υλοποιεί ένα interface μόνο εάν υλοποιεί ΟΛΕΣ τις μεθόδους του interface. Έάν έστω και μία μέθοδος δεν υλοποιείται τότε η κλάση δεν υλοποιεί το interface. | Μία κλάση υλοποιεί ένα interface μόνο εάν υλοποιεί ΟΛΕΣ τις μεθόδους του interface. Έάν έστω και μία μέθοδος δεν υλοποιείται τότε η κλάση δεν υλοποιεί το interface. | ||
- | Σε συνέχεια του προηγούμενου παραδείγματος θα επιχειρήσουμε να δημιουργήσουμε δύο διαφορετικές κλάσεις που υλοποιούν το συγκεκριμένο interface. Οι κλάσεις αυτές είναι η ArrayStack που υλοποιεί τη στοίβα μέσω ενός πίνακα και η LinkedStack που υλοποιεί τη στοίβα διασυνδέοντας επιμέρους αντικείμενα μεταξύ τους σε αναλογία με μία διασυνδεδεμένη λίστα. | + | Σε συνέχεια του προηγούμενου παραδείγματος θα επιχειρήσουμε να δημιουργήσουμε δύο διαφορετικές κλάσεις που υλοποιούν το συγκεκριμένο //interface//. Οι κλάσεις αυτές είναι **SimpleTimer** και **StartStopTimer**. Και οι δύο κλάσεις παρέχουν την ίδια λειτουργικότητα. |
- | ===== ArrayStack ===== | + | ==== SimpleTimer ==== |
- | <code java ArrayStack.java> | + | <code java SimpleTimer.java> |
- | public class ArrayStack implements Stack { | + | class SimpleTimer implements Timer { |
- | public int capacity; | + | private long start_time, duration; |
- | public Object []array; | + | private boolean running = false; |
- | public int size; | + | |
+ | public void setTimer(int seconds) { | ||
+ | duration = seconds * 1000; | ||
+ | } | ||
| | ||
- | public ArrayStack() { | + | public boolean startTimer() { |
- | this(256); | + | Date now = new Date(); |
+ | //System.err.println("[startTimer] duration: "+duration); | ||
+ | if(duration > 0) { | ||
+ | start_time = now.getTime(); | ||
+ | running = true; | ||
+ | //System.err.println("[startTimer] Running again!"); | ||
+ | } | ||
+ | else { | ||
+ | //System.err.println("[startTimer] Failed to start!"); | ||
+ | running = false; | ||
+ | } | ||
+ | return running; | ||
} | } | ||
| | ||
- | public ArrayStack(int capacity) { | + | public void stopTimer() { |
- | array = new Object[capacity]; | + | Date now = new Date(); |
- | this.capacity = capacity; | + | if( running && now.getTime() > start_time) |
- | size = 0; | + | duration -= now.getTime() - start_time; |
+ | running = false; | ||
} | } | ||
| | ||
- | public int size() { | + | public boolean isRunning() { |
- | return size; | + | return running; |
} | } | ||
| | ||
- | public void push(Object o) { | + | public boolean hasExpired() { |
- | | + | Date now = new Date(); |
- | if( size >= capacity-1 ) { | + | //System.err.println("[hasExpired] running: "+running); |
- | capacity *= 2; | + | if( (running && now.getTime() - start_time >= duration) || duration <= 0 ) { |
- | Object []newArray = new Object[capacity]; | + | start_time = 0L; |
- | for(int i=0; i<array.length; i++) | + | duration = 0L; |
- | newArray[i] = array[i]; | + | running = false; |
- | array = newArray; | + | return true; |
- | newArray = null; | + | |
} | } | ||
- | array[size++] = o; | + | return false; |
} | } | ||
| | ||
- | 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> | ||
- | ===== LinkedStack ===== | + | ===== StartStopTimer ===== |
- | <code java LinkedStack.java> | + | <code java StartStopTimer.java> |
- | class LinkedNode { | + | class StartStop implements Timer { |
- | private Object o; | + | private long start_time, duration; |
- | private LinkedNode next; | + | ArrayList starts = new ArrayList(); |
- | + | ArrayList stops = new ArrayList(); | |
- | public LinkedNode(LinkedNode nxt, Object e) { | + | private boolean running = false; |
- | next = nxt; | + | |
- | o = e; | + | |
- | } | + | |
- | + | ||
- | public LinkedNode(Object e) { | + | |
- | this(null, e); | + | |
- | } | + | |
- | + | ||
- | public Object getElement() { return o; } | + | |
- | public LinkedNode getNext() { return next; } | + | |
- | + | ||
- | public void setElement(Object e) { o = e; } | + | |
- | public void setNext(LinkedNode node) { next = node; } | + | |
- | } | + | |
- | public class LinkedStack implements Stack { | + | public void setTimer(int seconds) { |
- | int size; | + | duration = seconds * 1000; |
- | LinkedNode head; | + | |
- | + | ||
- | public LinkedStack() { | + | |
- | size=0; | + | |
- | head = null; | + | |
} | } | ||
- | | + | public boolean startTimer() { |
- | public int size() { | + | long now = new Date().getTime(); |
- | return size; | + | if(!running) { |
+ | starts.add(now); | ||
+ | running = true; | ||
+ | } | ||
+ | return running; | ||
} | } | ||
- | | + | public void stopTimer() { |
- | public void push(Object o) { | + | long now = new Date().getTime(); |
- | // the following is OK even if head == null. | + | if(running) { |
- | head = new LinkedNode(head, o); | + | stops.add(now); |
- | size++; | + | running = false; |
+ | } | ||
} | } | ||
- | | + | public boolean isRunning() { |
- | public Object pop() { | + | return running; |
- | LinkedNode pN = head; | + | |
- | head = head.getNext(); | + | |
- | size--; | + | |
- | return pN.getElement(); | + | |
} | } | ||
| | ||
- | public Object top() { | + | public boolean hasExpired() { |
- | return head; | + | if( !running ) |
- | } | + | return running; |
- | + | long runtime = 0L; | |
- | public String toString() { | + | for(int i=0; i<stops.size(); i++) { |
- | String str = "@@@@@@@@ - Stack - @@@@@@@@\n"; | + | runtime += (long)((Long)stops.get(i) - (Long)starts.get(i)); |
- | LinkedNode curr = head; | + | } |
- | while(curr != null) | + | long now = new Date().getTime(); |
- | str += curr.getElement().toString(); | + | runtime += now - (Long)starts.get(stops.size()); |
- | return str + "@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"; | + | if( running && runtime > duration ) { |
+ | start_time = 0; | ||
+ | duration = 0; | ||
+ | running = false; | ||
+ | return true; | ||
+ | } | ||
+ | return false; | ||
} | } | ||
} | } | ||
</code> | </code> | ||
- | Παραπάνω έχουμε υλοποιήσει το συγκεκριμένο //interface// μέσα από δύο διαφορετικές κλάσεις. Αν και η υλοποίηση των κλάσεων διαφέρει σημαντικά, η λειτουργία τους είναι κοινή. Μπορείτε να χρησιμοποιήσετε οποιαδήποτε από της δύο κλάσεις για να εξυπηρετήσετε τη λειτουργικότητα του //interface// **Stack** σε ένα πρόγραμμα. | + | Παραπάνω έχουμε υλοποιήσει το συγκεκριμένο //interface// μέσα από δύο διαφορετικές κλάσεις. Αν και η υλοποίηση των κλάσεων διαφέρει σημαντικά, η λειτουργικότητα που παρέχουν είναι ισοδύναμη. Μπορείτε να χρησιμοποιήσετε οποιαδήποτε από της δύο κλάσεις για να εξυπηρετήσετε τη λειτουργικότητα του //interface// **Timer** σε ένα πρόγραμμα. |
| Προηγούμενο : [[ :java:interface_implementation | Υλοποίηση του interface ]] | [[ :toc | Περιεχόμενα ]] | Επόμενο: [[ :java:interface_as_data_type | Το interface ως τύπος δεδομένων ]] | | | Προηγούμενο : [[ :java:interface_implementation | Υλοποίηση του interface ]] | [[ :toc | Περιεχόμενα ]] | Επόμενο: [[ :java:interface_as_data_type | Το interface ως τύπος δεδομένων ]] | | ||