来源:http://www.bjsxt.com/
一、S03E238_01【GOF23设计模式】_桥接模式、多层继承结构、银行日志管理、管理系统消息管理、人力资源的奖金计算
未用桥接模式:
package com.test.bridge;
public interface Computer {
void sale();
}
class Desktop implements Computer{
@Override
public void sale() {
System.out.println("销售台式机");
}
}
class Laptop implements Computer{
@Override
public void sale() {
System.out.println("销售笔记本");
}
}
class Pad implements Computer{
@Override
public void sale() {
System.out.println("销售平板电脑");
}
}
class LenovoDesktop extends Desktop{
@Override
public void sale(){
System.out.println("销售联想台式机");
}
}
class LenovoLaptop extends Laptop{
@Override
public void sale(){
System.out.println("销售联想笔记本");
}
}
class LenovoPad extends Pad{
@Override
public void sale(){
System.out.println("销售联想平板电脑");
}
}
class ShenzhouDesktop extends Desktop{
@Override
public void sale(){
System.out.println("销售神舟台式机");
}
}
class ShenzhouLaptop extends Laptop{
@Override
public void sale(){
System.out.println("销售神舟笔记本");
}
}
class ShenzhouPad extends Pad{
@Override
public void sale(){
System.out.println("销售神舟平板电脑");
}
}
class DellDesktop extends Desktop{
@Override
public void sale(){
System.out.println("销售戴尔台式机");
}
}
class DellLaptop extends Laptop{
@Override
public void sale(){
System.out.println("销售戴尔笔记本");
}
}
class DellPad extends Pad{
@Override
public void sale(){
System.out.println("销售戴尔平板电脑");
}
}
使用桥接模式:
package com.test.bridge;
/**
* 电脑类型的维度
*/
public class Computer2 {
protected Brand brand;
public Computer2(Brand brand) {
super();
this.brand = brand;
}
public void sale(){
brand.sale();
}
}
class Desktop2 extends Computer2{
public Desktop2(Brand brand) {
super(brand);
}
@Override
public void sale() {
super.sale();
System.out.println("销售台式机");
}
}
class Laptop2 extends Computer2{
public Laptop2(Brand brand) {
super(brand);
}
@Override
public void sale() {
super.sale();
System.out.println("销售笔记本");
}
}
package com.test.bridge;
/**
* 品牌
*/
public interface Brand {
void sale();
}
class Lenovo implements Brand{
@Override
public void sale() {
System.out.println("销售联想电脑");
}
}
class Dell implements Brand{
@Override
public void sale() {
System.out.println("销售戴尔电脑");
}
}
//新增之前没有的品牌,不用同时新增电脑类型(不用在Computer2中写代码)
class Shenzhou implements Brand{
@Override
public void sale() {
System.out.println("销售神舟电脑");
}
}
package com.test.bridge;
public class Client {
public static void main(String[] args) {
//销售联想的笔记本电脑
Computer2 c = new Laptop2(new Lenovo());
c.sale();
//销售神舟的笔记本电脑
Computer2 c2 = new Desktop2(new Shenzhou());
c2.sale();
}
}