Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Applied Java™ Patterns - Stephen Stelting, Olav Maassen.pdf
Скачиваний:
198
Добавлен:
24.05.2014
Размер:
2.84 Mб
Скачать

Flyweight

This example uses the Flyweight pattern to share common State objects within the PIM. The State pattern example used state objects to edit and store information for a set of Appointments. In this example, the States will be used to manage edits and save for multiple collections of objects.

The State interface provides standard behavior for all application states. It defines two basic methods, edit and

save.

Example A.171 State.java

1. package flyweight.example;

2.

3.import java.io.File;

4.import java.io.IOException;

5.import java.io.Serializable;

7.public interface State {

8.public void save(File f, Serializable s) throws IOException;

9.public void edit();

10.}

State is implemented by two classes— CleanState and DirtyState. This example uses these classes to track the state of multiple objects, so the classes have additional support to track which items need to be refreshed.

Example A.172 CleanState.java

1.import java.io.File;

2.import java.io.FileOutputStream;

3.import java.io.IOException;

4.import java.io.ObjectOutputStream;

5.import java.io.Serializable;

6.

7.public class CleanState implements State{

8.public void save(File file, Serializable s, int type) throws IOException{ }

10.public void edit(int type){

11.StateFactory.setCurrentState(StateFactory.DIRTY);

12.((DirtyState)StateFactory.DIRTY).incrementStateValue(type);

13.}

14.}

Example A.173 DirtyState.java

1. package flyweight.example;

2.

3.import java.io.File;

4.import java.io.FileOutputStream;

5.import java.io.IOException;

6.import java.io.ObjectOutputStream;

7.import java.io.Serializable;

8.

9.public class DirtyState implements State {

10.public void save(File file, Serializable s) throws IOException {

11.//serialize s to f

12.FileOutputStream fos = new FileOutputStream(file);

13.ObjectOutputStream out = new ObjectOutputStream(fos);

14.out.writeObject(s);

15.}

16.

17.public void edit() {

18.//ignored

19.}

20.}

Since these two classes are used to track the overall state of the application, they are managed by a StateFactory class that creates both objects and provides them on demand.

Example A.174 StateFactory.java

1.public class StateFactory {

2.public static final State CLEAN = new CleanState();

3.public static final State DIRTY = new DirtyState();

4.private static State currentState = CLEAN;

312

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.}

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;

313

4.private String street;

5.private String city;

6.private String state;

7.private String zipCode;

8.public static final String HOME = "home";

9.public static final String WORK = "work";

11.public AddressImpl(){ }

12.public AddressImpl(String newDescription, String newStreet,

13.String newCity, String newState, String newZipCode){

14.description = newDescription;

15.street = newStreet;

16.city = newCity;

17.state = newState;

18.zipCode = newZipCode;

19.}

20.

21.public String getType(){ return type; }

22.public String getDescription(){ return description; }

23.public String getStreet(){ return street; }

24.public String getCity(){ return city; }

25.public String getState(){ return state; }

26.public String getZipCode(){ return zipCode; }

27.

28.public void setType(String newType){ type = newType; }

29.public void setDescription(String newDescription){ description = newDescription; }

30.public void setStreet(String newStreet){ street = newStreet; }

31.public void setCity(String newCity){ city = newCity; }

32.public void setState(String newState){ state = newState; }

33.public void setZipCode(String newZip){ zipCode = newZip; }

34.

35.public String toString(){

36.return street + EOL_STRING + city + COMMA + SPACE +

37. state + SPACE + zipCode + EOL_STRING;

38.}

39.}

Example A.178 Contact.java

1.import java.io.Serializable;

2.public interface Contact extends Serializable{

3.public static final String SPACE = " ";

4.public String getFirstName();

5.public String getLastName();

6.public String getTitle();

7.public String getOrganization();

8.

9.public void setFirstName(String newFirstName);

10.public void setLastName(String newLastName);

11.public void setTitle(String newTitle);

12.public void setOrganization(String newOrganization);

13.}

Example A.179 ContactImpl.java

1.public class ContactImpl implements Contact{

2.private String firstName;

3.private String lastName;

4.private String title;

5.private String organization;

6.

7.public ContactImpl(){}

8.public ContactImpl(String newFirstName, String newLastName,

9.String newTitle, String newOrganization){

10. firstName = newFirstName;

11. lastName = newLastName;

12. title = newTitle;

13. organization = newOrganization;

14. }

15.

16.public String getFirstName(){ return firstName; }

17.public String getLastName(){ return lastName; }

18.public String getTitle(){ return title; }

19.public String getOrganization(){ return organization; }

21.public void setFirstName(String newFirstName){ firstName = newFirstName; }

22.public void setLastName(String newLastName){ lastName = newLastName; }

23.public void setTitle(String newTitle){ title = newTitle; }

24.public void setOrganization(String newOrganization){ organization = newOrganization; }

314

25.

26.public String toString(){

27.return firstName + SPACE + lastName;

28.}

29.}

RunPattern provides a way to test the Flyweight. It creates ManagedList objects for addresses and contacts, then uses common State objects to manage saving the objects to two different files.

Example A.180 RunPattern.java

1.public class RunPattern{

2.public static void main(String [] arguments) throws java.io.IOException{

3.System.out.println("Example for the Flyweight pattern");

4.System.out.println();

5.System.out.println("In this sample, State objects are shared between multiple");

6.System.out.println(" parts of the PIM. Two lists, representing a Contact list");

7.System.out.println(" and an Address Book, are used for the demonstration.");

8.System.out.println(" The State objects - CleanState and DirtyState - represent");

9.System.out.println(" the Flyweight objects in this example.");

10.System.out.println();

11.

12.System.out.println("Creating ManagedList objects to hold Contacts and Addresses");

13.ManagedList contactList = new ManagedList(Contact.class);

14.ManagedList addressList = new ManagedList(Address.class);

15.System.out.println();

16.

17.System.out.println("Printing the State for the application");

18.printPIMState();

19.System.out.println();

20.

21.System.out.println("Editing the Address and Contact lists");

22.StateFactory.getCurrentState().edit(State.CONTACTS);

23.StateFactory.getCurrentState().edit(State.ADDRESSES);

24.contactList.addItem(new ContactImpl("f", "l", "t", "o"));

25.addressList.addItem(new AddressImpl("d", "s", "c", "st", "z"));

26.System.out.println("Printing the State for the application");

27.printPIMState();

28.System.out.println();

29.

30.System.out.println("Saving the Contact list");

31.StateFactory.getCurrentState().save(new java.io.File("contacts.ser"),

contactList.getItems(), State.CONTACTS);

32.System.out.println("Printing the State for the application");

33.printPIMState();

34.System.out.println();

35.

36.System.out.println("Saving the Address list");

37.StateFactory.getCurrentState().save(new java.io.File("addresses.ser"),

addressList.getItems(), State.ADDRESSES);

38.System.out.println("Printing the State for the application");

39.printPIMState();

40.}

41.

42.private static void printPIMState(){

43.System.out.println(" Current State of the PIM: " + StateFactory.

getCurrentState().getClass());

44.System.out.println(" Object ID: " + StateFactory.getCurrentState(). hashCode());

45.System.out.println();

46.}

47.}

315