Skip to content

Commit 3efa634

Browse files
committed
coupling in java
1 parent dce3078 commit 3efa634

36 files changed

+450
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.metadata/

Loose_Coupling_1/.classpath

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
4+
<attributes>
5+
<attribute name="module" value="true"/>
6+
</attributes>
7+
</classpathentry>
8+
<classpathentry kind="src" path="src"/>
9+
<classpathentry kind="output" path="bin"/>
10+
</classpath>

Loose_Coupling_1/.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Loose_Coupling_1</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding/<project>=UTF-8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=17
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
13+
org.eclipse.jdt.core.compiler.release=enabled
14+
org.eclipse.jdt.core.compiler.source=17
534 Bytes
Binary file not shown.
531 Bytes
Binary file not shown.
707 Bytes
Binary file not shown.
134 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.masai.main;
2+
3+
//INTERFACE TO MAKE OUR CLASS LOOSELY COUPLED
4+
interface Vehicle{
5+
6+
void start();
7+
8+
}
9+
10+
11+
12+
/* LOOSE COUPLING => BASICALLY IN LOOSE COUPLING
13+
WE USE INTERFACES OR ABTRACT CLASSES TO
14+
MAKE OUR CLASSES LOOSELY
15+
COUPLED SO IN LOOSE COUPLING
16+
IF WE DO SOME CHANGES IN DEPENCY
17+
SO WE DON'T HAVE TO WORRY ABOUT DO THE CHANGES IN
18+
DEPENDENT CLASS BECUASE HERE WE USE
19+
INTERFACE OR ABSTRACT CLASS TO
20+
MAKE THE DEPENDECY LOOSELY COUPLED
21+
22+
DEGREE OF DEPENDENCY IS LOW
23+
24+
*/
25+
26+
//DEPENDECY CLASS
27+
class Bike implements Vehicle{
28+
29+
@Override
30+
public void start() {
31+
32+
System.out.println("trip start by bike");
33+
34+
}
35+
36+
37+
}
38+
39+
40+
//DEPENDECY CLASS
41+
class Car implements Vehicle{
42+
43+
@Override
44+
public void start() {
45+
46+
System.out.println("trip start by car");
47+
48+
}
49+
50+
51+
}
52+
53+
54+
55+
56+
57+
//DEPENDENT CLASS
58+
59+
/* SO HERE OUR TRAVELLER WANTS TO ANY
60+
VEHICLE SO THEY SIMPLY PASS
61+
THE OBJECT OF THAT VEHICLE IN
62+
THE V VERIABLE */
63+
64+
65+
public class Traveller {
66+
67+
// REFERENCE OF INTERFACE
68+
private Vehicle v;
69+
70+
public void setV(Vehicle v) {
71+
this.v = v;
72+
}
73+
74+
public static void main(String[] args) {
75+
76+
Traveller t = new Traveller();
77+
t.setV(new Bike());
78+
79+
t.v.start();
80+
81+
}
82+
83+
84+
}
85+
86+
87+
88+
89+

Loose_Coupling_2/.classpath

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
4+
<attributes>
5+
<attribute name="module" value="true"/>
6+
</attributes>
7+
</classpathentry>
8+
<classpathentry kind="src" path="src"/>
9+
<classpathentry kind="output" path="bin"/>
10+
</classpath>

Loose_Coupling_2/.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Loose_Coupling_2</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding/<project>=UTF-8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=17
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
13+
org.eclipse.jdt.core.compiler.release=enabled
14+
org.eclipse.jdt.core.compiler.source=17
601 Bytes
Binary file not shown.
535 Bytes
Binary file not shown.
544 Bytes
Binary file not shown.
591 Bytes
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.masai.main;
2+
3+
//INTERFACE FOR MAKI THE CLASSES LOOSELY COUPLED
4+
interface WritingInstrument{
5+
6+
void work();
7+
8+
9+
}
10+
11+
//DEPENDECY CLASS
12+
class Pen implements WritingInstrument{
13+
14+
@Override
15+
public void work() {
16+
17+
System.out.println("student using pen");
18+
19+
}
20+
21+
22+
}
23+
24+
//DEPENDECY CLASS
25+
class Pencil implements WritingInstrument{
26+
27+
@Override
28+
public void work() {
29+
System.out.println("student using pencil");
30+
31+
}
32+
33+
34+
}
35+
36+
37+
38+
//DEPENDNt CLASS
39+
40+
/* NOW OUR STUDENT WANTS TO USE PEN , PENCIL
41+
OR OBJECT HE CAN EASILY PASS THE OBJECT
42+
INTO SET METHOD BECUASE HERE OUR STUDENT CLASS
43+
IS LOOSELY COUPLED */
44+
45+
class Student {
46+
47+
// USING THE REFERENCE OF INTERFACE
48+
WritingInstrument wi;
49+
50+
public void setWi(WritingInstrument wi) {
51+
this.wi = wi;
52+
}
53+
54+
public void doWork() {
55+
56+
wi.work();
57+
58+
}
59+
60+
}
61+
62+
63+
64+
65+
public class Main {
66+
67+
public static void main(String[] args) {
68+
69+
Student st = new Student();
70+
71+
st.setWi(new Pen());
72+
73+
st.doWork();
74+
75+
}
76+
}

Tight_Coupling_1/.classpath

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
4+
<attributes>
5+
<attribute name="module" value="true"/>
6+
</attributes>
7+
</classpathentry>
8+
<classpathentry kind="src" path="src"/>
9+
<classpathentry kind="output" path="bin"/>
10+
</classpath>

Tight_Coupling_1/.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Tight_Coupling_1</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding/<project>=UTF-8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=17
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
13+
org.eclipse.jdt.core.compiler.release=enabled
14+
org.eclipse.jdt.core.compiler.source=17
504 Bytes
Binary file not shown.
501 Bytes
Binary file not shown.
575 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.masai.main;
2+
3+
//DEPENDENCY CLASS
4+
class Car {
5+
6+
public void start() {
7+
8+
System.out.println("start trip by car");
9+
10+
}
11+
12+
}
13+
14+
15+
//DEPENDENCY CLASS
16+
class Bike {
17+
18+
public void start() {
19+
20+
System.out.println("start trip by bike");
21+
22+
}
23+
24+
}
25+
26+
27+
/* TIGHT COUPLING => IF WE HAVE DEPENDENCY OF A CLASS
28+
IN OTHER CLASS(OBJECT OF ANOTHER CLASS CONTAIN)
29+
IF WE DO SOME CHANGES IN OUR DEPENDENCY AND AFTER
30+
SAME WE HAVE TO SOME CHANGES IN OUR DEPENDENT CLASS
31+
SO IT IS CALLED AS TIGHT COUPLING THAT IS NOT GOOD PRACTICE
32+
33+
DEGREE OB DEPENDENCY IS HIGH
34+
35+
*/
36+
37+
38+
39+
40+
//DEPENDENT CLASS
41+
/* NOW OUR TRAVELLER WANTS TO START THIER TRIP BY BIKE , OR ANY OTHER VEHICLE (ANOTHER CLASS)
42+
SO WE HAVE DO SOME CHANGES IN TRAVELLER CLASS ALSO
43+
INSTEAD OF CRETING CAR OBJECT WE HAVE TO CRAETE
44+
BIKE OBJECT WHICH IS NOT GOOD PRACTICE
45+
SO HERE OUR CLASSES IS TIGHT COUPLED WITH EACH OTHER
46+
*/
47+
public class Traveller {
48+
49+
50+
static Bike bike = new Bike();
51+
52+
public static void main(String[] args) {
53+
54+
bike.start();
55+
56+
}
57+
58+
}

Tight_Coupling_2/.classpath

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
4+
<attributes>
5+
<attribute name="module" value="true"/>
6+
</attributes>
7+
</classpathentry>
8+
<classpathentry kind="src" path="src"/>
9+
<classpathentry kind="output" path="bin"/>
10+
</classpath>

Tight_Coupling_2/.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Tight_Coupling_2</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>

0 commit comments

Comments
 (0)