NOTE

Bridge Pattern

Bridge-pattern notes for class hierarchies with multiple varying dimensions, with UML and a Java brand/device example.

Software Architecture & Engineering1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. Definition

Suitable for class hierarchies that vary along multiple dimensions.

2. UML

PlantUML 图表

3. Java

3.1. Client

public class Client
{
    public static void main(String[] args)
    {
        // Create a Lenovo laptop
        Computer computer = new Laptop(new Lenovo());
        computer.sale();
    }
}

3.2. Dimension 1

public interface Brand
{
    void sale();
}
public class Dell implements Brand
{

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

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

3.3. Dimension 2

public class Computer
{
    protected Brand brand;

    public Computer(Brand brand)
    {
        this.brand = brand;
    }

    public void sale()
    {
        this.brand.sale();
    }
}
class Desktop extends Computer
{
    public Desktop(Brand brand)
    {
        super(brand);
    }

    @Override
    public void sale()
    {
        super.sale();
        System.out.println("desktop");
    }
}
public class Laptop extends Computer
{

    public Laptop(Brand brand)
    {
        super(brand);
    }

    @Override
    public void sale()
    {
        super.sale();
        System.out.println("laptop");
    }
}

4. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub