Java/Design Pattern/FlightWeight Pattern

Материал из Java эксперт
Перейти к: навигация, поиск

Flyweight Pattern in Java

   <source lang="java">

//[C] 2002 Sun Microsystems, Inc.--- import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; public class RunFlyweightPattern {

   public static void main(String [] arguments) throws java.io.IOException{
       System.out.println("Example for the Flyweight pattern");
       System.out.println();
       System.out.println("In this sample, State objects are shared between multiple");
       System.out.println(" parts of the PIM. Two lists, representing a Contact list");
       System.out.println(" and an Address Book, are used for the demonstration.");
       System.out.println(" The State objects - CleanState and DirtyState - represent");
       System.out.println(" the Flyweight objects in this example.");
       System.out.println();
       
       System.out.println("Creating ManagedList objects to hold Contacts and Addresses");
       ManagedList contactList = new ManagedList(Contact.class);
       ManagedList addressList = new ManagedList(Address.class);
       System.out.println();
       
       System.out.println("Printing the State for the application");
       printPIMState();
       System.out.println();
       
       System.out.println("Editing the Address and Contact lists");
       StateFactory.getCurrentState().edit(State.CONTACTS);
       StateFactory.getCurrentState().edit(State.ADDRESSES);
       contactList.addItem(new ContactImpl("f", "l", "t", "o"));
       addressList.addItem(new AddressImpl("d", "s", "c", "st", "z"));
       System.out.println("Printing the State for the application");
       printPIMState();
       System.out.println();
       
       System.out.println("Saving the Contact list");
       StateFactory.getCurrentState().save(new java.io.File("contacts.ser"), contactList.getItems(), State.CONTACTS);
       System.out.println("Printing the State for the application");
       printPIMState();
       System.out.println();
       
       System.out.println("Saving the Address list");
       StateFactory.getCurrentState().save(new java.io.File("addresses.ser"), addressList.getItems(), State.ADDRESSES);
       System.out.println("Printing the State for the application");
       printPIMState();
   }
   
   private static void printPIMState(){
       System.out.println("  Current State of the PIM: " + StateFactory.getCurrentState().getClass());
       System.out.println("  Object ID: " + StateFactory.getCurrentState().hashCode());
       System.out.println();
   }

} interface State{

   public static final int CONTACTS = 1;
   public static final int ADDRESSES = 2;
   public static final int MAXIMUM_STATE_VALUE = 2;
   public void save(File f, Serializable s, int type) throws IOException;
   public void edit(int type);

} class StateFactory {

   public static final State CLEAN = new CleanState();
   public static final State DIRTY = new DirtyState();
   private static State currentState = CLEAN;
   
   public static State getCurrentState(){
       return currentState;
   }
   
   public static void setCurrentState(State state){
       currentState = state;
   }

} class CleanState implements State{

   public void save(File file, Serializable s, int type) throws IOException{ }
   public void edit(int type){
       StateFactory.setCurrentState(StateFactory.DIRTY);
       ((DirtyState)StateFactory.DIRTY).incrementStateValue(type);
   }

} class DirtyState implements State{

   private int stateFlags;
   
   public void save(File file, Serializable data, int stateType) throws IOException{
       FileOutputStream fos = new FileOutputStream(file);
       ObjectOutputStream out = new ObjectOutputStream(fos);
       out.writeObject(data);
       decrementStateValue(stateType);
       if (stateFlags == 0){
           StateFactory.setCurrentState(StateFactory.CLEAN);
       }
   }
   public void edit(int type){
       incrementStateValue(type);
   }
   
   public void incrementStateValue(int stateType){
       if ((stateType > 0) && (stateType <= MAXIMUM_STATE_VALUE)){
           stateFlags = stateFlags | stateType;
       }
   }
   public void decrementStateValue(int stateType){
       if ((stateType > 0) && (stateType <= MAXIMUM_STATE_VALUE)){
           stateFlags = stateFlags ^ stateType;
       }
   }

} interface Address extends Serializable{

   public static final String EOL_STRING = System.getProperty("line.separator");
   public static final String SPACE = " ";
   public static final String COMMA = ",";
   public String getType();
   public String getDescription();
   public String getStreet();
   public String getCity();
   public String getState();
   public String getZipCode();
   public void setType(String newType);
   public void setDescription(String newDescription);
   public void setStreet(String newStreet);
   public void setCity(String newCity);
   public void setState(String newState);
   public void setZipCode(String newZip);

} class AddressImpl implements Address{

   private String type;
   private String description;
   private String street;
   private String city;
   private String state;
   private String zipCode;
   public static final String HOME = "home";
   public static final String WORK = "work";
   
   public AddressImpl(){ }
   public AddressImpl(String newDescription, String newStreet,
       String newCity, String newState, String newZipCode){
       description = newDescription;
       street = newStreet;
       city = newCity;
       state = newState;
       zipCode = newZipCode;
   }
   
   public String getType(){ return type; }
   public String getDescription(){ return description; }
   public String getStreet(){ return street; }
   public String getCity(){ return city; }
   public String getState(){ return state; }
   public String getZipCode(){ return zipCode; }
   
   public void setType(String newType){ type = newType; }
   public void setDescription(String newDescription){ description = newDescription; }
   public void setStreet(String newStreet){ street = newStreet; }
   public void setCity(String newCity){ city = newCity; }
   public void setState(String newState){ state = newState; }
   public void setZipCode(String newZip){ zipCode = newZip; }
   
   public String toString(){
       return street + EOL_STRING + city + COMMA + SPACE +
           state + SPACE + zipCode + EOL_STRING;
   }

} interface Contact extends Serializable{

   public static final String SPACE = " ";
   public String getFirstName();
   public String getLastName();
   public String getTitle();
   public String getOrganization();
   
   public void setFirstName(String newFirstName);
   public void setLastName(String newLastName);
   public void setTitle(String newTitle);
   public void setOrganization(String newOrganization);

} class ContactImpl implements Contact{

   private String firstName;
   private String lastName;
   private String title;
   private String organization;
   
   public ContactImpl(){}
   public ContactImpl(String newFirstName, String newLastName,
       String newTitle, String newOrganization){
           firstName = newFirstName;
           lastName = newLastName;
           title = newTitle;
           organization = newOrganization;
   }
   
   public String getFirstName(){ return firstName; }
   public String getLastName(){ return lastName; }
   public String getTitle(){ return title; }
   public String getOrganization(){ return organization; }
   
   public void setFirstName(String newFirstName){ firstName = newFirstName; }
   public void setLastName(String newLastName){ lastName = newLastName; }
   public void setTitle(String newTitle){ title = newTitle; }
   public void setOrganization(String newOrganization){ organization = newOrganization; }
   
   public String toString(){
       return firstName + SPACE + lastName;
   }

} class ManagedList{

   private ArrayList elements = new ArrayList();
   private Class classType;
   
   public ManagedList(){ }
   public ManagedList(Class newClassType){
       classType = newClassType;
   }
   
   public void setClassType(Class newClassType){
       classType = newClassType;
   }
   
   public void addItem(Object item){
       if ((item != null) && (classType.isInstance(item))){
           elements.add(item);
       } else {
           elements.add(item);
       }
   }
   
   public void removeItem(Object item){
       elements.remove(item);
   }
   
   public ArrayList getItems(){
       return elements;
   }

}

      </source>