http://www.verejava.com/?id=16999137231072
package com.mediator.theory;
public class TestMediator
{
public static void main(String[] args)
{
Mediator mediator=new MediatorImpl();
mediator.handle("rent");
mediator.handle("sale");
}
}
package com.mediator.theory;
public interface Mediator
{
public void handle(String content);
}
package com.mediator.theory;
public class MediatorImpl implements Mediator
{
private HouseOwner owner1;
private HouseOwner owner2;
public MediatorImpl()
{
owner1=new HouseRent();
owner2=new HouseSale();
}
@Override
public void handle(String content)
{
if("rent".equals(content))
{
owner1.action();
}
if("sale".equals(content))
{
owner2.action();
}
}
}
package com.mediator.theory;
public interface HouseOwner
{
public void action();
}
package com.mediator.theory;
public class HouseRent implements HouseOwner
{
@Override
public void action()
{
System.out.println("客户来了需要租房");
}
}
package com.mediator.theory;
public class HouseSale implements HouseOwner
{
@Override
public void action()
{
System.out.println("客户来了需要卖房");
}
}
http://www.verejava.com/?id=16999137231072
原文地址:https://www.cnblogs.com/verejava/p/9237049.html
时间: 2024-10-10 04:46:58