Skip to content

Commit 436e951

Browse files
committed
Concept of tight coupling and loose coupling
1 parent 0a20831 commit 436e951

14 files changed

Lines changed: 239 additions & 88 deletions

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,16 @@
1+
## Video 2.1 of Design Patterns Tutorial uses this code
2+
3+
# What is tight coupling
4+
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.
5+
6+
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.
7+
8+
# What is loose coupling
9+
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.
10+
11+
A loosely-coupled class can be consumed and tested independently of other (concrete) classes.
12+
13+
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.
14+
115
# DesignPatternsJava9
216
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.

src/CyclomatricComplexity.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

src/CyclomatricSimplicity.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/com/premaseem/loose/Bike.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.premaseem.loose;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
@copyright: 2018 Packt Publication
8+
*/
9+
public class Bike implements Vehicle {
10+
11+
@Override
12+
public void move () {
13+
System.out.println("Moving");
14+
}
15+
}

src/com/premaseem/loose/Car.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.premaseem.loose;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
@copyright: 2018 Packt Publication
8+
*/
9+
public class Car implements Vehicle {
10+
11+
12+
@Override
13+
public void move () {
14+
System.out.println("Moving");
15+
}
16+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.premaseem.loose;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
@copyright: 2018 Packt Publication
8+
*/
9+
public class Client {
10+
public static void main (String[] args) {
11+
LooselyCoupledTraveller traveller = new LooselyCoupledTraveller();
12+
13+
// Here client does not need to know about implementor
14+
15+
Vehicle car= traveller.getCar();
16+
Vehicle bike = traveller.getBike();
17+
Vehicle truck= traveller.getTruck();
18+
// Easy to add any other vehicle without
19+
// Vehicle plane= traveller.getPlane();
20+
21+
traveller.startJourney(car);
22+
// Polymorphism in full force
23+
traveller.startJourney(bike);
24+
traveller.startJourney(truck);
25+
26+
// Take Away:
27+
// Class needs not to be modified or changes, compiled or retested
28+
// when any sub class needs to be added or changed.
29+
// All classes are independent
30+
31+
}
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.premaseem.loose;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
@copyright: 2018 Packt Publication
8+
*/
9+
public class LooselyCoupledTraveller {
10+
Vehicle car= new Car();
11+
Vehicle bike = new Bike();
12+
Vehicle truck= new Truck();
13+
14+
public Vehicle getCar () {
15+
return car;
16+
}
17+
18+
public Vehicle getBike () {
19+
return bike;
20+
}
21+
22+
public Vehicle getTruck () {
23+
return truck;
24+
}
25+
26+
public void startJourney (Vehicle car){
27+
car.move();
28+
}
29+
30+
}

src/com/premaseem/loose/Truck.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.premaseem.loose;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
@copyright: 2018 Packt Publication
8+
*/
9+
public class Truck implements Vehicle {
10+
public void move () {
11+
System.out.println("Moving");
12+
}
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.premaseem.loose;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
@copyright: 2018 Packt Publication
8+
*/
9+
public interface Vehicle {
10+
void move ();
11+
}

src/com/premaseem/tight/Bike.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.premaseem.tight;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
@copyright: 2018 Packt Publication
8+
*/
9+
public class Bike {
10+
11+
12+
public void move () {
13+
System.out.println("Moving");
14+
}
15+
}

0 commit comments

Comments
 (0)