Only one class can instantiate another class, how to do this?

3

Hello, I'm doing a UML about an exercise, but I can not do a part of it.

The exercise says that only the administrator class can create another collaborator, I wanted to know how I could implement this.

Example of what I've done so far:

CLASSE Official

package com.trabalho1;

public abstract class Funcionario {

    protected String nome;
    protected String cpf;

    public Funcionario(String nome, String cpf){
        this.nome = nome;
        this.cpf = cpf;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getCpf() {
        return cpf;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

}

CLASS Administrator

package com.trabalho1;

public class Administrador extends Funcionario{

    public Administrador(String nome, String cpf) {
        super(nome, cpf);
    }
}

CLASS Collaborator

package com.trabalho1;

public abstract class Colaborador extends Funcionario{

    public Colaborador(String nome, String cpf) {
        super(nome, cpf);
    }

}

DELIVERY CLASS

package com.trabalho1;

public class Entregador extends Colaborador{

    public Entregador(String nome, String cpf) {
        super(nome, cpf);
    }

}
    
asked by anonymous 30.05.2018 / 20:53

2 answers

1

If only a Administrador can create a Colaborador , it means that no one else can create it, meaning its constructors can not be public. Remove the public from the constructors of Colaborador and its subclasses:

public abstract class Colaborador extends Funcionario {
    // construtor sem "public"
    Colaborador(String nome, String cpf) {
        super(nome, cpf);
    }
}

public class Entregador extends Colaborador {
    // construtor sem "public"
    Entregador(String nome, String cpf) {
        super(nome, cpf);
    }
}

Thus, only classes within the same package "see" the constructors. And since Administrador is the only one that can create these classes and it's in the same package ( com.trabalho1 ), that's exactly what we need.

So, the only point at which a Colaborador can be created is within classes that are in the same package - and Administrador is one of them. And if only Administrador can create these classes, then you create a public method in this class, which will be responsible for creating collaborators.

In addition, Colaborador is an abstract class, and you can not instantiate it. We can only instantiate its subclasses. So one way to do it would be to have a method that would receive the type of collaborator to be instantiated, in addition to the information needed to build it (in this case, the name and CPF).

In the example below I'm using int as the type of collaborator, but in those cases I prefer to use a enum (as I do not know if you already know enum , I'll leave the example with int first ):

public class Administrador extends Funcionario {
    public Colaborador criaColaborador(int tipo, String nomeColaborador, String cpfColaborador) {
        if (tipo == 1) {
            return new Entregador(nomeColaborador, cpfColaborador);
        } else if (tipo == 2) {
            // retorna outro tipo de colaborador (Secretaria, Gerente, etc)
        }
        // vários if's, para cada tipo de colaborador

        // tipo desconhecido, retorna null (ou algum valor default)? lança exception? você decide
        return null;
    }
}

If you already know enum , it looks like this:

public enum TipoColaborador {
    SECRETARIA, GERENTE_VENDA, ENTREGADOR;
}

public class Administrador extends Funcionario {
    public Colaborador criaColaborador(TipoColaborador tipo, String nomeColaborador, String cpfColaborador) {
        if (tipo == TipoColaborador.ENTREGADOR) {
            return new Entregador(nomeColaborador, cpfColaborador);
        } else if (tipo == TipoColaborador.GERENTE_VENDA) {
            // retorna outro tipo de colaborador (Secretaria, Gerente, etc)
        }
        // vários if's, para cada tipo de colaborador

        // retorna null (ou algum valor default)? lança exception? você decide
        return null;
    }
}

I prefer to use enum because this makes it clear which values can be passed in the type. If I use int , I can inadvertently pass some number that has nothing to do with the type, for example.

Remembering that this is just a way to do it. It is also possible to have a method for each type ( criarEntregador , criarSecretaria , etc), use Map to map types and their classes ( Map<TipoColaborador, Class<? extends Colaborador>> ), among others (besides the obvious "use% instead of switch ", if you already know).

But the general idea for your problem is this. By not letting the class have public constructors, you limit and control who can create instances of it. In this case, the only place these constructors are called is in the public method which is in if , which means that only this class can create instances of Administrador .

To create your instances, it would look like this:

Administrador admin = new Administrador("Admin", "12345678909");
Colaborador col = admin.criaColaborador(TipoColaborador.ENTREGADOR, "Fulano de Tal", "11122233396");
    
31.05.2018 / 20:17
0

When you are going to implement the employee registration (if you want to implement this restriction on the functionality), you in the class that performs the registration will verify that the Employee who is logged in to the system is of type Administrator! If it meets the condition you save the employee, otherwise throws an exception stating that the user is not allowed to perform this action on the system.

The other way to solve this problem is by showing the option in the menu for the employee registration if it is an Administrator type (you will have to do this check in the system login), before showing the screen with the options. So when he logs in you will know what options are available to this employee.

    
30.05.2018 / 22:29