How do I make a class method available only to another class?

In addition, there is the Worker class, in which one of its attributes is the WorkOrder object queue and the Boss class, which is the only one that can add or remove new WorkOrders to that queue. How can you make a way that changes the queue only accessible by the "Boss"?

I thought of two solutions:

(a) Make the method package-private, keep the two classes in one package, and create them in the other.

(b) Create an interface with methods that can be accessed by the worker, make the constructor of this private class, and instead create a static method that returns an object of the same type as the interface.

What is your opinion? Can you think of a more elegant solution?

+3


source to share


2 answers


Consider having an interface that provides the methods of work that you want to publish. The host can contain a reference to the implementation (which has a work order queue queue) and only represents an interface to other classes.



import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;

class Class {
  public static void main(final String... args) {
    final Boss boss = new Boss();
    final Worker worker = boss.getWorker("sivmish");
    worker.getWorkItems();//ERROR: only Boss has access to this.
  }
}

interface Worker {
  void processWork();
}

interface WorkOrder {
  void doWork();
}

class WorkerImpl implements Worker {
  private final Queue<WorkOrder> workItems;

  WorkerImpl() {
    this.workItems = new ArrayDeque<>();
  }

  public Queue<WorkOrder> getWorkItems() {
    return workItems;
  }

  @Override
  public void processWork() {
    while (!workItems.isEmpty()) {
      workItems.poll().doWork();
    }
  }
}

class Boss {
  private final Map<String, WorkerImpl> workersByName;

  Boss() {
    workersByName = new HashMap<String, WorkerImpl>();
  }

  public Worker getWorker(final String name, WorkOrder... workOrders) {
    if (!workersByName.containsKey(name)) {
      final WorkerImpl worker = new WorkerImpl();
      workersByName.put(name, worker);
    }
    final WorkerImpl worker = workersByName.get(name);
    worker.getWorkItems().addAll(Arrays.asList(workOrders));
    return worker;
  }
}

      

+1


source


I would prefer to keep the queue in the Boss class because you want to make it Boss only available to add or remove



class Boss
    {
    public LinkedList<E> queue = new LinkedList<E>(); 
    private void add(item)
    {
    //do stuff...
    }
    private void remove()
    {//do stuff...}
    public static void viewOnly()
    {
      //display details
    }

}

class Workers
    { 
          public workers() 
          {
             Boss.viewOnly();
          }
    }

class Main
{
    public static void main(String args[])
     {    
          Boss b = new Boss();
            b.add(1); //job 1
            b.add(5); //job 5
               /* and so on..

              */
        workers w = new workers();
    }
}

      

0


source







All Articles