|
| 1 | +/** |
| 2 | + * The MIT License |
| 3 | + * Copyright (c) 2014-2016 Ilkka Seppälä |
| 4 | + * <p> |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * <p> |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * <p> |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | +package com.iluwatar.dirtyflag; |
| 24 | + |
| 25 | +import java.util.List; |
| 26 | +import java.util.concurrent.Executors; |
| 27 | +import java.util.concurrent.ScheduledExecutorService; |
| 28 | +import java.util.concurrent.TimeUnit; |
| 29 | + |
| 30 | +/** |
| 31 | + * |
| 32 | + * This application demonstrates the <b>Dirty Flag</b> pattern. The dirty flag behavioral pattern allows you to avoid |
| 33 | + * expensive operations that would just need to be done again anyway. This is a simple pattern that really just explains |
| 34 | + * how to add a bool value to your class that you can set anytime a property changes. This will let your class know that |
| 35 | + * any results it may have previously calculated will need to be calculated again when they’re requested. Once the |
| 36 | + * results are re-calculated, then the bool value can be cleared. |
| 37 | + * |
| 38 | + * There are some points that need to be considered before diving into using this pattern:- there are some things you’ll |
| 39 | + * need to consider:- (1) Do you need it? This design pattern works well when the results to be calculated are difficult |
| 40 | + * or resource intensive to compute. You want to save them. You also don’t want to be calculating them several times in |
| 41 | + * a row when only the last one counts. (2) When do you set the dirty flag? Make sure that you set the dirty flag within |
| 42 | + * the class itself whenever an important property changes. This property should affect the result of the calculated |
| 43 | + * result and by changing the property, that makes the last result invalid. (3) When do you clear the dirty flag? It |
| 44 | + * might seem obvious that the dirty flag should be cleared whenever the result is calculated with up-to-date |
| 45 | + * information but there are other times when you might want to clear the flag. |
| 46 | + * |
| 47 | + * In this example, the {@link DataFetcher} holds the <i>dirty flag</i>. It fetches and re-fetches from <i>world.txt</i> |
| 48 | + * when needed. {@link World} mainly serves the data to the front-end. |
| 49 | + */ |
| 50 | +public class App { |
| 51 | + /** |
| 52 | + * Program execution point |
| 53 | + */ |
| 54 | + public void run() { |
| 55 | + |
| 56 | + final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); |
| 57 | + executorService.scheduleAtFixedRate(new Runnable() { |
| 58 | + @Override |
| 59 | + public void run() { |
| 60 | + World world = new World(); |
| 61 | + List<String> countries = world.fetch(); |
| 62 | + System.out.println("Our world currently has the following countries:-"); |
| 63 | + for (String country : countries) { |
| 64 | + System.out.println("\t" + country); |
| 65 | + } |
| 66 | + } |
| 67 | + }, 0, 15, TimeUnit.SECONDS); // Run at every 15 seconds. |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Program entry point |
| 72 | + * |
| 73 | + * @param args |
| 74 | + * command line args |
| 75 | + */ |
| 76 | + public static void main(String[] args) { |
| 77 | + App app = new App(); |
| 78 | + |
| 79 | + app.run(); |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments