SK176H
SK176H

Reputation: 1319

Design pattern for dependencies required by multiple classes (java)

I am writing java code and I have a lot of classes which depend on two classes Say D1 and D2

I created the other classes (A1, B1 ...Z1) to have a constructor which will accept instances of D1 and D2 and thus the dependency requirement is satisfied. Later I am wiring them using Spring.

The wiring is happening in a different maven project and I do not want to introduce any coupling to Spring in this maven project so i am not using the @Autowired annotation.

However my question is that I am not sure if the way I am designing the classes A1 to Z1 is correct ( each of them has the same kind of constructor which is very repetitive)

Is there a design pattern that comes handy in here ?

How would someone design this if there were a lot of classes to depend on say D3 , D4 , and so on ?

Upvotes: 1

Views: 750

Answers (2)

Dima
Dima

Reputation: 40500

You said you were going to use spring to wire the dependencies. If that is the case, than there is no downside to using setters, what you mentioned in a comment does not apply, because you will not be creating the instances explicitly and won't need to initialize them.

So, if your classes are going to be used with spring, I suggest that you make a base class that holds references to your dependencies, with a default constructor, and setD1(), setD2() etc. This way you'll have zero duplicated code.

Otherwise, if you are planning on instantiating your classes explicitly, outside of spring, then you are right, initializing via constructor parameters is, probably, somewhat less painful, than setters.

Alternatively, if not using spring, consider making a factory method, holding the dependency instances:

protected <T extends AbstractPage> initializeInstance(T inst) { 
   inst.setD1(d1);
   inst.setD2(d2);
   return inst;
}
public A1 createA1 { return initializeInstance(new A1()); }
public B1 createB1 { return initializeInstance(new B1()); }

//etc.

Or even just

public <T extends AbstractPage> createInstance(Class<T> clazz) {
    initializeInstance(clazz.newInstance());
}

Upvotes: 2

Allen Kinzalow
Allen Kinzalow

Reputation: 21

I don't want to discourage you from making use of good object oriented practice and efficient use of design patterns, but do not over complicate something if it makes sense to you already. You also do not have to TRY to follow a design pattern(You don't work on something looking for a design pattern to follow just so that you use a pattern).

If A1 IS-A B1 IS-A .... Z1 then of course create a super class, but again don't search for a design pattern for the sake of using a design pattern.

As for the last question, if D1...Dn are all related you could have a super class or implement and interface such as Dependable (in the case that you're explaining it) to pass to the constructors as an array or list if you're passing a number of these depended classes.

Edit: meant to just comment, sorry it's my first post.

Upvotes: 2

Related Questions