Design Pattern 101: Singleton and Factory Pattern

Singleton Pattern

Car.javapublic class Car(){
//default empty constructor
public Car(){}
public void start(){
System.out.println("Car engine start");
}
}
CarShop.javapublic class CarShop(){

//because we want to keep track on the number of cars that we have sold
int carSold;
//we start our bussiness from zero
public CarShop(){
carSold = 0;
}
//function to sell our car, as it returns car
public Car sellCar(){
carSold++;
return new Car();
}
}
//you do this for every classes that wants to get car
CarShop carShop = new CarShop();
Car car = carShop.sellCar();
CarShop.javapublic class CarShop(){

private static CarShop carShop;
private int carSold;
//see that we make the constructor private
private CarShop(){
carSold = 0;
}
//you make a static function that return the carShop
public static CarShop getInstance(){
if(carShop == null)
carShop = new CarShop();
return carShop;
}
//function to sell our car, as it returns car
public Car sellCar(){
carSold++;
return new Car();
}
}
Car car = CarShop.getInstance().sellCar();

Factory Pattern

//more Car classes
SportCar.java
public SportCar()
{
public SportCar(){}
public start(){
System.out.println("Sport car engine start!")
}
}
FlyingCar.javapublic FlyingCar()
{
public FlyingCar(){}
public start(){
System.out.println("Flying car engine start!")
}
}
...
//and etc
//And now your sell car function
public SportCar sellSportCar()
{
carSold++;
return new SportCar();
}
... make more functions for each car and so on..
  • The CarShop doesn’t need to know how the car is made, it only needs the car, in summary car shop is not supposed to instantiate the Car object.
  • We need to keep on creating new function for each new car that will be sold in the shop.
  • Our Car object model become unused.
Car.javapublic interface Car{
void start();
}
..onto our other classesSportCar.javapublic class SportCar implements Car{
public SportCar(){};
public void start(){
System.out.println("Sport Car started");
}
}
FlyingCar.javapublic class FlyingCar implements Car{
public FlyingCar(){};
public void start(){
System.out.println("Flying Car started");
}
}
.. and so on
CarFactory.javapublic class CarFactory {

public Car getCar(String carType){
if(carType.equalsIgnoreCase("SportCar")){
return new SportCar();

} else if(carType.equalsIgnoreCase("FamilyCar")){
return new FamilyCar();

} else if(carType.equalsIgnoreCase("FlyingCar")){
return new FlyingCar();
}

return null;
}
}
CarShop.javapublic class CarShop{

...
//we need to make car factory now
public carFactory;
..public CarShop()
{
...
//we need to initialize our car factory before hand
CarFactory = new CarFactory();
}
....
//now our functions also return car
public Car sellCar(String carType)
{
carSold++;
//short and simple, our factory now provides abstraction to our shop
return carFactory.makeCar(carType);
}
}..
// when we call our car shop now it will looks like this
Car sportCar = carShop.sellCar("SportCar");
Car flyingCar = carShop.sellCar("FlyingCar");
sportCar.start(); //Sport car started
flyingCar.start(); //Flying car started
  • Our shop now doesn’t know how the car is built.
  • Adding new car simply need to add more logical path inside our factory.
  • Our car interface is used for everytime a new car type is out.

Wrapping up

--

--

--

Your Usual Software Engineer Guy, but also Android and Serverless enthusiast.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Set up Zulu java enthronement in Ubuntu

Its Never Too Late, To Start Over, To Begin Again…

DevOps: everything you need to know

Understanding CSS Specificity

How does the shell work?

Dynamic translations in Outsystems Mobile apps, using MultiLingual plugin — Save the translations…

My Unit 5 Construct Week SugarCosmetic Project At Masai

How to send emails from Powershell using an SMTP Service

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pramono Winata

Pramono Winata

Your Usual Software Engineer Guy, but also Android and Serverless enthusiast.

More from Medium

Java vs .Net: Which Technology Is Better For Software Development?

Clean Architecture in Software Engineering

Introduction to Application Development

PostgreSQL and typeorm — 9 Tips, tricks and common issues