Base on my understand a mediator is a central authority that varies the interaction between objects in the same group. So can I classify the code below as a mediator?

package com.mediator;

public class BaseMediator {
    public void store(){
         System.out.println("store");
    }
    public void retrieve(){
         System.out.println("retrieve");
    }
}



package com.mediator;

public class Consumer extends BaseMediator {

     private BaseMediator b;

     public Consumer(BaseMediator b){
         this.b = b;
     }
}



package com.mediator;

public class Producer extends BaseMediator {

     private BaseMediator b;

     public Producer(BaseMediator b){
         this.b = b;
     }
}



package com.mediator;

public class Driver {
    public static void main(String[] args){
        BaseMediator mediator = new BaseMediator();
        Producer p = new Producer(mediator);
        Consumer c = new Consumer(mediator);
        p.store();
        c.retrieve();
    }
}

Your help is kindly appeciated.

Thank You.

Traditionally a mediator has at least two colleagues and it has pointers to its colleagues. You seem to have three mediators (BaseMediator, Producer, and Consumer) and only one colleague (Driver), and none of your mediators have a pointer to the colleague. This seems like a group of three highly unconventional mediators with nothing to mediate between.

Fortunately, a pattern is always a means to an end, not a goal in itself, so if what you have does what you want, there is no need to worry about whether it is a mediator.

According to your explanation I wrote a code as below:

package com.mediator;

public class ConsumerSuper {

    private String key;
    private Mediator m;

    public ConsumerSuper(String key, Mediator m){
         this.key = key;
         this.m = m;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public Mediator getM() {
        return m;
    }

    public void setM(Mediator m) {
        this.m = m;
    }
}


package com.mediator;

public class Consumer extends ConsumerSuper{

    public Consumer(String key, Mediator m){
        super(key, m);
        m.regConsumer(this);
    }

    public void display(){
        System.out.println("Consumer");
    }
}


package com.mediator;

public class ProducerSuper {

    private String key;
    private Mediator m;

    public ProducerSuper(String key, Mediator m){
         this.key = key;
         this.m = m;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public Mediator getM() {
        return m;
    }

    public void setM(Mediator m) {
        this.m = m;
    }
}


package com.mediator;

public class Producer extends ProducerSuper {

    public Producer(String key, Mediator m){
        super(key, m);
        m.regProducer(this);
    }

}



package com.mediator;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class Mediator {
     private HashMap<Producer,Consumer> map = new HashMap<Producer,Consumer>(); 
     private Producer p;
     private Consumer c;

     public void regProducer(Producer p){
         this.p = p;
     }

     public void regConsumer(Consumer c){
         this.c = c;
     }

     public void asscPC(){
         map.put(this.p, this.c);
     }

     @SuppressWarnings("rawtypes")
     public void display(){
         Set<Producer> p = map.keySet();
         Iterator iter = p.iterator();
         while(iter.hasNext()){
             Producer producer = (Producer)iter.next();
             Consumer consumer = (Consumer)map.get(producer);
             System.out.println(producer.getKey() + " " + consumer.getKey());
         }
     }
}


package com.mediator;

public class Driver {
    public static void main(String[] args){
        Mediator m = new Mediator();
        Producer p = new Producer("P1",m);
        Consumer c = new Consumer("C1",m);
        m.asscPC();
        m.display();
    }
}

Does my code qualify as a mediator design pattern? Your feedback is kindly appreciated.

Thank You.

This question has already been answered. Start a new discussion instead.