java设计模式之中介者模式

来源:互联网 发布:淘宝详情页私单价格 编辑:程序博客网 时间:2024/05/16 07:09

public interface Meditor{

public void createMeditor();

public void workAll();

}


public class MyMeditor implements Meditor{

private User1 user1;

private User2 user2;


public User1 getUser1(){

return user1;

}


public User2 getUser2(){

return user2;

}


@Override

public void createMeditor(){

user1 = new User1();

user2 = new User2();

}


@Override

public void workAll(){

user1.work();

user2.work();

}

}


public abstract class User{

private Meditor meditor;


public Meditor getMeditor(Meditor meditor){

this.meditor = meditor;

}


public abstract void work();


public  User(Meditor meditor){

this.meditor = meditor;

}

}


public class User1 extend User{


public User1(Meditor meditor){

super(meditor);

}

@Override

public void work(){

System.out.println("this is the User1");

}

}


public class User2 extend User{


public User2(Meditor meditor){

super(meditor);

}

@Override

public void work(){

System.out.println("this is the User2");

}

}


public class Test{

public static void main(String args[]){

Meditor meditor = new Meditor();

meditor.createMeditor();

meditor.workAll();

}

}



0 0
原创粉丝点击