Skip to content

Factory design pattern is used when we have a super class with multiple sub-classes and based on input, we need to return one of the sub-class. This pattern take out the responsibility of instantiation of a class from client program to the factory class. We can apply Singleton pattern on Factory class or make the factory method static

Notifications You must be signed in to change notification settings

Design-pattrns/Factory-Pattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FactoryDesignPattern

Factory Design Pattern Implemented in Android

A design pattern is a formal way of documenting a solution to a design problem. Design patterns promotes reusability that leads to more robust and highly maintainable code. Java Design Patterns are divided into three categories

  1. Creational
  2. Structural
  3. Behavioral

Factory Pattern is Creational Design pattern.Itis used when we have a parent class with multiple child classes and based on input, we need to return one of the child-class.

#Factory Class: Here is the basic implementation of Factory Class.

 public class CarFactory {
 
    public static Car getCar(Cars type) { 
       if (type == CarsConst.maruti800) {
            return new MarutiCar();
        } else if (type == CarsConst.alto) {
            return new AltoCar();
        } else if (type == CarsConst.swift){
            return new SwiftCar();
        }
        return null;
    }
}

About

Factory design pattern is used when we have a super class with multiple sub-classes and based on input, we need to return one of the sub-class. This pattern take out the responsibility of instantiation of a class from client program to the factory class. We can apply Singleton pattern on Factory class or make the factory method static

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published