kchaitanya-training / BFS45

0 stars 0 forks source link

session4: loose coupling and tight coupling #16

Open kchaitanya-training opened 9 months ago

nimadsherpa commented 9 months ago
package coupling;

public interface Car {

    void carDetails();
    }

package coupling;

public class Bmw implements Car{

    String make;
    String model;
    int year;

    Bmw(String make, String model, int year){
        this.make= make;
        this.model = model;
        this.year = year;
    }

    public void carDetails() {
        System.out.println("\tMake: "+ make);
        System.out.println("\tModel: "+ model);
        System.out.println("\tYear: "+ year);
    }
}

package coupling;

public class Toyota implements Car{

    String make;
    String model;
    int year;

    Toyota(String make, String model, int year){
        this.make= make;
        this.model = model;
        this.year = year;
    }

    public void carDetails() {
        System.out.println("\tMake: "+ make);
        System.out.println("\tModel: "+ model);
        System.out.println("\tYear: "+ year);
    }
}

package coupling;

public class Main {

    void method() {
        System.out.println("It is A");

        System.out.println("Car1 info:");
        Car car1=new Bmw("BMW", "5 series", 2023);
        car1.carDetails();

        System.out.println("Car2 info:");
        Car car2=new Toyota("Toyota", "Tacoma", 2022);
        car2.carDetails();

    }

    public static void main(String[] args) {
        Main a=new Main();
        a.method();
    }

}
bik-on-git commented 9 months ago

class Main{ public void mainMethod() { System.out.println("this is loose coupling"); I i = new MySqlB(); I j = new OracleA(); i.connection(); j.connection(); } } interface I{ void connection();

} class OracleA implements I{ public void connection() { System.out.println(" This is oracle calling."); } } class MySqlB implements I { public void connection() { System.out.println("This is mysql calling"); } } public class LooseTest {

public static void main(String[] args) {
    Main m = new Main();
    m.mainMethod();

//Output // Tight Coupling // I am M.

}

}

bik-on-git commented 9 months ago

class M{ void methodM() { System.out.println("I am M."); } } class N{ void methodN() { System.out.println("Tight Coupling"); M m = new M(); m.methodM(); } }

public class TightTest { public static void main(String[] args) {

    N n = new N();
    n.methodN();

}}
mwdorji commented 9 months ago

public class Sport { public static void main(String[] args) {

    Basketball bb = new Basketball();
    bb.shooting();
    bb.dribbling();
    bb.blocking();

    Football fb = new Football();
    fb.shooting();
    fb.dribbling();

}

}

public class Basketball implements shoot{ public void shooting() { System.out.println("Player shoots the ball"); } public void dribbling() { System.out.println("Player dribbles the ball"); } public void blocking() { System.out.println("Player blocks the ball"); }

}

public class Football { public void shooting() { System.out.println("Player shoots the ball"); } public void dribbling() { System.out.println("Player dribbles the ball"); }

}

public interface block { public void blocking();

}

public interface dribble { public void dirbbling();

}

public interface dribble { public void dirbbling();

}

output Player shoots the ball Player dribbles the ball Player blocks the ball Player shoots the ball Player dribbles the ball

dhawasangbolama2 commented 9 months ago

package CarBrands;

//loose coupling

public interface Car {

void features();

public static void main(String[] args) {

    Bmw newBmw = new Bmw();
    newBmw.features();

    Audi newAudi = new Audi();
    newAudi.features();
}

}

package CarBrands;

class Audi implements Car {

@Override public void features() { System.out.println("It is luxurious."); }

}

package CarBrands;

class Bmw implements Car {

@Override
public void features() {
// TODO Auto-generated method stub
System.out.println("It is fast.");

}

}

OUTPUT It is fast. It is luxurious.

package VehiclesWithTires; // tight coupling

class VehiclesWithTires{

public static void main(String[] args) {
    Vehicle CompleteV = new Vehicle();
    CompleteV.vehiclesGotTires();

}

} package VehiclesWithTires; //class vehicle output depends on class tires

public class Vehicle {

public void vehiclesGotTires() {
    System.out.println("I have car.");
    Tires T = new Tires();
    T.vehicles();   
}

} package VehiclesWithTires; public class Tires {

    public void vehicles() {
        System.out.println("My car got 4 wheels.");
    }

}

OUTPUT I have car. My car got 4 wheels.