| 40 | |
| 41 | === Chain of Responsibility |
| 42 | This pattern is a method of processing data using multiple classes. Each class is responsible for dealing with a particular type of data or output, and passes along any data it doesn't know how to deal with to the next class(es). |
| 43 | |
| 44 | See example: |
| 45 | {{{ |
| 46 | #!java |
| 47 | public class ClawPolicy { |
| 48 | /* instance variables declared; not shown */ |
| 49 | |
| 50 | public void execute() { |
| 51 | if (modeBtn == AUTOMATED) |
| 52 | clawAlgorithmPolicy.execute(); |
| 53 | else if (modeBtn == MANUAL) |
| 54 | clawHandler.execute(); |
| 55 | else |
| 56 | System.out.println("[ERROR: ClawPolicy] IllegalStateException for the mode button"); |
| 57 | } |
| 58 | } |
| 59 | }}} |
| 64 | A singleton is a class which can only ever have one instance of it, generally created by the class itself and accessed through a public accessor method. |
| 65 | |
| 66 | {{{ |
| 67 | #!div class="important" |
| 68 | '''Reasons for Making a Class a Singleton''' |
| 69 | - class contains objects that should not be instantiated more than once |
| 70 | - instances of the class require a lot of processing power, time, or other machine resources |
| 71 | }}} |
| 72 | |
| 73 | See the following sections for various methods of implementing a singleton class in the Java programming language: |
| 74 | ==== Singleton from Enum |
| 75 | A singleton can be written as an java enum with one constant: the instance. |
| 76 | |
| 77 | {{{ |
| 78 | #!java |
| 79 | public enum DatabaseAccessObject { |
| 80 | INSTANCE; |
| 81 | |
| 82 | private DatabaseModel dm; |
| 83 | private SQLConnector sqlconnect = new SQLConnector(/* arguments not shown */); |
| 84 | |
| 85 | private DatabaseAccessObject() { |
| 86 | /* implementation not shown, but will take a long time with a large strain on the machine */ |
| 87 | } |
| 88 | |
| 89 | /* other accessor and modifier methods not shown */ |
| 90 | |
| 91 | } |
| 92 | }}} |
| 93 | |
| 94 | Then, to access this object: |
| 95 | |
| 96 | {{{ |
| 97 | #!java |
| 98 | ... |
| 99 | DatabaseAccessObject dao = DatabaseAccessObject.INSTANCE; |
| 100 | |
| 101 | dao.find(/* args not shown*/); |
| 102 | ... |
| 103 | }}} |
| 104 | |
| 105 | ==== Private Constructor and Public getInstance() |
| 106 | A singleton can also be made using a static getInstance() method and setting the constructor to private. |
| 107 | |
| 108 | {{{ |
| 109 | #!java |
| 110 | public class DAO { |
| 111 | public static DAO instance = new DAO(); |
| 112 | |
| 113 | /** |
| 114 | * Time consuming and resource intensive constructor. |
| 115 | */ |
| 116 | private DAO() { |
| 117 | // implementation not shown |
| 118 | } |
| 119 | |
| 120 | public static DAO getInstance() { |
| 121 | return instance; |
| 122 | } |
| 123 | |
| 124 | /* other methods not shown */ |
| 125 | } |
| 126 | }}} |
| 127 | |
| 128 | Then, to access the object: |
| 129 | |
| 130 | {{{ |
| 131 | #!java |
| 132 | ... |
| 133 | DAO dao = DAO.getInstance(); |
| 134 | |
| 135 | dao.<some method>(); |
| 136 | ... |
| 137 | }}} |