Loading, please wait ...
Open its course - Design Patterns
Mediator By Mukhtiar Zamin
Summary

Summary

2
Teachers with
Mukhtiar Zamin
178
Followers
For Learning
All
Visibility
Security Status
2
Contributions
By Teachers
Notes
  1. Students are auto-evaluated against related topics to ensure they learned it.

This lecture covers Mediator design pattern.

Watch the interaction videos and examples.

Mediator Example for All, By Mukhtiar Zamin

Mediator Simple Implementation

Steps:

  1. Create a Java application with a package name "Mediator"
  2. Create the following files with respective code in each file.

    Mediator.java

    package Mediator;
    public interface Mediator {
        public void operationD();
        public void operationA();
    }
    

    BaseComponent.java

    package Mediator;
    public class BaseComponent {
        protected Mediator mediator;    
        public void setMediator(Mediator objMediator){
            this.mediator = objMediator;
        }
    }
    

    ConcreteColleaque1.java

    package Mediator;
    public class ConcreteColleaque1 extends BaseComponent{
        public void doOperationA(){
            System.out.println("Colleaque 1 performed operation A.");
        }
        public void doOperationB(){
            System.out.println("Colleaque 1 performed operation B.");
        }
    }
    

    ConcreteColleaque2.java

    package Mediator;
    public class ConcreteColleaque2 extends BaseComponent{
        public void doOperationC(){
            System.out.println("Colleaque 2 performed operation C.");
        }
        public void doOperationD(){
            System.out.println("Colleaque 2 performed operation D.");
        }
    }
    

    ConcreteMediator.java

    package Mediator;
    public class ConcreteMediator  implements Mediator{
        private ConcreteColleaque1 objColleaque1;
        private ConcreteColleaque2 objColleaque2;
                   
        // Mediator aggregates its concrete colleaques
        public ConcreteMediator(ConcreteColleaque1 component1, ConcreteColleaque2 component2) {
            this.objColleaque1 = component1;
            component1.setMediator(this);
            this.objColleaque2 = component2;
            component2.setMediator(this);
        }		
    	
        @Override
        public void operationA() {
            System.out.println("Mediator knows that operationA can be done by Colleaque1.");        
            objColleaque1.doOperationA();
        }
        @Override
        public void operationD() {
            System.out.println("Mediator knows operationD can be done by Colleaque2.");        
            objColleaque2.doOperationD();
        }
    }
    

    Client.java

    package Mediator;
    public class Client {
       public static void main(String[] args) {
            // 1. Initiate concrete colleques
            ConcreteColleaque1 objColleaque1 = new ConcreteColleaque1();
            ConcreteColleaque2 objColleaque2 = new ConcreteColleaque2();
            // 2. Initiate mediator with controlling components
            Mediator objM =  new ConcreteMediator(objColleaque1, objColleaque2);
            // 3. Ask Mediator to perform operations
            objM.operationA();        
            objM.operationD();
       } 
    }
    

Lab Tasks for All, By Mukhtiar Zamin

  1. Add another ConcreteColleaque3 with Operation doOperationAll that internally ask mediator to perform OperationA and OperationD. Also mediator should be setup via its constructor.
    Answer
    a. Add the following ConcreteColleaque3.java file to the package "Mediator"
    package Mediator;
    public class ConcreteColleaque3 extends BaseComponent {
        public ConcreteColleaque3(Mediator objMediator){
            this.mediator = objMediator;
        }    
        public void doOperationAll(){
            System.out.println("Colleaque 3 asking mediator to perform operationA and operationD.");
            mediator.operationA();
            mediator.operationD();
            System.out.println("Colleaque 3 performed all operations.");
        }
    }
    
    b. Append the following code in the main of Client.java
    // 4. Mediator serving colleques internal operations
            ConcreteColleaque3 objColleaque3 = new ConcreteColleaque3(objM);
            objColleaque3.doOperationAll();
    
  2. Modify the concrete mediator such that concrete colleques will subscribe and unsubscribe to it than passing it colleques in constructor. 

×

Mediator Evaluation

To earn PASS status, system is about to evaluate you using multiple choice questions (MCQs) and True/False type questions.

  • CAREFULLY READ THE FOLLOWING INSTRUCTIONS!
  • Make sure you have learned this lecture along with its topics and tutorials.
  • On first evaluation you get 100% marks on correct answer, then on 2nd you get 95% marks and so on.
  • Answer the questions with a gap NOT MORE THAN A MINUTE, Otherwise it will re-start.

I have read above instrucitons and ready for evaluation.
Your's
Status
Not Registered
Teacher
Mukhtiar Zamin
Alert
Your performance monitoring will start when you register in a class of this course.

Questions on

Contact Us

support@subexpert.com
Write to Us View Help
Subject Expert Logo

Subject Expert

Learn, Evaluate and Optimize

Follow Us
Facebook Switch Display Mode Enable Translation
© 2024 - Subject Expert