Bridge Design Pattern

Bridging application scenarios:

1.jdbc driver

2. AWT's Peer Architecture

3. Bank log management:

        Format classification: operation log, transaction log, exception log

        Distance classification: local logging, remote logging

4. Bonus calculation in HR system:

        Bonus department: individual bonus, group bonus, incentive bonus

        Department classification: personnel department, sales department, R&D department

5.Message processing of the oa system:

        Service type: ordinary message, urgent message, special message

        Sending message: system message, SMS, email



Scenario: Common product classification in the mall system, taking a computer as an example, how to deal with the problem of product classification?

For example, the multi-level integration structure is shown in the figure:


But this will encounter a scalability problem:

If you want to add a new type of computer, you must add a class under each brand, add a new brand, and also add a class for each computer type.

First take the way of not using bridge, class diagram:


computer

package com.gcxzflgl.bridge;

public interface Computer {
	void sale();
}

class Desktop implements Computer {
	@Override
	public void sale() {
		System.out.println("Selling desktops!");
	}
}

class Laptop implements Computer {
	@Override
	public void sale() {
		System.out.println("Sale notebook!");
	}
}
class Pad implements Computer {
	@Override
	public void sale() {
		System.out.println("Selling tablets!");
	}
}

class LenovoDesktop extends Desktop {
	@Override
	public void sale() {
		System.out.println("Selling Lenovo Desktops");
	}
}
class LenovoLaptop extends Laptop {
	@Override
	public void sale() {
		System.out.println("Selling Lenovo Notebooks");
	}
}
class LenovoPad extends Pad {
	@Override
	public void sale() {
		System.out.println("Selling Lenovo Tablet");
	}
}



class ShenzhouDesktop extends Desktop {
	@Override
	public void sale() {
		System.out.println("Sales Shenzhou Desktop");
	}
}
class ShenzhouLaptop extends Laptop {
	@Override
	public void sale() {
		System.out.println("Sales Shenzhou Notebook");
	}
}
class ShenzhouPad extends Pad {
	@Override
	public void sale() {
		System.out.println("Sales Shenzhou Tablet PC");
	}
}


class DellDesktop extends Desktop {
	@Override
	public void sale() {
		System.out.println("Selling Dell Desktops");
	}
}
class DellLaptop extends Laptop {
	@Override
	public void sale() {
		System.out.println("Selling Dell Notebooks");
	}
}
class DellPad extends Pad {
	@Override
	public void sale() {
		System.out.println("Selling Dell Tablets");
	}
}

Client call:

package com.gcxzflgl.bridge;

public class Client {
	public static void main(String[] args) {
		//Sell Lenovo's laptops
		Computer  c = new Laptop();
		Laptop l = new LenovoLaptop();
		c.sale();
		l.sale();	
	}
}

The above method is difficult to maintain in actual development


The following bridge mode is reasonably designed as such



Class Diagram:


Create a brand class:

package com.gcxzflgl.bridge;

/**
 * brand
 * @author Administrator
 *
 */
public interface Brand {
	void sale();
}

class Lenovo implements Brand {

	@Override
	public void sale() {
		System.out.println("Selling Lenovo Computers");
	}
	
}

class Dell implements Brand {
	
	@Override
	public void sale() {
		System.out.println("Selling Dell computers");
	}
	
}

class Shenzhou implements Brand {
	
	@Override
	public void sale() {
		System.out.println("Sales Shenzhou Computer");
	}
	
}

Create the computer class:

package com.gcxzflgl.bridge;

/**
 * Dimensions of computer type
 * @author Administrator
 *
 */
public class Computer2 {
	
	protected Brand brand;
	
	public Computer2(Brand b) {
		this.brand = b;
	}
	
	public void sale(){
		brand.sale();
	}
	
}

class Desktop2 extends Computer2 {

	public Desktop2(Brand b) {
		super(b);
	}
	
	@Override
	public void sale() {
		super.sale();
		System.out.println("Selling Desktop");
	}
}

class Laptop2 extends Computer2 {
	
	public Laptop2(Brand b) {
		super(b);
	}
	
	@Override
	public void sale() {
		super.sale();
		System.out.println("Sales Notebook");
	}
}

Client call:

package com.gcxzflgl.bridge;

public class Client {
	public static void main(String[] args) {
		//Sell Lenovo's laptops
		Computer  c = new Laptop();
		Laptop l = new LenovoLaptop();
		c.sale();
		l.sale();
		
		//Sell Shenzhou's desktop
		Computer2 c2 = new Desktop2(new Shenzhou());
		c2.sale();
		
		
	}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324695535&siteId=291194637