The Artima Developer Community
Sponsored Link

Interface Design by Bill Venners
Use interfaces to define large families or to say "What objects can do."

Advertisement

Interface Design | Contents | Previous | Next

Adjective interface: Way to do callbacks. Include performance of these kinds of services in my contract.

How to use interfaces


Some interface Examples


Pulling interfaces out of Thin Air


Adding a Notification Service to OverdraftAccount

 1 // In file ex3/OverdraftAccount.java
 2
 3 public class OverdraftAccount extends Account {
 4
       //...
74
75     public void addOverdraftListener(OverdraftListener l) {
76         //...
77     }
78
79     public void removeOverdraftListener(
80         OverdraftListener l) {
81         //...
82     }
83
84 }

 1 // In file ex3/OverdraftEvent.java
 2
 3 public class OverdraftEvent
 4     extends java.util.EventObject {
 5
 6     private long overdraft;
 7
 8     // overdraft is the current value of overdraft
 9     // in US pennies. overdraft will be either some
10     // positive number or zero. If zero, it indicates
11     // that the overdraft has just been paid back.
12     // If positive, it means that the account was just
13     // overdrawn. (If $50 has been loaned to the
14     // account holder, overdraft will be 5000)
15     public OverdraftEvent(Object source, long overdraft) {
16
17         super(source);
18         this.overdraft = overdraft;
19     }
20
21     public long getOverdraft() {
22         return overdraft;
23     }
24
25 }

 1 // In file ex3/OverdraftListener.java
 2
 3 public interface OverdraftListener
 4     extends java.util.EventListener {
 5
 6     void overdraftOccurred(OverdraftEvent e);
 7
 8     void overdraftRepaid(OverdraftEvent e);
 9
10 }

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use