user592748
user592748

Reputation: 1234

Using interface injection while having to create instances of the underlying object

I have an interface to represent a data structure with competing implementations. I need to use this in a class while decoupling the class from having to know the underlying data structure. And within this class, I will need to create several instances of this implementation. How does one do it using interface injection?

class Foo {
  Map<String, IDataStructure> map = new HashMap<String, IDataStructure>();

  public void addValue(String key, String value) {
        if(!map.containsKey(key)) {
             map.put(key, new SomeDataStructure(value));
        }
  }

}

EDIT

I found out an approach to use interface injection. Create a factory interface

class ADataStructureFactory implements DataStructureFactory {
     IDataStructure create() {
         return new SomeDataStructure();
 }
}

And inject this in the constructor

Foo(DataStuctureFactory factory)

Change the add method

public void addValue(String key, String value) {
     if(!map.containsKey(key)) {
           map.put(key, factory.create());
     }
}

Upvotes: 1

Views: 106

Answers (2)

user592748
user592748

Reputation: 1234

I found out an approach to use interface injection. Create an abstract factory.

class ADataStructureFactory implements DataStructureFactory {
     IDataStructure create() {
         return new SomeDataStructure();
 }
}

And inject this in the constructor

Foo(DataStuctureFactory factory)

Change the add method

public void addValue(String key, String value) {
     if(!map.containsKey(key)) {
           map.put(key, factory.create());
     }
}

Upvotes: 0

Chetan Kinger
Chetan Kinger

Reputation: 15212

This is what you can do :

Define an add method in IDataStructure :

public interface IDataStructure {
    public void add(String value);
}

Create an implementation of IDataStrucutre called ListDataStructure as follows :

public class ListDataStructure implements IDataStructure {

    private List<String> dataStructure = new ArrayList<String>();
    @Override
    public void add(String value) {
        dataStructure.add(value);
    }

}

Create an implementation of IDataStructure called SetDataStructure

public class SetDataStructure implements IDataStructure {

    private Set<String> dataStructure = new HashSet<String>();

    @Override
    public void add(String value) {
        dataStructure.add(value);
    }

}

Modify your Foo class as follows :

class Foo {
  private Map<String, IDataStructure> map;

  public Foo(Map<String,IDataStructure> map) {
      this.map = map;
  }

  public void addValue(String key, String value) {
        if(map.containsKey(key)) {
             map.get(key).add(value);
        } else {
           /*handle what happens when data structure does not exist. Maybe thow an exception            
        }
  }

}

Example of how to inject the supported data structures. Note that you cannot define data structures on the fly. You need to prepopulate your map in Foo with the supported implementations of data structures.

public class DataStructureExample {
    public static void main(String []args) {
        Map<String,IDataStructure> dataStrucures = new HashMap<String,IDataStructure>();

        //injecting different data structures into Foo
        dataStrucures.put("List", new ListDataStructure());
        dataStrucures.put("Set", new SetDataStructure());

        Foo foo = new Foo(dataStrucures);

        //add some value to a list data structure
        foo.addValue("List", "Value1");

        //add some valu to a set data structure
        foo.addValue("Set", "Value1");

    }
}

Upvotes: 1

Related Questions