简体   繁体   中英

Factory pattern and complex beans

I'm trying to apply a factory pattern for creating request beans to use on a protocol stack. Now the request beans hold properties with other beans - which also should be part of the factory pattern (as they are different depending on the stack).

Something like:

public interface Factory {
  public Request createRequest();
}

public interface Request {
  public Details getDetails();
  public void setDetails(Details details);
  ..
}

public interface Details {
  public String getSource();
  public void setSource(String s);
  ..
}

My first attempt was to add factory methods for Details as well, but this quickly becomes a hazard - especially pass some arguments for the factory.

Also the setters become a bit weird as they actually throw a ClassCastException if you were to pass an ´Details´ implementation from another factory.

The main reason for my situation is that I'm sitting on a rather complex 3rd party request/response/stack implementation which I want to fit in under my own bean interfaces. Is there a more sensible way to do this?

You might look more into your design requirement : Which one has more different variants or implementations. Make that into a factory and leave the other. In this case it looks to me as Details can be created using a factory . (If request is not much implemented in many different ways.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM