Java 设计模式 之 桥接模式

http://www.verejava.com/?id=16999016430959

package com.brige.theory;

public class TestBrige
{
	public static void main(String[] args)
	{
		Person man=new Man("男人");
		Person lady=new Lady("女人");
		
		Clothing jacket=new Jacket("夹克");
		Clothing trouser=new Trouser("裤子");
		
		man.setCloth(jacket);
		man.dress();
		
		man.setCloth(trouser);
		man.dress();
		
		lady.setCloth(jacket);
		lady.dress();
		
		lady.setCloth(trouser);
		lady.dress();
		
	}
}





package com.brige.theory;

public abstract class Person
{
	protected String name;
	protected Clothing cloth;
	public Person(String name)
	{
		super();
		this.name = name;
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public Clothing getCloth()
	{
		return cloth;
	}
	public void setCloth(Clothing cloth)
	{
		this.cloth = cloth;
	}
	
	public abstract void dress();
}





package com.brige.theory;

public class Man extends Person
{

	public Man(String name)
	{
		super(name);
	}

	@Override
	public void dress()
	{
		System.out.println(name+" 穿 "+cloth.getName());
		
	}

}





package com.brige.theory;

public class Lady extends Person
{

	public Lady(String name)
	{
		super(name);
	}

	@Override
	public void dress()
	{
		System.out.println(name+" 穿 "+cloth.getName());
		
	}

}





package com.brige.theory;

public abstract class Clothing
{
	protected String name;
	protected Person person;
	
	
	public Clothing(String name)
	{
		super();
		this.name = name;
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public Person getPerson()
	{
		return person;
	}
	public void setPerson(Person person)
	{
		this.person = person;
	}
	
	
}





package com.brige.theory;

public class Jacket extends Clothing
{

	public Jacket(String name)
	{
		super(name);
	}

}





package com.brige.theory;

public class Trouser extends Clothing
{

	public Trouser(String name)
	{
		super(name);
	}

}


http://www.verejava.com/?id=16999016430959

posted @ 2018-06-28 09:11  verejava  阅读(129)  评论(0编辑  收藏  举报