I have the following DataSource class:
public class MyDataSource extends DataSource {
private static MyDataSource instance;
private static final String COLUMN_ONE = "One";
private static final String COLUMN_TWO = "Two";
public static MyDataSource getInstance() {
if (instance == null) {
instance = new MyDataSource("ID");
}
return instance;
}
private MyDataSource(String id) {
setDataProtocol(DSProtocol.CLIENTCUSTOM);
setDataFormat(DSDataFormat.CUSTOM);
setClientOnly(false);
constructDataSource(id);
}
private void constructDataSource(String id) {
setID(id);
DataSourceTextField one = new DataSourceTextField(COLUMN_ONE);
DataSourceTextField two= new DataSourceTextField(COLUMN_TWO);
setFields(one, two);
}
// and the rest of the class
}
I create an instance of this class from another class via MyDataSource ds = MyDataSource.getInstance();
and upon some actions, I need to destroy and recreate it. I do ds.destroy(); ds = MyDataSource.getInstance();
but I noticed that instance
is not null after the destruction, so basically the second call to MyDataSource.getInstance();
is returning the old object to me. How can I destroy this instance?
Your datasource follows the singleton pattern. You are not "creating an instance of this class" since the static getInstance
method always return the same instance. I'm not sure how destroy()
is implemented in the com.smartgwt.client.core.BaseClass
but if you want to destroy your datasource, maybe you should override this method and set instance
to null
. This will enforce the creation of a new instance in the getInstance
method.