I wanted to verify is this a good example of a flyweight design pattern:

package com.flyweight;

public interface Chemical {
    String getName();
    String getSymbol();
    Double getAtomicWeight();
}


package com.flyweight;

import java.util.HashMap;
import java.util.Map;

public class ChemicalFactory {
      private static Map chemicals = new HashMap();
      private static ChemicalFactory factory = new ChemicalFactory();

      class ChemicalImpl implements Chemical{

            private String name;
            private String symbol;
            private Double atomicWeight;

            public ChemicalImpl(String name, String symbol, Double atomicWeight){
                this.name = name;
                this.symbol = symbol;
                this.atomicWeight = atomicWeight;
            }

            public String getName() {
                return name;
            }

            public String getSymbol() {
                return symbol;
            }

            public Double getAtomicWeight() {
                return atomicWeight;
            }

      }

      static{
          chemicals.put("Sulfer", factory.new ChemicalImpl("Sulfer","S",32.0));
          chemicals.put("Carbon", factory.new ChemicalImpl("Carbon","C",12.0));
          chemicals.put("Saltpeter", factory.new ChemicalImpl("Saltpeter","KN03",101.0));
      }

      public static Chemical getChemical(String name){
          return (Chemical) chemicals.get(name);
      }
}


package com.flyweight;

public class Substance {

    private Double grams;
    private Chemical chemical;

    public Substance(Double grams,Chemical chemical){
        this.grams = grams;
        this.chemical = chemical;
    }

    public String getName() {
        return chemical.getName();
    }

    public String getSymbol() {
        return chemical.getSymbol();
    }

    public Double getAtomicWeight() {
        return chemical.getAtomicWeight();
    }   

    public Double getGrams() {
        return grams;
    }

    public String toString(){
        return getName() + " (" + getSymbol() + ") [" + getAtomicWeight() + "]";
    }
}


package com.flyweight;

public class Test {
      public static void main(String[] args){
          Chemical c = ChemicalFactory.getChemical("Sulfer");
          Substance s = new Substance(50.0,c);
          System.out.println(s);
      }
}

Your help is kindly appreciated.

Thank You.

try to debug first to see what is an error..:)

No error :) I coded the above according to the an explanation on how to apply the flyweight design pattern :)

Looks good though there is a small issue. Normally the factory method/classes used for the flyweight pattern have the same signature as the constructor of the object which you want to cache. Your solution looks more like a static cache since there is no capability of creating new chemicals on the fly if they don't exist.

The client code should ideally use the factory as follows Chemical c = ChemicalFactory.getChemical("Sulfer", "S", 32.0);.

On a related note, if you already know the number of items you can have in all, prefer using an Enum with an helper class, which offers a bit more type safety.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.