Created
April 17, 2025 17:08
-
-
Save thinkphp/eca8d98ecc7bca0415d1717e4ac05e91 to your computer and use it in GitHub Desktop.
Transportation Factory Design Pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//abstract product | |
abstract class Transport { | |
protected String cargo; | |
protected String destination; | |
public void setCargo(String cargo) { | |
this.cargo = cargo; | |
} | |
public void setDestination(String destination) { | |
this.destination = destination; | |
} | |
public abstract void deliver(); | |
} | |
//concret Pruducts | |
class Truck extends Transport { | |
@Override | |
public void deliver() { | |
System.out.println("Delivering " + cargo + "by land to " + destination); | |
System.out.println("Using trucks for road transportation"); | |
} | |
} | |
class Ship extends Transport { | |
@Override | |
public void deliver() { | |
System.out.println("Delivering " + cargo + "by sea to " + destination); | |
System.out.println("Using cargo ships for maritime transportation"); | |
} | |
} | |
class Airplane extends Transport { | |
@Override | |
public void deliver() { | |
System.out.println("Delivering " + cargo + "by air to " + destination); | |
System.out.println("Using cargo planes for air transportation"); | |
} | |
} | |
//abstract Creator | |
abstract class LogisticsFactory { | |
//factory method | |
public abstract Transport createTransport(); | |
public Transport planDelivery(String cargo, String destination) { | |
Transport transport = createTransport(); | |
transport.setCargo(cargo); | |
transport.setDestination(destination); | |
return transport; | |
} | |
} | |
//concret creators | |
class RoadLogistics extends LogisticsFactory { | |
@Override | |
public Transport createTransport() { | |
return new Truck(); | |
} | |
} | |
class SeaLogistics extends LogisticsFactory { | |
@Override | |
public Transport createTransport() { | |
return new Ship(); | |
} | |
} | |
class AirLogistics extends LogisticsFactory { | |
@Override | |
public Transport createTransport() { | |
return new Airplane(); | |
} | |
} | |
//client code | |
public class LogisticsApplication { | |
public static void main(String[] args) { | |
//create a road Delivery | |
LogisticsFactory roadLogistics = new RoadLogistics(); | |
Transport truck = roadLogistics.planDelivery("Furniture","Local Warehouse"); | |
truck.deliver(); | |
LogisticsFactory seaLogistics = new SeaLogistics(); | |
Transport ship = seaLogistics.planDelivery("Electronics","California"); | |
ship.deliver(); | |
LogisticsFactory airLogistics = new AirLogistics(); | |
Transport plane = airLogistics.planDelivery("Medical Supplies","Emergency Relief Center"); | |
plane.deliver(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment