Sobes.tech
Junior — Middle

Tell about your skills and experience applying the bridge pattern in practice.

sobes.tech AI

Answer from AI

The "Bridge" pattern is used to separate abstraction from its implementation so that they can vary independently. In Java development practice, I applied this pattern when I needed to support multiple implementation options for a single interface, such as different ways of saving data (to a database, to a file, to the cloud), while maintaining a unified interface for the client.

Example:

// Abstraction
interface Remote {
    void turnOn();
    void turnOff();
}

// Implementation
interface Device {
    void powerOn();
    void powerOff();
}

class TV implements Device {
    public void powerOn() { System.out.println("TV is on"); }
    public void powerOff() { System.out.println("TV is off"); }
}

class RemoteControl implements Remote {
    private Device device;
    public RemoteControl(Device device) { this.device = device; }
    public void turnOn() { device.powerOn(); }
    public void turnOff() { device.powerOff(); }
}

// Usage
Device tv = new TV();
Remote remote = new RemoteControl(tv);
remote.turnOn();

This approach makes it easy to add new devices or new types of remotes without changing existing code.