Bridge Pattern

The main idea of Bridge pattern is separating the objects’ implementation from its abstraction, this will help when you want to change an object’s implementation code without affecting on its abstraction.

For better understanding, consider the following scenario:-
[1] Vehicle is an abstraction
[2] Cars, buses, motorcycle, …etc are objects that inherit from Vehicle and each one of them has its own implementation
[3] Each vehicle should contain an engine which has different types (Kia, Nissan .. etc), so you can consider engine is an abstraction that all vehicles should have it.

The following code represents an interface that contains some of the vehicles functions

public interface VehicleObject {
        public void move();
        public void accelerate();
        public void stop();
    }

We will create car and bus classes that inherit and implement the above functions

public static class Car implements VehicleObject {
        public void move() {
            System.out.println("Car is moving!");
        }
        
        public void accelerate() {
            System.out.println("Car is accelerating!");
        }
        
        public void stop() {
            System.out.println("Car stopped!");
        }
    }

 

public static class Bus implements VehicleObject {
        public void move() {
            System.out.println("Bus is moving!");
        }
        
        public void accelerate() {
            System.out.println("Bus is accelerating!");
        }
        
        public void stop() {
            System.out.println("Bus stopped!");
        }
    }

This is the implementation phase, each vehicle now has its own implementation.

Each one of these objects should contain an engine, so we will create an Engine interface that holds some of its functions or states.

public interface EngineObject {
        public void running();
        public void stop();
    }
public static class Engine implements engineObject {
        private vehicleObject object;
        
        public void setObject(vehicleObject object) {
            this.object = object;
        }
        
        public void running() {
            this.objectFunctions.move();
        }
        
        public void stopped() {
            this.objectFunctions.stop();
        }
    }

Engine class contains “setObject” to map the engine with its car, engine state depends on the machine (object) state, when the car is moving the engine is running too and vice versa.

We can summarize Bridge pattern implementation in the following points:-
[1] Abstract class
[2] Normal class that represents objects which implement the abstraction
[3] Find an abstraction in the object itself
[4] If it is found, create interface that represent the new abstraction
[5] Implement the new interface

You can see the full implementation Here
 
References:
http://en.wikipedia.org/wiki/Bridge_pattern

Advertisements

2 thoughts on “Bridge Pattern

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s