Skip to content

Instantly share code, notes, and snippets.

@viveknaskar
Created March 11, 2021 13:27
Show Gist options
  • Save viveknaskar/2e651bb8925fe4a646258fb60b41c0d2 to your computer and use it in GitHub Desktop.
Save viveknaskar/2e651bb8925fe4a646258fb60b41c0d2 to your computer and use it in GitHub Desktop.
An illustration of Factory Method Design Pattern
import java.util.Scanner;
public class FactoryMethodExample {
public static void main(String[] args) {
System.out.println("Enter the type of car:\nAudi \nTesla");
Scanner in = new Scanner(System.in);
String carType = in.nextLine();
/**
* The factory method is called to get the object of the concrete classes by passing
* the information of the car from the user.
*/
CarFactory carFactory = new CarFactory();
carFactory.manufactureCar(carType.toLowerCase());
}
}
/**
* Abstract Class with abstract and concrete method
*/
abstract class Car {
public abstract void addEngineType();
public void deliverCar() {
System.out.println("Your car will be delivered at your doorstep.");
}
}
/**
* Concrete class 'AudiCar' extending the abstract Class
*/
class AudiCar extends Car {
@Override
public void addEngineType() {
System.out.println("You have ordered a car with gasoline Engine.");
}
}
/**
* Concrete class 'TeslaCar' extending the abstract Class
*/
class TeslaCar extends Car {
@Override
public void addEngineType() {
System.out.println("You have ordered a car with electric Engine. ");
}
}
/**
* In Factory method, the object of the Car is created.
*/
class CarFactory {
public Car manufactureCar(String type){
Car car;
switch (type.toLowerCase())
{
case "audi":
car = new AudiCar();
break;
case "tesla":
car = new TeslaCar();
break;
default: throw new IllegalArgumentException("No such car available.");
}
car.addEngineType();
car.deliverCar();
return car;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment