Java 设计模式 之 调停者模式

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

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页