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

.class file created in Java on Ubuntu is not working in cmd in Windows? [duplicate]
Realm data not updated in android using copyToRealmOrUpdate method
Convert PCM audio format to AAC format
Error “class not found” in IntelliJ ide on mac
Lucene TermQuery and BooleanQuery case sensitivity
Sockets server cleint
How to add a password to an existing private key in Java
implement url request in android and getting result
CFG to PDA(Push Down Automata)
threads are WAITING in the ThreadPoolExecutor
Android FileInputStream locked issue
Unable to find plug-in: org.eclipse.fx.javafx when trying to Export Eclipse RCP 4.x + JavaFx as product
Selenium PageFactoryInit - Good Practices. What would be the good way to return new page?
Error while building ROM from source
Program that takes code using I/O dialog box and prints output code onto a colored JFrame panel
Does Octopus Deploy talk to any other repos ( like maven, npm) other than nuGet?

Categories

HOME
log4j
deployment
proxy
mfc
comparison
setup-deployment
cmd
google-project-tango
windows-server
add
facebook-php-sdk
gnupg
reveal.js
kentor-authservices
usergrid
size
ups
hapi
google-static-maps
pugjs
jqwidget
lucene.net
karma-jasmine
java-3d
language-agnostic
dxf
rundeck
java-7
xpages-ssjs
poltergeist
pingfederate
traffic
minitab
calibre
caret
bytecode-manipulation
restlet
ansible-playbook
opshub
nomethoderror
password-encryption
qcombobox
scrollable
azure-application-gateway
zip4j
pnotify
boost-multi-index
mu
yii2-extension
windows-mobile-6.5
nbconvert
background-service
angular-strap
android-fonts
trash
thrust
ctest
mongocsharpdriver
jwplayer7
dlna
captivenetwork
ios4
associative-array
historian
reactive-banana
msys
intel-fortran
ora-00900
coveralls
tarjans-algorithm
service-accounts
event-bubbling
mmc
system32
truevault
tt-news
network-interface
google-reader
starcluster
expected-exception
doskey
pyhdf
ccss
ms-project-server-2010
jsctypes
funscript
frameset
enter
postgresql-performance
gwt-ext
xtype
self-extracting
radcombobox
coda-slider
dmx512
firefox4
nintendo-ds
javap
nerddinner
post-build
django-notification

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App