Java Reference
In-Depth Information
5.
6. public static State getCurrentState(){
7. return currentState;
8. }
9.
10. public static void setCurrentState(State state){
11. currentState = state;
12. }
13. }
The example tracks collections of items which are held within the class ManagedList . This class makes it
possible to ensure that only classes of a certain type are allowed to be stored in a specific ManagedList .
Example A.175 ManagedList.java
1. import java.util.ArrayList;
2. public class ManagedList{
3. private ArrayList elements = new ArrayList();
4. private Class classType;
5.
6. public ManagedList(){ }
7. public ManagedList(Class newClassType){
8. classType = newClassType;
9. }
10.
11. public void setClassType(Class newClassType){
12. classType = newClassType;
13. }
14.
15. public void addItem(Object item){
16. if ((item != null) && (classType.isInstance(item))){
17. elements.add(item);
18. } else {
19. elements.add(item);
20. }
21. }
22.
23. public void removeItem(Object item){
24. elements.remove(item);
25. }
26.
27. public ArrayList getItems(){
28. return elements;
29. }
30. }
The Address and Contact classes (interface and implementations) provide support for the business objects used
in this pattern.
Example A.176 Address.java
1. import java.io.Serializable;
2. public interface Address extends Serializable{
3. public static final String EOL_STRING = System.getProperty("line.separator");
4. public static final String SPACE = " ";
5. public static final String COMMA = ",";
6. public String getType();
7. public String getDescription();
8. public String getStreet();
9. public String getCity();
10. public String getState();
11. public String getZipCode();
12.
13. public void setType(String newType);
14. public void setDescription(String newDescription);
15. public void setStreet(String newStreet);
16. public void setCity(String newCity);
17. public void setState(String newState);
18. public void setZipCode(String newZip);
19. }
20.
Example A.177 AddressImpl.java
1. public class AddressImpl implements Address{
2. private String type;
3. private String description;
Search WWH ::




Custom Search