SHOW:
|
|
- or go back to the newest paste.
| 1 | package DisposersDemo; | |
| 2 | ||
| 3 | /** | |
| 4 | * A Disposer does some kind of disposing. | |
| 5 | * | |
| 6 | */ | |
| 7 | public interface Disposer | |
| 8 | {
| |
| 9 | /** | |
| 10 | * Dispose of the given waste. | |
| 11 | * Implementers must dispose of the waste in the manner they choose. | |
| 12 | * | |
| 13 | * @param waste the wasted to be disposed of. | |
| 14 | */ | |
| 15 | void dispose(String waste); | |
| 16 | } | |
| 17 | package DisposersDemo; | |
| 18 | ||
| 19 | ||
| 20 | /** | |
| 21 | * Chipper disposes thing by cutting them into chips. | |
| 22 | */ | |
| 23 | public class Chipper implements Disposer | |
| 24 | {
| |
| 25 | public void dispose(String waste) | |
| 26 | {
| |
| 27 | System.out.println("Now chipping: " + waste);
| |
| 28 | } | |
| 29 | } | |
| 30 | package DisposersDemo; | |
| 31 | /** | |
| 32 | * Incinerator disposes thing by burning them. | |
| 33 | */ | |
| 34 | public class Incinerator implements Disposer | |
| 35 | {
| |
| 36 | public void dispose(String waste) | |
| 37 | {
| |
| 38 | System.out.println("Now burning: " + waste);
| |
| 39 | } | |
| 40 | } | |
| 41 | package DisposersDemo; | |
| 42 | /** | |
| 43 | * Some Gardener will be modeled here. | |
| 44 | */ | |
| 45 | public class Gardener | |
| 46 | {
| |
| 47 | private Disposer disposer; | |
| 48 | /** | |
| 49 | * Constructor for objects of class Gardener | |
| 50 | */ | |
| 51 | public Gardener(Disposer disposer) | |
| 52 | {
| |
| 53 | this.disposer = disposer; | |
| 54 | } | |
| 55 | public void removeWeeds(String input) | |
| 56 | {
| |
| 57 | for (int ltr = 0; ltr < input.length()-4; ltr+=4) | |
| 58 | {
| |
| 59 | - | String slice = input.substring(ltr,ltr+2); |
| 59 | + | String weeds = input.substring(ltr,ltr+2); |
| 60 | - | disposer.dispose(slice); |
| 60 | + | disposer.dispose(weeds); |
| 61 | } | |
| 62 | } | |
| 63 | } | |
| 64 | package DisposersDemo; | |
| 65 | ||
| 66 | import java.util.*; | |
| 67 | public class AppMain | |
| 68 | {
| |
| 69 | public static void main(String[] args) | |
| 70 | {
| |
| 71 | Disposer machine; | |
| 72 | // Decide which Disposer to use | |
| 73 | if (args.length == 0) | |
| 74 | {
| |
| 75 | machine = new Incinerator(); | |
| 76 | } | |
| 77 | else | |
| 78 | {
| |
| 79 | machine = new Chipper(); | |
| 80 | } | |
| 81 | // Read a line of input and give to Gardener | |
| 82 | Scanner scan = new Scanner(System.in); | |
| 83 | String data = scan.nextLine(); | |
| 84 | // The gardener uses whatever disposer we created | |
| 85 | Gardener gardener = new Gardener(machine); | |
| 86 | gardener.removeWeeds(data); | |
| 87 | } | |
| 88 | } |