java


Constructor for a class whose super should only be made via factory method


I have the following class:
public class Foo(){
int parameter;
static Set<Foo> cache=new HashSet<Foo>();
public Foo(int parameter){
this.parameter=parameter;
addToCache(this);
}
public static Foo Factory(int parameter){
Foo duplicate=findDuplicate(parameter);
if (duplicate!=null){
return duplicate;
}else{
return new Foo(parameter);
}
}
}
Notice that calling Foo's constructor directly will add to the static cache.
I now need to subclass this object to add some functionality.
public class Bar() extends Foo{
public Bar(int parameter){
//Danger
}
}
But now I'm stuck. Bar's constructor has to call super() one way or another, but that won't check for duplicates like Foo.Factory() does.
What I would really like would be something like:
public Bar(int parameter){
this=Foo.Factory(parameter);
}
But that's obviously not valid java.
For now, I have been forced to write a hacky secondary constructor for Foo that also checks for duplicates, and have Bar use that:
//Second unused parameter just so the constructors are different
public Foo(int parameter, boolean isEvil){
Foo duplicate= findDuplicate(parameter);
if (duplicate!=null){
this.copy(duplicate); //Evilly take on all attributes of duplicate
}else{
//Now we have to copy the body of the original constructor.
//It has to be kept synched forever, and I can't even call it!
this.parameter=parameter;
addToCache(this);
}
}
Bar(int parameter){
super(int,true);
}
But this has the problem of always creating a new object, which can cause mutability and hashing concerns. Furthermore, anyone not paying attention won't be able to tell that this constructor works differently.
TLDR: How do I make a constructor for a class whose super should only be made via a factory method.
Possible duplicate of this question, but in java (also that question had only one answer and it was unsatisfying to both me and OP)
The way I see it, you have two options.
Options 1 is to create a factory method for bar instead of a public constructor.
Option 2 is that, instead of making bar inherit from foo, it instead contains an instance of foo as a member. In the constructor you can call the factory method for foo.
Which way you go probably depends on the details.

Related Links

stack memory allocation is it done in processor/RAM
Two Dimensional Array | Java
NoClassDefFoundError - external jar file
How to parse string into BigDecimal uniformly?
Change ImageIcon during
Java looping to take away fractional numbers?
Gradle 1.7 with Java 1.8: compileGroovy error?
How to Query search index on cloudant using java?
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after statement closed
Array<Vector2> How to define it?
Simple syntax error
Implementing Linked List in JAVA (Reverse without using iterator)
Char not adding to my string when i println?
Convert Generic Arraylist into an array with Generic Type
Android/Java Asynchronous Image Download Task Never Stops Running
NullPointerException during getSize

Categories

HOME
xamarin
pdf
google-play
sd-card
octobercms
yarn
jsrender
constraint-programming
gorm
vault
quickbooks
n-gram
task
progressive-web-apps
shared-hosting
visjs
paging
cx-freeze
csrf-protection
telerik-reporting
autosys
javacv
sparse-matrix
semantic-analysis
wpfdatagrid
tooltipster
social-media
fifo
windowbuilder
nouislider
semantic-versioning
google-qpx-express-api
http-referer
bytecode-manipulation
wixsharp
git-merge
bitbucket-pipelines
swisscomdev
libvpx
reportbuilder
document.write
streamreader
user-controls
gzipstream
http-live-streaming
fault
react-scripts
jquery-validate
worksheet
idisposable
carrot
feeds
crypt
mako
mcafee
orthogonal
quartz-composer
angular-cache
mongocsharpdriver
storekit
websitepanel
whois
livequery
maven-tomcat-plugin
icu4j
thredds
marching-cubes
intellij-14
sankey-diagram
ng-animate
java.util.concurrent
socketexception
rdl
elliptic-curve
bundles
mbr
imdbpy
jsctypes
odata4j
dotnetnuke-5
excel-2003
sqlperformance
hosts-file
spring-portlet-mvc
gnustep
tomcat-valve
pyinotify
gdlib
paster
castle-monorail
firefox-5
zend-tool
kdbg
ti-dsp
commodore
signal-handling
paul-graham

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App