I have a family of classes in swift 4.2 and I want to restrict the creation of those instances only to the factory class, in C++ I can enforce this by declaring the constructors as private and add the friend keyword to the factory method like this:
class A{
friend factoryClass::createInstance(int type);
private A();
}
class subA: private A{
friend factoryClass::createInstance(int type);
private subA() : A(){
}
}
class factoryClass{
static A* createInstance(int type){
switch(type){
case 0:
return new A();
case 1:
default:
return new subA();
}
}
}
Is it possible to do this in swift 4.2? I'm pretty a new on this.
Aucun commentaire:
Enregistrer un commentaire