[Design mode-behavior type] Mediator Mode)
One sentence
It is also translated into the mediation mode.
It is similar to a real estate agent. Buyers and sellers do not need to negotiate with each other directly.
Summary
Analysis
MEDIATOR-four MM players playing mahjong. It is unclear who should pay for each other. Fortunately, I was there and counted the money based on their chips, if you make money, you can get it from me. If you lose the money, you can pay it to me. Everything is OK. You get a phone number of four MM.
Called by mode: The called by mode encapsulates a series of ways in which objects interact, so that these objects do not have to interact explicitly. So that they can be loosely coupled. When the roles of some objects change, the roles of other objects are not immediately affected. Ensure that these functions can be changed independently of each other. The mediator mode converts the multi-to-many interaction into one-to-many interaction. The mediation mode abstracts the behavior and collaboration of objects, and processes the interaction between objects and other objects on a small scale.
Instance
As an example of a real estate transaction, there is no need to pay property tax for houses that have been in the second-hand housing market for more than five years.
Including class files:
Mediator. java APIs)
An intermediary implementation class: (MediatorImpl. java)
A homeowner's interface (HouseOwner. java)
Classes of the two homekeepers (HouseOwnerA. java, HouseOwnerB. java)
Test main class: (TestMain. java)
/** * @author oscar999 * @date 2015-1-6 * @version V1.0 */package designptn.mediator;public abstract class Mediator {public abstract void sellHouse(String houseType);}
/** * @author oscar999 * @date 2015-1-6 * @version V1.0 */package designptn.mediator;/** * @author Administrator * */public class MediatorImpl extends Mediator {private HouseOwnerA ha;private HouseOwnerB hb;public MediatorImpl(){ha = new HouseOwnerA();hb = new HouseOwnerB();}@Overridepublic void sellHouse(String houseType) {// TODO Auto-generated method stubif(houseType.equals(typeA)){ha.sell();}else if(houseType.equals(typeB)){hb.sell();}}}
/** * @author oscar999 * @date 2015-1-6 * @version V1.0 */package designptn.mediator;/** * @author Administrator * */public abstract class HouseOwner {public abstract void sell() ;}
/** * @author oscar999 * @date 2015-1-6 * @version V1.0 */package designptn.mediator;/** * @author Administrator * */public class HouseOwnerA extends HouseOwner {@Overridepublic void sell() {// TODO Auto-generated method stubSystem.out.println(Sell house with Tax);}}
/** * @author oscar999 * @date 2015-1-6 * @version V1.0 */package designptn.mediator;/** * @author Administrator * */public class HouseOwnerB extends HouseOwner {@Overridepublic void sell() {// TODO Auto-generated method stubSystem.out.println(Sell house without Tax);}}
/** * @author oscar999 * @date 2015-1-6 * @version V1.0 */package designptn.mediator;/** * @author Administrator * */public class TestMain {public static void main(String[] args) {// TODO Auto-generated method stubMediator mediator = new MediatorImpl();mediator.sellHouse(typeA);mediator.sellHouse(typeB);}}