diff --git a/4_4_Bridge_pattern_sequence.png b/4_4_Bridge_pattern_sequence.png new file mode 100644 index 0000000..f568972 Binary files /dev/null and b/4_4_Bridge_pattern_sequence.png differ diff --git a/Bridge Design Pattern class diagram.png b/Bridge Design Pattern class diagram.png new file mode 100644 index 0000000..e6cb158 Binary files /dev/null and b/Bridge Design Pattern class diagram.png differ diff --git a/README.md b/README.md index 9e0034c..565f904 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/diagrams/4_4_Bridge_pattern_sequence.png b/diagrams/4_4_Bridge_pattern_sequence.png new file mode 100644 index 0000000..f568972 Binary files /dev/null and b/diagrams/4_4_Bridge_pattern_sequence.png differ diff --git a/diagrams/Bridge Design Pattern class diagram.png b/diagrams/Bridge Design Pattern class diagram.png new file mode 100644 index 0000000..e6cb158 Binary files /dev/null and b/diagrams/Bridge Design Pattern class diagram.png differ diff --git a/pattern/src/com/premaseem/BridgeRemoteControl.java b/pattern/src/com/premaseem/BridgeRemoteControl.java new file mode 100644 index 0000000..71378ba --- /dev/null +++ b/pattern/src/com/premaseem/BridgeRemoteControl.java @@ -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 "); + } + +} \ No newline at end of file diff --git a/pattern/src/com/premaseem/Client.java b/pattern/src/com/premaseem/Client.java index 15f05df..d38fadf 100644 --- a/pattern/src/com/premaseem/Client.java +++ b/pattern/src/com/premaseem/Client.java @@ -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); + */ } } diff --git a/pattern/src/com/premaseem/ITV.java b/pattern/src/com/premaseem/ITV.java new file mode 100644 index 0000000..125003e --- /dev/null +++ b/pattern/src/com/premaseem/ITV.java @@ -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); + } +} \ No newline at end of file