1

Can I have like

private SharableClass object1;
private SharableClass object2;

public ClassA(SharableClass object1, SharableClass object2){
    this.object1=object1;
    this.object2=object2;
}

Then fill the object1 and object2 and share it. Is that possible?

luk2302
  • 55,258
  • 23
  • 97
  • 137
HudBud108
  • 41
  • 1
  • 5
  • what do you mean by sharing? – mstfyldz Mar 04 '19 at 15:51
  • @mstfyldz In ClassA I declare object1 and object2, then fill object1 and object2 in that same class. After that I declare object1 and object2 in ClassB, in here I can get the data that was filled by ClassA. That is what I meant by sharing of data, that is why I am using picocontainer a lightweight DI in my Cucumber steps. Please let me know if you need more information. Thanks. – HudBud108 Mar 04 '19 at 16:01
  • In the context of using Cucumber JVM with Pico Container, then no. You can not inject two different instances of the same class into your step definitions. – M.P. Korstanje Mar 05 '19 at 07:49

4 Answers4

0

If by sharing you mean access to the values of object1 and object2 anywhere in the code then these variables should be static, if you mean that once you have initialized ClassA in your code and you want to access to object1 and object2 then the variables should be public.

Is that what you mean by sharing?

Clarification:

Then the members object1 and object2 must be static, but if you want to keep the private you should declare those variables in a abstract class and ClassA and ClassB inherit that class.

public abstract class ClassAB{
    protected SharableClass object1;
    protected SharableClass object2;
}

public class ClassA extends ClassAB{
    //Change object1 and object2 (changes are applied in all ClassAB)
}

public class ClassB extends ClassAB{
    //Change object1 and object2 (changes are applied in all ClassAB)
}
DomCR
  • 533
  • 4
  • 19
  • #Dom93, by sharing what I mean is that you have declared private SharableClass object1, object2; in both ClassA and ClassB and added the constructor argument. Then in ClassA you do something like object1.name = "Test1"; object2.location="test2" in ClassA and now in ClassB you print that data name and location something like System.out.println(object1.name, object2.location) – HudBud108 Mar 04 '19 at 16:44
  • Dom93 why the abstract class and is this how you share objects of a same class in picocontainer? – HudBud108 Mar 04 '19 at 19:42
  • HudBud108, in your code you don't use picocontainer to share the classes, I suggest to use the abstract class as a solution for your problem, I don't know picocontainer enough to know if there is another way or a specific one. Below @mstfyldz has shared another example but in esence is the same as an abstract class. – DomCR Mar 05 '19 at 11:14
  • I am using pico container. – HudBud108 Mar 06 '19 at 15:03
0

If you declare private object1 and object2 in both classA and classB then you initiliaze these variables in a class that has dependency both classA and classB, may be picoContainer. Then you can populate object data in classA and use that data in classB. Something like below:

public class classC{
 private SharableObject object1;
 private SharableObject object2;

 public classC(){
  //initiliaze objects
  object1 = new SharableObject();
  object2 = new SharableObject();
 }

 private classA aClass = new classA(object1, object2);
 private classB bClass = new classB(object1, object2);

//since they refer same objects you can get data from classB when you change data from classA

}

mstfyldz
  • 482
  • 1
  • 6
  • 12
0

Pico supports collection/array injection, you can have multiple instances of the same type inside the container and then inject them like

public ClassA(List<SharableClass> objects){
  this.objects = objects;
}

public ClassB(List<SharableClass> objects){
  this.objects = objects;
}

then it's easy to do whatever you want with those shared objects inside the collection

xeye
  • 1,250
  • 10
  • 15
  • xeye how do you identify object1 and object2 from the list? So in ClassA you put values in object1, then you add object1 to objects, but then how do you get or refer to object1 in ClassB when you need to access a value in object1? – HudBud108 Mar 06 '19 at 13:55
  • xeye I tried your solution and I am getting org.picocontainer.injectors.AbstractInjector$UnsatisfiableDependenciesException: services.DtoSharingService has unsatisfied dependency. Here the DTOsharing service has List dtos declared in constructor for pico to instantiate. Then in another class I call/share DTOsharing class using pico and then I try to put some value in that list, and I get this error. Your help is appreciated in this. – HudBud108 Mar 06 '19 at 15:03
  • The aDTO class has default constructor and constructor with parameters. I have seen that error happen in those cases. I am trying to see how I can solve this issue. Will update. – HudBud108 Mar 06 '19 at 15:08
  • My example implies you instantiate aDTO somewhere outside and add to the container, how do you handle this? If you want to instantiate them from inside of ClassA, then do this and just inject ClassA in constructor of ClassB, so instance of ClassB can access those properties of ClassA instance – xeye Mar 07 '19 at 09:54
0

This is how I did it.

xDTO{
String xName;
void setxName(String name){ xName = name;}
String getxName(){ return xName;}
public xDTO(){
}
publicxDTO(String name){
this.xName=name;
}

Because shared state XDTO has more than the default constructor, pico container was not able to create the constructor the usual way. So I had to create a "wrapper class" xDTOShare{ ArrayList xDTOs;

public void addToxDTO(XDTO dto){
xDTO xdto1 = new xDTO();
xdto1.setxName(dto.name);
xDTOs.add(xdto1);
}

classA{
private ArrayList<xDTO> dtos;
public classA(ArrayList<xDTO> dtos){
this.dtos = dtos;
dtos.set(new XDTO("test"));
}
classB{
private ArrayList<xDTO> dtos;
public classB(ArrayList<xDTO> dtos){
this.dtos = dtos;
System.out.println(dtos.getXName);
}

Output will be - test

HudBud108
  • 41
  • 1
  • 5