Fly weight Factory : Factory Pattern « Design Pattern « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Design Pattern » Factory Pattern 




Fly weight Factory
Fly weight Factory

/*
Software Architecture Design Patterns in Java
by Partha Kuchana 

Auerbach Publications

*/


import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;

public class FlyweightTest {

  public static void main(String[] argsthrows Exception {
    Vector empList = initialize();
    FlyweightFactory factory = FlyweightFactory.getInstance();

    for (int i = 0; i < empList.size(); i++) {
      String s = (StringempList.elementAt(i);
      StringTokenizer st = new StringTokenizer(s, ",");
      String name = st.nextToken();
      String title = st.nextToken();
      String division = st.nextToken();

      FlyweightIntr flyweight = factory.getFlyweight(division);
      //associate the flyweight
      //with the extrinsic data object.
      VCard card = new VCard(name, title, flyweight);
      card.print();
    }
  }

  private static Vector initialize() {
    //for simplicity values are being hardcoded.

    Vector v = new Vector();
    v.add("name1,title1,North");
    v.add("name2,title2,South");
    v.add("name3,title1,North");
    v.add("name4,title3,East");
    v.add("name5,title4,East");
    v.add("name6,title2,East");
    v.add("name7,title1,West");
    v.add("name8,title3,West");
    v.add("name9,title1,West");
    v.add("name10,title6,South");
    v.add("name11,title5,North");
    v.add("name12,title1,North");

    return v;
  }
}

class FlyweightFactory {
  private HashMap lstFlyweight;

  private static FlyweightFactory factory = new FlyweightFactory();

  private FlyweightFactory() {
    lstFlyweight = new HashMap();
  }

  public synchronized FlyweightIntr getFlyweight(String divisionName) {
    if (lstFlyweight.get(divisionName== null) {
      FlyweightIntr fw = new Flyweight(divisionName);
      lstFlyweight.put(divisionName, fw);
      return fw;
    else {
      return (FlyweightIntrlstFlyweight.get(divisionName);
    }
  }

  public static FlyweightFactory getInstance() {
    return factory;
  }

  //Inner flyweight class
  private class Flyweight implements FlyweightIntr {
    private String company;

    private String address;

    private String city;

    private String state;

    private String zip;

    private void setValues(String cmp, String addr, String cty, String st,
        String zp) {

      company = cmp;
      address = addr;
      city = cty;
      state = st;
      zip = zp;

    }

    private Flyweight(String division) {
      // values are hard coded
      //for simplicity
      if (division.equals("North")) {
        setValues("CMP""addr1""cty1""st1""10000");
      }
      if (division.equals("South")) {
        setValues("CMP""addr2""cty2""st2""20000");
      }
      if (division.equals("East")) {
        setValues("CMP""addr3""cty3""st3""30000");
      }
      if (division.equals("West")) {
        setValues("CMP""addr4""cty4""st4""40000");
      }
    }

    public String getCompany() {
      return company;
    }

    public String getAddress() {
      return address;
    }

    public String getCity() {
      return city;
    }

    public String getState() {
      return state;
    }

    public String getZip() {
      return zip;
    }

  }// end of Flyweight
}// end of FlyweightFactory

interface FlyweightIntr {
  public String getCompany();

  public String getAddress();

  public String getCity();

  public String getState();

  public String getZip();
}

class VCard {

  String name;

  String title;

  FlyweightIntr objFW;

  public VCard(String n, String t, FlyweightIntr fw) {
    name = n;
    title = t;
    objFW = fw;
  }

  public void print() {
    System.out.println(name);
    System.out.println(title);
    System.out.println(objFW.getAddress() "-" + objFW.getCity() "-"
        + objFW.getState() "-" + objFW.getZip());
    System.out.println("----------------");
  }

}

           
       














Related examples in the same category
1.Abstract Factory Pattern- Example
2.Abstract Factory Pattern in Java 2
3.Factory Method Pattern in Java
4.Illustrates use of Abstract Factory patternIllustrates use of Abstract Factory pattern
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.