Design issue to Create Object in Java
I am developing an application in which subscriber send SMS to get any package.
In my program, when I received user request, I have to create Subscriber
object. To initialize subscriber object, I have to query back-end database and IN node then set attributes of subscriber object.
Please suggest for this, should I use Factory pattern or Builder? or any good alternative? whats suggestions for putting my code for querying database and calling XMLRPC in Factory class?
My class look like following
public class Subscriber {
private String subno;
private String 开发者_如何学GosubPackageType;
private String subTariff;
private String subRequest;
private boolean isTransferable;
}
Now subno,subRequest i got from sms, subPackageType from database and subTariff from IN node by sending XMLRPC command.
I have more attributes as well but i just mention some to provide concept.
Regards,
imranStop thinking "which pattern should I use" as if any application exists of a single pattern and that's it, that just applying a specific pattern will mysteriously fix the world.
Start writing, and a pattern will emerge. Eventually, with experience, that will become second nature. Don't try to force things into a pattern, let it grow and mature.
First using factory is a good practice anyway. Second in your case I think that you should retrieve the data from DB and then create the Subscriber object. In this case you actually pass the data from DB to the subscriber object during creation. If for some other reason you cannot do it just make Subscriber to be a bean, i.e. provide appropriate setters. In this case you can create object, then query DB and then set all needed properties by calling appropriate setter.
It seems that Builder pattern could not help you too much here.
精彩评论