Bridge Pattern in java
with example central locking system and gear locking system:
Bridge is a structural design pattern that divides business logic or huge class into separate class hierarchies that can be developed independently. One of these hierarchies (often called the Abstraction) will get a reference to an object of the second hierarchy (Implementation).
The source code is :
package bridgepattern;
interface Product {
public String productName();
public void produce();
}
class CentralLocking implements Product{
private final String productName;
public CentralLocking(String productName){
this.productName = productName;
}
@Override
public String productName() {
return productName;
}
@Override
public void produce() {
System.out.println("Producing Central Locking System");
}
}
class GearLocking implements Product{
private final String productName;
public GearLocking(String productName){
this.productName = productName;
}
@Override
public String productName() {
return productName;
}
@Override
public void produce() {
System.out.println("Producing Gear Locking System");
}
}
abstract class Car {
private final Product product;
private final String carType;
public Car(Product product,String carType){
this.product = product;
this.carType = carType;
}
public abstract void assemble();
public abstract void produceProduct();
public void printDetails(){
System.out.println("Car: "+carType+", Product:"+product.productName());
}
}
class BigWheel extends Car{
private final Product product;
private final String carType;
public BigWheel(Product product, String carType) {
super(product, carType);
this.product = product;
this.carType = carType;
}
@Override
public void assemble() {
System.out.println("Assembling "+product.productName()+" for "+carType);
}
@Override
public void produceProduct() {
product.produce();
System.out.println("Modifing product "+product.productName()+" according to "+carType);
}
}
class Motoren extends Car{
private final Product product;
private final String carType;
public Motoren(Product product, String carType) {
super(product, carType);
this.product = product;
this.carType = carType;
}
@Override
public void assemble() {
System.out.println("Assembling "+product.productName()+" for "+carType);
}
@Override
public void produceProduct() {
product.produce();
System.out.println("Modifing product "+product.productName()+" according to "+carType);
}
}
public class Bridgepattern {
public static void main(String[] args) {
Product product = new CentralLocking("Central Locking System");
Product product2 = new GearLocking("Gear Locking System");
Car car = new BigWheel(product , "BigWheel xz model");
car.produceProduct();
car.assemble();
car.printDetails();
System.out.println();
car = new BigWheel(product2 , "BigWheel xz model");
car.produceProduct();
car.assemble();
car.printDetails();
System.out.println("-----------------------------------------------------");
car = new Motoren(product, "Motoren lm model");
car.produceProduct();
car.assemble();
car.printDetails();
System.out.println();
car = new Motoren(product2, "Motoren lm model");
car.produceProduct();
car.assemble();
car.printDetails();
}
}
0 Comments