Programming a one-to-many relationship

后端 未结 8 791
醉话见心
醉话见心 2020-12-25 08:16

So I am surprised that doing a search on google and stackoverflow doesn\'t return more results.

In OO programming (I\'m using java), how do you correctly implement a

8条回答
  •  轮回少年
    2020-12-25 08:26

    You can ensure that there are no duplicates by using a Set implementation like HashSet instead of using other data-structure. And instead of adding Job to a customer, create an final inner class in Job class that has private constructor. That ensure that the wrapper inner class can only be created by a job object. Make you Job constructor take in jobID and customer as parameter. To maintain consistency -if customer is Null throw Exception as dummy jobs shouldn't be created .

    In add method of Customer, check to see if the Job wrapped by JobUnit has the same customer ID as the its own id, if not throw Exception.

    When replacing a customer in Job class remove the JobUnit using the method provided by Customer class and add itself to the new customer and change the customer reference to the newly passed customer. That way you can reason with your code better.

    Here's what your customer class might look like.

    public class Customer {
    
        Set jobs=new HashSet();    
        private Long id;
        public Customer(Long id){        
            this.id = id;
        }
    
        public boolean add(JobUnit unit) throws Exception{
           if(!unit.get().getCustomer().id.equals(id))
               throw new Exception(" cannot assign job to this customer");
            return jobs.add(unit);
        }
    
         public boolean remove(JobUnit unit){
            return jobs.remove(unit);
        }
    
        public Long getId() {
            return id;
        }
    
    }
    

    And the Job Class:

    public class Job {
    Customer customer;
    private Long id;
    

    final JobUnit unit;

    public Job(Long id,Customer customer) throws Exception{
        if(customer==null)
            throw new Exception("Customer cannot be null");
        this.customer = customer; 
       unit= new JobUnit(this);       
        this.customer.add(unit);
    }
    
    public void replace(Customer c) throws Exception{      
        this.customer.remove(unit);
        c.add(unit);
        this.customer=c;
    }
    
    public Customer getCustomer(){
        return customer;
    }
    
    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }
    
    public final class JobUnit{
        private final Job j;
    
    
        private JobUnit(Job j){
            this.j = j;
    
        }
        public Job get(){
            return j;
        }
     }
    }
    

    But one thing I'm curious about is why do you even need to add jobs to a customer object? If all you want to check is to see which customer has been assigned to which job, simply inspecting a Job will give you that information. Generally I try not to create circular references unless unavoidable. Also if replacing a customer from a job once its been created is not necessary, simply make the customer field Final in the Job class and remove method to set or replace it.

    The restriction for assigning customer for a job should be maintained in database and the database entry should be used as a checking point. As for adding jobs to customer that were done for someone else, you can either check for customer reference in a job to ensure that the customer to which a job is being added is the same one it holds or even better-simply remove any reference in customer for Job and it will simplify things for you.

提交回复
热议问题