This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Confused about OOD Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Confused about OOD " Watch "Confused about OOD " New topic
Author

Confused about OOD

Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
Hi. Im trying to a small module and in creating the module I figured I would need a factory method for two classes.

Here is my factory class.

public FormatObjFactory{

public static FormatObj createFormatObj(String format){

if (format.equalsIgnoreCase("format1"){
return new Format1();
}else if (format.equalsIgnoreCase("format2"){
return new Format2();
}else{
return null;
}

}
}

Both Format1 and Format2 classes implement the FormatObj interface.

public class Format1 implements FormatObj{

}

public class Format2 implements FormatObj{

}

The interface contains two method signature.

public interface FormatObj{
public void createTitle();
public void createBody();
}


My problem here is I need to pass a list of specific data object per specific implementation. Meaning, if you instantiate it this way...

FormatObj obj = FormatObjFactory.createFormatObj("format1);
obj.createTitle(); // this is ok... they will have their own way of implementing the creation of title...
obj.createBody(); // I want them to have their own implementation of creating a body, however the method would need a specific object per specific implementation (meaning... format1 needs List<SpecificDTOForFormat1> list, format 2 needs List<SpecificDTOForFormat2> list)...... should have been like this ---> obj.createBody(List<SpecificDTOForFormat1> list);

The object SpecificDTOForFormat1 and SpecificDTOForFormat2 are not related at all. I was thinking if both of these objects extend from the same parent class...i could have made one of my interface methods like this...

public void createBody(List<ParentClass> listObject);

so if I do obj.createBody(List<SpecificDTOForFormat1> list) ... i think this will be accepted because they come from the same hierarchy. Unfortunately I cant edit the object structure, I just came in to write a small module and make it as a generic as possible, implement it the OOP way.

Anyone has any suggestions?



Be Afraid...Be very Afraid...
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

You could define an Adapter base class and have Format1Adapter and Format2Adapter as descendants. Then declare the createBody to take an Adapter parameter. FormatNAdapter would take the corresponding FormatNDTO as a constructor argument and do the adapting of whatever the DTO provides to whatever the Formatter needs.

The design still doesn't seem that nice though but using the Adapter might give you ideas for further refactoring.


Junilu - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

I know you're probably (hopefully?) using different names in your actual code but as a matter of style, it's quite redundant to put an 'Obj' suffix on your class names. Java is, after all, an object-oriented language. I'd advise using names like FooFormatter and BarFormatter for concrete format classes for Foo and Bar, respectively, and Formattable or Formatter for the interface name.
Sunderam Goplalan
Ranch Hand

Joined: Oct 10, 2011
Posts: 73
Paulo,

public void createBody(List<ParentClass> listObject)
so if I do obj.createBody(List<SpecificDTOForFormat1> list) ... i think this will be accepted because they come from the same hierarchy


The above won't be accepted because the "Generics" Syntax does NOT permit passing arguments this way. In other words, if the createBody's Signature is

public void createBody(List<ParentClass>...)

Then, the Compiler would restrict you from passing subclasses of "ParentClass" in to the List argument. You can only pass ParentClass objects in to the method. This was done to
protect the collection from becoming heterogeneous at runtime. However you should be able to pass List<SpecificDTOForFormat1> as you want if the method signature is

public void createBody(List<? extends ParentClass> format1List)

The latter allows you to pass a subclass of ParentClass. You should be able to implement createBody() by reading data from the format1List collection, but NOT
try to add anything to it. (format1List.add(....) results in error). The "extends" syntax is meant for read-only collections.

But you could go with the Adaptor approach suggested.


SCJP 5.0 , SCEA Java EE 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused about OOD
 
Similar Threads
Unparseable date exception
Generic Singleton Class
Implementing interface with list of generics
Design Patterns Factory Method example
setMaximumFractionDigits : Digits After Decimal Point without Rounding