桥模式主要将抽象与实现部分分离,使他们可以单独变化。
public interface Product {
public String getProductName();
}
public class ConcreteProduct1 implements Product {
public String getProductName() {
return "ConcreteProduct1" ;
}
}
public class ConcreteProduct2 implements Product {
public String getProductName() {
return "ConcreteProduct2" ;
}
}
public abstract class BrandProduct {
Product product;
BrandProduct(Product product) {
this.product = product;
}
public abstract String getBrandProductName ();
}
public class BrandProduct1 extends BrandProduct {
BrandProduct1(Product product) {
super(product);
}
@Override
public String getBrandProductName() {
return "BrandProduct1 : " + super.product.getProductName();
}
}
public class BrandProduct2 extends BrandProduct {
BrandProduct2(Product product) {
super(product);
}
@Override
public String getBrandProductName() {
return "BrandProduct2 : " + super.product.getProductName();
}
}
public class BrightClient {
public static void main(String[] args) {
Product product1 = new ConcreteProduct1();
Product product2 = new ConcreteProduct2();
BrandProduct brandProduct11 = new BrandProduct1(product1);
BrandProduct brandProduct12 = new BrandProduct1(product2);
BrandProduct brandProduct22 = new BrandProduct2(product2);
System. out.println(brandProduct11.getBrandProductName());
System. out.println(brandProduct12.getBrandProductName());
System. out.println(brandProduct22.getBrandProductName());
}
}