Home
Cloud
Big Data
CI
Install
Samples
Java
Ubuntu
Maven
Archive
Design Patterns
|
Bridge
References
Example
The
Implementor
The
Concrete Implementor
(1)
The
Concrete Implementor
(2)
The
Abstraction
The
Refined Abstraction
(1)
The
Refined Abstraction
(2)
A simple class to test the
Bridge
design pattern
References
Definition: (
source:
http://en.wikipedia.org/wiki/Bridge_pattern
)
The intent of the bridge pattern is to "decouple an abstraction from its implementation so that the two can vary independently."
Class diagram: (
source:
http://en.wikipedia.org/wiki/Bridge_pattern
)
Example
The
Implementor
:
public interface Logger { void log(); }
The
Concrete Implementor
(1):
public class FileLogger implements Logger { public void log() { System.out.println("FileLogger-log"); } }
The
Concrete Implementor
(2):
public class ConsoleLogger implements Logger { public void log() { System.out.println("ConsoleLogger-log"); } }
The
Abstraction
:
public interface AbstractionLogger { void log(); }
The
Refined Abstraction
(1):
public class AbstractionFileLogger implements AbstractionLogger { private final Logger logger = new FileLogger(); public void log() { logger.log(); } }
The
Refined Abstraction
(2):
public class AbstractionConsoleLogger implements AbstractionLogger { private final Logger logger = new ConsoleLogger(); public void log() { logger.log(); } }
A simple class to test the
Bridge
design pattern:
public class BridgePatternTest { public static void main(String[] args) { // Example 1: File Logger final AbstractionLogger fileLogger = new AbstractionFileLogger(); fileLogger.log(); // Example 2: Console Logger final AbstractionLogger consoleLogger = new AbstractionConsoleLogger(); consoleLogger.log(); } }
© 2010-2022
mti
tek