Last active
November 30, 2023 11:59
-
-
Save kishankg/bdafd23c34a0198345b5876d09173a6c to your computer and use it in GitHub Desktop.
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
// Product Interface: DatabaseConnection | |
interface DatabaseConnection { | |
void connect(); | |
void disconnect(); | |
} | |
// Concrete Products: MySqlConnection and PostgreSqlConnection | |
class MySqlConnection implements DatabaseConnection { | |
@Override | |
public void connect() { | |
System.out.println("Connected to MySQL database"); | |
// MySQL-specific connection logic | |
} | |
@Override | |
public void disconnect() { | |
System.out.println("Disconnected from MySQL database"); | |
// MySQL-specific disconnection logic | |
} | |
} | |
class PostgreSqlConnection implements DatabaseConnection { | |
@Override | |
public void connect() { | |
System.out.println("Connected to PostgreSQL database"); | |
// PostgreSQL-specific connection logic | |
} | |
@Override | |
public void disconnect() { | |
System.out.println("Disconnected from PostgreSQL database"); | |
// PostgreSQL-specific disconnection logic | |
} | |
} | |
// Factory Interface: DatabaseConnectionFactory | |
interface DatabaseConnectionFactory { | |
DatabaseConnection createConnection(); | |
} | |
// Concrete Factories: MySqlConnectionFactory and PostgreSqlConnectionFactory | |
class MySqlConnectionFactory implements DatabaseConnectionFactory { | |
@Override | |
public DatabaseConnection createConnection() { | |
return new MySqlConnection(); | |
} | |
} | |
class PostgreSqlConnectionFactory implements DatabaseConnectionFactory { | |
@Override | |
public DatabaseConnection createConnection() { | |
return new PostgreSqlConnection(); | |
} | |
} | |
// Client Code | |
public class DatabaseClient { | |
public static void main(String[] args) { | |
// Using a factory to create a MySQL database connection | |
DatabaseConnectionFactory mySqlConnectionFactory = new MySqlConnectionFactory(); | |
DatabaseConnection mySqlConnection = mySqlConnectionFactory.createConnection(); | |
mySqlConnection.connect(); | |
mySqlConnection.disconnect(); | |
// Using a factory to create a PostgreSQL database connection | |
DatabaseConnectionFactory postgreSqlConnectionFactory = new PostgreSqlConnectionFactory(); | |
DatabaseConnection postgreSqlConnection = postgreSqlConnectionFactory.createConnection(); | |
postgreSqlConnection.connect(); | |
postgreSqlConnection.disconnect(); | |
} | |
} |
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
// Product Interface | |
interface Vehicle { | |
void start(); | |
void stop(); | |
} | |
// Concrete Products | |
class Car implements Vehicle { | |
@Override | |
public void start() { | |
System.out.println("Car started."); | |
} | |
@Override | |
public void stop() { | |
System.out.println("Car stopped."); | |
} | |
} | |
class ElectricCar implements Vehicle { | |
@Override | |
public void start() { | |
System.out.println("Electric car started."); | |
} | |
@Override | |
public void stop() { | |
System.out.println("Electric car stopped."); | |
} | |
} | |
// Factory Interface with Logic | |
interface VehicleFactory { | |
Vehicle createVehicle(String fuelType); | |
} | |
// Concrete Factory with Logic | |
class VehicleFactoryImpl implements VehicleFactory { | |
@Override | |
public Vehicle createVehicle(String fuelType) { | |
if ("Gasoline".equalsIgnoreCase(fuelType)) { | |
return new Car(); | |
} else if ("Electric".equalsIgnoreCase(fuelType)) { | |
return new ElectricCar(); | |
} else { | |
throw new IllegalArgumentException("Invalid fuel type"); | |
} | |
} | |
} | |
// Client | |
public class Client { | |
public static void main(String[] args) { | |
// Using the factory with logic to create a Gasoline-powered vehicle | |
VehicleFactory vehicleFactory = new VehicleFactoryImpl(); | |
Vehicle gasolineCar = vehicleFactory.createVehicle("Gasoline"); | |
gasolineCar.start(); | |
gasolineCar.stop(); | |
// Using the factory with logic to create an Electric vehicle | |
Vehicle electricCar = vehicleFactory.createVehicle("Electric"); | |
electricCar.start(); | |
electricCar.stop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment