Fly weight Factory : Factory Pattern « Design Pattern « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Design Pattern » Factory PatternScreenshots 
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
w_ww___.___ja___v_a__2s___.c___o_m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.