Advertisement
Oppenheimer

Factory and AbstractFactory Pattern

Feb 24th, 2025
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. /*
  4.  * Factory pattern : creating object on some conditions ->
  5.  * condn 1 -> obj 1
  6.  * condn 2 -> obj 2 ..
  7.  *
  8.  *
  9.  * Abstract Factory : factory of factories
  10.  * when we want to group different prodcuts
  11.  * condn1 -> facotry 1 -> subcondn1 -> obj1
  12.  * condn2 -> facotry 2 -> subcondn2-> obj2;
  13.  */
  14.  
  15. interface Shape{
  16.     public void draw();
  17. }
  18.  
  19. abstract class Factory{
  20.     abstract Shape getShape(String input);
  21.     abstract Vehicle getVehicle(String input);
  22. }
  23.  
  24. class Circle implements Shape{
  25.     public void draw(){
  26.         System.out.println("Circle");
  27.     }
  28. }
  29.  
  30.  
  31. class Rectangle implements Shape{
  32.     public void draw(){
  33.         System.out.println("Rectangle");
  34.     }
  35. }
  36.  
  37. class ShapeFactory extends Factory{
  38.     Shape getShape(String input){
  39.         if(input == "circle"){
  40.             return new Circle();
  41.         }else if(input == "rectangle"){
  42.             return new Rectangle();
  43.         }else{
  44.             return new Circle();
  45.         }
  46.     }
  47. }
  48.  
  49. interface Vehicle{
  50.     public void run();
  51. }
  52.  
  53. class Car implements Vehicle{
  54.     public void run(){
  55.         System.out.println("Car");
  56.     }
  57. }
  58.  
  59.  
  60. class Bike implements Vehicle{
  61.     public void run(){
  62.         System.out.println("Bike");
  63.     }
  64. }
  65. class VehicleFactory extends Factory{
  66.     Vehicle getVehicle(String input){
  67.         if(input == "car"){
  68.             return new Car();
  69.         }else if(input == "bike"){
  70.             return new Bike();
  71.         }else{
  72.             return new Bike();
  73.         }
  74.     }
  75. }
  76.  
  77. class AbstractFactory{
  78.     Factory getFactory(String input){
  79.         if(input == "shape"){
  80.             return new ShapeFactory();
  81.         }else{
  82.             return new VehicleFactory();
  83.         }
  84.     }
  85. }
  86.  
  87.  
  88. public class Main{
  89.     public static void main(String args[]) throws Exception{
  90.         // factory pattern
  91.         ShapeFactory shapeFactory = new ShapeFactory();
  92.         Shape shape = shapeFactory.getShape("circle");
  93.         shape.draw();
  94.  
  95.         // abstract facotry pattern
  96.         AbstractFactory abstractFactory = new AbstractFactory();
  97.         Factory vehicleFactory = abstractFactory.getFactory("vehicle");
  98.         Vehicle vehicle = vehicleFactory.getVehicle("car");
  99.         vehicle.run();
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement