设计模式8——桥接模式
代码实现:
package com.ghl.bridge;
/**
* @ProjectName DesignPattern
* @ClassName brand
* @Date 2020/8/27 22:10
* @Author gaohengli
* @Version 1.0
*/
//桥接模式
//品牌
public interface Brand {
void info();
}
package com.ghl.bridge;
/**
* @ProjectName DesignPattern
* @ClassName Lenovo
* @Date 2020/8/27 22:11
* @Author gaohengli
* @Version 1.0
*/
//苹果品牌
public class Apple implements Brand {
@Override
public void info() {
System.out.println("苹果");
}
}
package com.ghl.bridge;
/**
* @ProjectName DesignPattern
* @ClassName Lenovo
* @Date 2020/8/27 22:11
* @Author gaohengli
* @Version 1.0
*/
//联想品牌
public class Lenovo implements Brand {
@Override
public void info() {
System.out.println("联想");
}
}
package com.ghl.bridge;
/**
* @ProjectName DesignPattern
* @ClassName Computer
* @Date 2020/8/27 22:16
* @Author gaohengli
* @Version 1.0
*/
//抽象的电脑类型类
public abstract class Computer {
//组合:品牌
protected Brand brand;
public Computer(Brand brand) {
this.brand = brand;
}
public void info(){
brand.info();//自带品牌
}
}
//台式机
class Desktop extends Computer{
public Desktop(Brand brand) {
super(brand);
}
@Override
public void info() {
super.info();
System.out.println("台式机");
}
}
//笔记本
class Laptop extends Computer{
public Laptop(Brand brand) {
super(brand);
}
@Override
public void info() {
super.info();
System.out.println("笔记本");
}
}
package com.ghl.bridge;
/**
* @ProjectName DesignPattern
* @ClassName Test
* @Date 2020/8/27 22:29
* @Author gaohengli
* @Version 1.0
*/
//测试
public class Test {
public static void main(String[] args) {
//苹果笔记本
Computer laptop = new Laptop(new Apple());
laptop.info();
//联想台式机
Computer desktop = new Desktop(new Lenovo());
desktop.info();
}
}