桥接模式

用于多层继承,对象对维度变化

如下,电脑分为品牌和(台式机,笔记本,pad)两个维度变化

public interface Brand {//品牌接口
	public String name();
}
class Asus implements Brand{//具体品牌
	@Override
	public String name() {
		// TODO Auto-generated method stub
		return getClass().getName();
	}	
}
class Apple implements Brand{//具体品牌
	@Override
	public String name() {
		// TODO Auto-generated method stub
		return getClass().getName();
	}
}
public class Computer {//电脑基础类
	protected Brand brand;
	
	public void sale() {
		
	}
}
class DesktopComputer extends Computer{//台式机
	public DesktopComputer(Brand brand)
	{
		this.brand = brand;
	}
	
	public void sale()
	{
		System.out.println("sale " + brand.name() + " " + getClass().getName());
	}
}
class PadComputer extends Computer{//pad
	public PadComputer(Brand brand)
	{
		this.brand = brand;
	}
	
	public void sale()
	{
		System.out.println("sale " + brand.name() + " " + getClass().getName());
	}
}
public class Client {//使用
	public static void main(String[] args)
	{
		new DesktopComputer( new Asus()).sale();
		new PadComputer( new Apple()).sale();
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章