What I'm doing
I'm using Dependency Injection to decouple my classes.
How I'm trying to do it
The class I am making constructs ObjectImplementation
(the interface) Objects to hold data and it acts as a sort of container. I'm doing this to parse data and cross reference two data-sets. My problem is that I want to decouple object construction so this container class can hold any object that implements the ObjectImplementation
interface. I am using the Factory
pattern and a properties file "config.properties"
.
What I want to be able to do
I want to be able to have the factory take in an array of fields or some other similar type and be able to construct instances of the reflected object type without dependencies. In this case they are Salesrep
instances but other times I want to construct Salesrep
instances with different fields filled and different ones null
.
The end goal
The point is so that I can construct different objects with the same container code. If I want to contain the objects differently I'll simply make a new implementation of the parent interface of this container class.
What I'm thinking is the problem
I don't know if its even possible to decouple object construction, maybe some editing of the data to fill in certain fields based on a String
because the fields are contained as a String[]
so maybe if I paired them with the name of the field they need to fill in and then used reflection to match the field name of the class instance I want to construct but that seems convoluted and wrong and it might introduce dependencies I'm not thinking about.
Extra Clarification
Some pointers or a general direction would be very helpful, Yes I have tried google and stack overflow before asking this but all questions I searched through weren't related to the specific problem of decoupling object creation. Also If the way I'm asking this is terrible feel free to ream me out for it.
//creates new properties object and loads in the file configuration
Properties prop = new Properties();
prop.load(SalesRepbyId.class.getResourceAsStream("config.properties"));
//reflects in the class we wish to use
Class<? extends ObjectImplementation> Classtouse = Class.forName(prop.getProperty("ObjectImplementation")).asSubclass(ObjectImplementation.class);
//initializes the data and some hashmaps to store the data or the methods of the reflected class
ArrayList<String[]> Salesrep_contactlist = FileParser.ReadFile();
Map<String, ObjectImplementation> SalesrepByIdMap = new HashMap<>();
Map<String, Method> MethodMap = new HashMap<>();
//adds in the data (fields) by constructing objects of the reflected type using the ObjectImplementation interface
for (String[] fieldarray : Salesrep_contactlist) {
ObjectImplementation object_to_add = null;
try {
//utilizes the factory pattern to return an instance of the reflected class
object_to_add = Factory.getObjectImpl(prop.getProperty("ObjectImplementation"),fieldarray);
/**
uses a method hashmap to map the name of the method to the Method object.
I did it this way because dynamic variable declarations are not possible and
I wanted to decouple Method declarations from the specific class that has
them. If i just hardcoded in which methods I get from the implementing class
that introduces extra dependencies I don't want.
**/
for (Method method:Classtouse.getMethods()) {
MethodMap.put(method.getName(),method);
}
for (Field field:Classtouse.getFields()) {
//don't know if this is the right approach but I want something to decouple the fields of the class
}
Aucun commentaire:
Enregistrer un commentaire