橋接模式

用於多層繼承,對象對維度變化

如下,電腦分爲品牌和(臺式機,筆記本,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();
	}
}

 

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