Skip to content

Concept of tight coupling and loose coupling #8

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 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="/DesignPatternsJava9/src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Compiled class file
.idea/*
*.class

# Log file
*.log

*.iws
# BlueJ files
*.ctxt

Expand Down
15 changes: 15 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>DesignPatternsJava9</name>
<comment/>
<projects/>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments/>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
6 changes: 6 additions & 0 deletions DesignPatternsJava9.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<component inheritJdk="true">
<output-test url="file://$MODULE_DIR$/bin/test/DesignPatternsJava9"/>
<exclude-output/>
<contentEntry url="file://$MODULE_DIR$"/>
</component>
13 changes: 13 additions & 0 deletions DesignPatternsJava9.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager">
<output url="file://$MODULE_DIR$/bin/production/DesignPatternsJava9" />
<output-test url="file://$MODULE_DIR$/bin/test/DesignPatternsJava9" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
# 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.
## Video 2.1 of Design Patterns Tutorial uses this code

# What is tight coupling
This scenario arises when a class is not desiged to be independent and assumes too many responsibilities, or when one concern is spread over many classes rather than having its own class.

The tight coupling between classes creates a ripple effect, which means when one class is changed it impacts too many other classes. A minor change in one class would need to modify and compile many other classes.

# What is loose coupling
Loose coupling is achieved by means of a design that promotes single-responsibility and separation of concerns. If the only knowledge that class A has about class B, is what class B has exposed through its interface, then class A and class B are said to be loosely coupled.

A loosely-coupled class can be consumed and tested independently of other (concrete) classes.

Interfaces are a powerful tool to use for decoupling. Classes can communicate through interfaces rather than other concrete classes, and any class can be on the other end of that communication simply by implementing the interface.

# The fragile base-class problem
Base classes are considered fragile because you can modify a base class in a seemingly safe way, but this new behavior, when inherited by the derived classes, might cause the derived classes to malfunction. You can't tell whether a base-class change is safe simply by examining the base class's methods in isolation; you must look at (and test) all derived classes as well. Moreover, you must check all code that uses both base-class and derived-class objects too, since this code might also be broken by the new behavior. A simple change to a key base class can render an entire program inoperable.

### 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/
15 changes: 15 additions & 0 deletions src/com/premaseem/loose/Bike.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.premaseem.loose;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
public class Bike implements Vehicle {

@Override
public void move () {
System.out.println("Moving");
}
}
16 changes: 16 additions & 0 deletions src/com/premaseem/loose/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.premaseem.loose;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
public class Car implements Vehicle {


@Override
public void move () {
System.out.println("Moving");
}
}
32 changes: 32 additions & 0 deletions src/com/premaseem/loose/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.premaseem.loose;

/*
@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) {
LooselyCoupledTraveller traveller = new LooselyCoupledTraveller();

// Here client does not need to know about implementor

Vehicle car= traveller.getCar();
Vehicle bike = traveller.getBike();
Vehicle truck= traveller.getTruck();
// Easy to add any other vehicle without
// Vehicle plane= traveller.getPlane();

traveller.startJourney(car);
// Polymorphism in full force
traveller.startJourney(bike);
traveller.startJourney(truck);

// Take Away:
// Class needs not to be modified or changes, compiled or retested
// when any sub class needs to be added or changed.
// All classes are independent

}
}
30 changes: 30 additions & 0 deletions src/com/premaseem/loose/LooselyCoupledTraveller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.premaseem.loose;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
public class LooselyCoupledTraveller {
Vehicle car= new Car();
Vehicle bike = new Bike();
Vehicle truck= new Truck();

public Vehicle getCar () {
return car;
}

public Vehicle getBike () {
return bike;
}

public Vehicle getTruck () {
return truck;
}

public void startJourney (Vehicle car){
car.move();
}

}
13 changes: 13 additions & 0 deletions src/com/premaseem/loose/Truck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.premaseem.loose;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
public class Truck implements Vehicle {
public void move () {
System.out.println("Moving");
}
}
11 changes: 11 additions & 0 deletions src/com/premaseem/loose/Vehicle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.premaseem.loose;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
public interface Vehicle {
void move ();
}
15 changes: 15 additions & 0 deletions src/com/premaseem/tight/Bike.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.premaseem.tight;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
public class Bike {


public void move () {
System.out.println("Moving");
}
}
15 changes: 15 additions & 0 deletions src/com/premaseem/tight/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.premaseem.tight;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
public class Car {


public void move () {
System.out.println("Moving");
}
}
21 changes: 21 additions & 0 deletions src/com/premaseem/tight/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.premaseem.tight;

/*
@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) {
TightCoupledTraveller traveller = new TightCoupledTraveller();

Car car= traveller.getCar();
Bike bike = traveller.getBike();
Truck truck= traveller.getTruck();

traveller.startJourney(car);
traveller.startJourney(bike);
traveller.startJourney(truck);
}
}
44 changes: 44 additions & 0 deletions src/com/premaseem/tight/TightCoupledTraveller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.premaseem.tight;

import com.premaseem.tight.Bike;
import com.premaseem.tight.Car;
import com.premaseem.tight.Truck;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
public class TightCoupledTraveller {
Car car= new Car();
Bike bike = new Bike();
Truck truck= new Truck();

public Car getCar () {
return car;
}

public Bike getBike () {
return bike;
}

public Truck getTruck () {
return truck;
}

public void startJourney (Car car){
car.move();
}

public void startJourney (Bike bike){
bike.move();
}

public void startJourney (Truck truck){
truck.move();
}



}
13 changes: 13 additions & 0 deletions src/com/premaseem/tight/Truck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.premaseem.tight;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
public class Truck {
public void move () {
System.out.println("Moving");
}
}