Thirteen: Mediator Mode (Alliance and Faction)

Mediator pattern: A mediator object is used to encapsulate a series of object interactions. The mediator makes the objects do not need to refer to each other explicitly, so that the coupling is loose, and the interaction between them can be changed independently. The mediator mode, also known as the mediator mode, is an object behavioral mode.

               "According to what the Six Paths of Heaven said back then, the series of objects mentioned in this manual should refer to various sects. Now each sect interacts with each other independently, and the relationship is complicated, and it is difficult to make the mainland quiet." Zuo Ge As the Lord spoke, a map of the current continental pattern appeared in the void.

                "Although there are abstract parent sects, the relationship between the specific sects seems to be a bit confusing. If you want to interpret the secret code, the relationship between my God Pavilion, the command sect and the single instance sect should be like this. Yes." Pavilion Master Zuo said, his hands began to draw strange symbols in the void.

                "First of all, it is an abstract sect parent class, which represents the commonality of our various sects. This seems to have a taste of the template method "Template Method Pattern", like a template class."

copy code
package com.mediator;
//门派抽象类
public abstract class School {

    public void defense() {
        System.out.println(getName() + "防御");
    }

    public void attack(School school) {
        System.out.println(getName() + "攻击" + school.getName());
    }
    
    public abstract String getName();
    
}
copy code

               "The following is the specific sect of my God Pavilion, the Order Gate, and the Single Case Sect." Zuo Pavilion Master said, one by one strange symbols constantly poured out of his hands, floating in the void in front of him.

copy code
package com.mediator;

public class MediatorSchool extends School{

    public String getName() {
        return "中介者神阁";
    }

}
copy code
copy code
package com.mediator;

public class CommandSchool extends School{

    public String getName() {
        return "命令门";
    }

}
copy code
copy code
package com.mediator;

public class SingletonSchool extends School{

    public String getName() {
        return "单例宗";
    }

}
copy code

                "With these words, the current pattern of the mainland can be easily described. It should be like this now."

copy code
package com.mediator;
//大陆格局
public class ContinentalPattern {

    public static void main(String[] args) {
        School singletonSchool = new SingletonSchool();
        School commandSchool = new CommandSchool();
        School mediatorSchool = new MediatorSchool();
        
        singletonSchool.attack(mediatorSchool);
        commandSchool.attack(mediatorSchool);
        
        mediatorSchool.defense();
    }
    
}
copy code


              "This is really not good. Each sect can attack at will, it is chaotic, and there is no control force, and it is very cumbersome to communicate with each sect. Each sect has to deal with the relationship with the other 22 sects. No wonder that the model continent is gradually gradually changing. It's starting to be chaotic. Besides, my **** pavilion has been attacked by the single case and the command gate for a while, and finally reacted slowly and adopted a defensive strategy in a hurry. The pavilion master shook his head secretly.
               After a pause, Pavilion Master Zuo continued to talk to himself: "According to the secrets of my God Pavilion, there should be an intermediary object in the mainland, which can be responsible for the communication between various sects and control the actions of each sect. This way. If so, it can indeed solve the turmoil in the mainland, remove the complex relationship between the various sects, and let all the sects obey this intermediary. It seems that the six gods are as expected."

               After thinking for a moment with his eyes closed, Pavilion Master Zuo suddenly opened his eyes, and then said the name of the mediator, "Model Alliance" word by word.

               Afterwards, the hands of the left pavilion master phantom painted in the void, and the secret words of secret books exuded dazzling rays of light in the void. These rays of light formed the interface of the mode alliance.

copy code
package com.mediator; 
//Pattern alliance, intermediary interface 
public interface IPatternAlliance { 

    //Join alliance 
    public abstract void add(School school); 

    //Alliance attack 
    public abstract void resolveAttack(School activeSide, School passiveSide); 

    //Alliance defense 
    public abstract void resolveDefense(School passiveSide); 

}
copy code

                "This is the model alliance interface. It is the alliance purpose of the model alliance and guides what the alliance has to do." Zuo Gezhu nodded.

                "The following is the true face of the model alliance."

copy code
package com.mediator;

import java.util.ArrayList;
import java.util.List;
//模式联盟,中介者
public class PatternAlliance implements IPatternAlliance {

    private List<School> schoolList;

    public PatternAlliance() {
        super();
        schoolList = new ArrayList<School>();
    }
    
    public void add(School school){
        schoolList.add(school);
    }
    
    public void resolveAttack(School activeSide,School passiveSide){
        if (schoolList.contains(activeSide) && schoolList.contains(passiveSide)) { 
            System.out.println("The main attacker" + activeSide.getName() + "And the attacked side" + passiveSide.getName() + "All have joined the alliance, no infighting!");
        }else if (schoolList.contains(activeSide) && !schoolList.contains(passiveSide)) {
            System.out.println("The main attacker" + activeSide.getName() + "Joined the alliance, the attacked side" + passiveSide.getName() + "Not in the alliance, will attack the sect collectively!"); 
            for ( School school : schoolList) { 
                school.attack(passiveSide); 
            } 
        }else { 
            System.out.println("Attack" + activeSide.getName() + "Not joining the alliance, the alliance has no right to intervene!"); 
            activeSide .attack(passiveSide); 
        } 
    } 

