Skip to content

Bridge design pattern #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added 4_4_Bridge_pattern_sequence.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Bridge Design Pattern class diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
# DesignPatternsJava9
This repo consists Gang of Four Design patterns code on Java 9. Each branch in the repository has code of 1 design pattern. Switch repository to try out different design patterns.
# What is Bridge Design Pattern
* The bridge pattern can also be thought of as two layers of abstraction.
* Bridge pattern Decouples an abstraction so two classes can vary independently.

## Diagram
![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/bridge-pattern/diagrams/Bridge%20Design%20Pattern%20class%20diagram.png "Diagram")

![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/bridge-pattern/diagrams/4_4_Bridge_pattern_sequence.png "Diagram")

### When to use Bridge Design Pattern
When we need to make functionality of concrete classes independent from interface implementer classes. Both types of classes can be altered structurally without affecting each other.

### Learn Design Patterns with Java by Aseem Jain
This repository contains working project code used in video Course by Packt Publication with title "Learn Design Patterns with Java " authored by "Aseem Jain".

### Course link:
https://www.packtpub.com/application-development/learn-design-patterns-java-9-video

### ![ http://in.linkedin.com/in/premaseem](https://github.com/premaseem/DesignPatternsJava9/blob/master/linkedin.png "http://in.linkedin.com/in/premaseem") Profile: http://in.linkedin.com/in/premaseem

### Authors blog on design patterns:
https://premaseem.wordpress.com/category/computers/design-patterns/

### Software Design pattern community face book page:
https://www.facebook.com/DesignPatternGuru/

### Note:
* This code base will work on Java 9 and above versions.
* `diagrams` folders carry UML diagrams.
* `pattern` folder has code of primary example.
* `patternBonus` folder has code of secondary or bonus example.
Binary file added diagrams/4_4_Bridge_pattern_sequence.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added diagrams/Bridge Design Pattern class diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 66 additions & 0 deletions pattern/src/com/premaseem/BridgeRemoteControl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.premaseem;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public abstract class BridgeRemoteControl {

// Introduced has a relationship
private ITV tv;

public BridgeRemoteControl (ITV tv) {
this.tv = tv;
}

public void turnOn () {
tv.on();
}

public void turnOff () {
tv.off();
}

public void setChannel (int channel) {
tv.switchProgram(channel);
}

// additional features
public void recordProgram(){
System.out.println("IndependentRemoteControl use keyword to set channel.");
}
}


class IndependentRemoteControl extends BridgeRemoteControl {
public IndependentRemoteControl (ITV tv) {
super(tv);
}

public void setChannelKeyboard (int channel) {
setChannel(channel);
System.out.println("IndependentRemoteControl use keyword to set channel.");
}


}

// coupled implementation of remote controller
class DependentRemoteControl implements ITV {
@Override
public void on () {
System.out.println(" Forced to change, if TV interface changes ");
}

@Override
public void off () {
System.out.println(" Forced to change, if TV interface changes ");
}

@Override
public void switchProgram (int channel) {
System.out.println(" Forced to change, if TV interface changes ");
}

}
22 changes: 20 additions & 2 deletions pattern/src/com/premaseem/Client.java
Original file line number Diff line number Diff line change
@@ -4,10 +4,28 @@
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
public class Client {
public static void main (String[] args) {
System.out.println("Singleton cook example ");
System.out.println("Bridge design pattern example ");
ITV tv = new SonyTV();

// Bridge between Remote and TV interface helps both of them to
// evolve independently
IndependentRemoteControl remote = new IndependentRemoteControl(tv);
remote.turnOn();
remote.setChannel(7);
remote.turnOff();
// additional methods
remote.recordProgram();


/*
// // DependentRemoteControl sub classes TV interface
// // and is forced to change even with minor changes in TV interface
// DependentRemoteControl remote = new DependentRemoteControl();
// remote.on();
// remote.switchProgram(23);
*/
}
}
50 changes: 50 additions & 0 deletions pattern/src/com/premaseem/ITV.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.premaseem;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/

public interface ITV {
public void on ();

public void off ();

public void switchProgram (int channel);
}

class SamsungTV implements ITV {
@Override
public void on () {
System.out.println("Samsung is turned on.");
}

@Override
public void off () {
System.out.println("Samsung is turned off.");
}

@Override
public void switchProgram (int channel) {
System.out.println("Samsung: channel - " + channel);
}
}

class SonyTV implements ITV {

@Override
public void on () {
System.out.println("Sony is turned on.");
}

@Override
public void off () {
System.out.println("Sony is turned off.");
}

@Override
public void switchProgram (int channel) {
System.out.println("Sony: channel - " + channel);
}
}