lundi 11 novembre 2019

Java: make package level constructor in a class accessible for outer packages. Allow only certain classes inside project access to constructor

I'm developing a library and trying to keep everything organized. Thus, I have nested package structure and a single class that is supposed to have access to constructors to all other classes:

src
   main
      java
         zoo
            Zoo.java
            animals
               Animal.java
               mammals
                  Dog.java
               fish
                  Salmon.java
               birds
                  Falcon.java
Zoo zoo = new Zoo(password);
Animal dog = zoo.getDog(name);
Animal salmon = zoo.getSalmon(name);
Animal falcon = zoo.getFalcon(name);

I want to prevent a user from being able to create an animal directly:

Animal dog = new Dog("Charlie");  //error - constructor isn't public

However I'm not sure how to accomplish it. I could put all classes inside the zoo package and make animal constructors only package-level accessible. However, I would have to sacrifice the package structure and I'd rather not do that.

Zoo:

public class Zoo {
    private String password;

    public Zoo(String password)  {this.password = password;}

    public Dog getDog(String name)  {return new Dog(name);}

    public Salmon getSalmon(String name)  {return new Salmon(name);}

    public Falcon getFalcon(String name)  {return new Falcon(name);}
}

Animal:

public abstract class Animal {
    protected String name;
    public Animal(String name)  {this.name = name;}
}

animals:

public class Dog extends Animal {
    public Dog(String name) {super(name);}
}
public class Salmon extends Animal {
     public Salmon(String name) {super(name);}
}
public class Falcon extends Animal {
    public Falcon(String name)  {super(name);}
}

So what can be done to accomplish the above? Pehaps there exists some pattern for it?

Aucun commentaire:

Enregistrer un commentaire