    public void resolveDefense(School passiveSide){ 
        if (schoolList.contains(passiveSide)) { 
            for (School school : schoolList) { 
                school.defense(); 
            } 
        }else { 
            System.out.println("attacked party" + passiveSide.getName() + "Not joining the alliance, the alliance will not give defense help!"); 
            passiveSide.defense(); 
        } 
    } 
    
}
copy code

               "This should be the general blueprint of the alliance. It can be responsible for the communication between the various sects, remove the direct connection between the sects, and in doing so, it can also control the sects in the alliance. The rest is for each sect. The layout needs to be changed a little." Zuo Pavilion looked at the alliance he planned and said softly.

                "First of all, the abstract parent class needs to be changed, because now the actions of each sect need to be delegated to the alliance."

copy code
package com.mediator; 
//Sect abstract class 
public abstract class School { 

    protected IPatternAlliance patternAlliance; 

    public School(IPatternAlliance patternAlliance) { 
        super(); 
        this.patternAlliance = patternAlliance; 
    } 
    
    public void defense() { 
        System.out.println(getName () + "defense"); 
    } 

    public void attack(School school) { 
        System.out.println(getName() + "attack" + school.getName()); 
    } 
    
    //With a mediator, it will be handled by the mediator 
    public void attackByPatternAlliance(School school){ 
        patternAlliance.resolveAttack(this, school); 
    }
     
    public void defenseByPatternAlliance(){
        patternAlliance.resolveDefense(this);
    }
    
    public abstract String getName();
    
}
copy code

                "This time, the layout of each sect just needs to be changed a little, because every sect must know the intermediary, that is, the model alliance. Then the strategy palace, the target of public criticism, also needs to know, but the alliance should accept this or not. Guys, that's another story, hehe!" Zuo Pavilion said with a yin smile.

copy code
package com.mediator;

public class CommandSchool extends School{

    public CommandSchool(IPatternAlliance patternAlliance) {
        super(patternAlliance);
    }

    public String getName() {
        return "命令门";
    }

}
copy code
copy code
package com.mediator;

public class MediatorSchool extends School{

    public MediatorSchool(IPatternAlliance patternAlliance) {
        super(patternAlliance);
    }

    public String getName() {
        return "中介者神阁";
    }

}
copy code
copy code
package com.mediator;

public class SingletonSchool extends School{

    public SingletonSchool(IPatternAlliance patternAlliance) {
        super(patternAlliance);
    }

    public String getName() {
        return "单例宗";
    }

}
copy code
copy code
package com.mediator;

public class StrategySchool extends School{

    public StrategySchool(IPatternAlliance patternAlliance) {
        super(patternAlliance);
    }

    public String getName() {
        return "策略宫";
    }
    
}
copy code

                "The pattern of the mainland is about to change. This pavilion master will simulate one first. But the outcome of the strategy palace doesn't seem to be very good. Hehe." With a sneer, the left pavilion master began to draw in the void. Secret code.

copy code
package com.mediator;
//大陆格局
public class ContinentalPattern {

    public static void main(String[] args) {
        IPatternAlliance patternAlliance = new PatternAlliance();
        
        School singletonSchool = new SingletonSchool(patternAlliance);
        School commandSchool = new CommandSchool(patternAlliance);
        School mediatorSchool = new MediatorSchool(patternAlliance);
        
        School strategySchool = new StrategySchool(patternAlliance);
        
        //策略宫没有被联盟收留
        patternAlliance.add(mediatorSchool);
        patternAlliance.add(commandSchool);
        patternAlliance.add(singletonSchool);
        
        singletonSchool.attackByPatternAlliance(mediatorSchool);
        commandSchool.attackByPatternAlliance(mediatorSchool);
        System.out.println("------------------------------------------------------");
        mediatorSchool.attackByPatternAlliance(strategySchool);
        System.out.println("------------------------------------------------------");
        strategySchool.attackByPatternAlliance(mediatorSchool);
        mediatorSchool.defenseByPatternAlliance();
        System.out.println("------------------------------------------------------");
    }
    
}
copy code


The problem that "Mediator Mode" aims at is to solve the complex coupling relationship between a series of objects. This series of objects is often a "many-to-many" coupling relationship. Objects are centrally managed, and each object also delegates the interaction between itself and other objects to the mediator, thereby reducing the coupling between this series of objects.

Its advantages are very obvious, it clears the complex coupling relationship between a series of objects, and the mediator can control the behavior of this series of objects and manage them uniformly.

The disadvantage of the "Mediator Pattern" is that since the mediator is responsible for the interaction and control of a series of objects, the mediator's class will be very complex, and once the mediator class does not work properly, all classes that delegate behavior to the mediator There will be problems, so be careful when using it.

"Next, the pavilion master will take a look at the current layout map of the mainland." After speaking, the left pavilion master began to draw the layout map in the void.


         

Reprinted in: https://www.cnblogs.com/2019lgg/p/11091078.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324160834&siteId=291194637