3. Abstract Factory Pattern


Abstract Factory Pattern

 - Almost similar to Factory Pattern, except the fact that it’s more like factory of factories. If you are familiar with factory design pattern in java, you will notice that we have a single Factory class that returns the different sub-classes based on the input provided. 

- Generally,  factory class uses if-else or switch-case statement to achieve this. However, in Abstract Factory pattern, we get rid of if-else block and have a factory class for each sub-class and then an Abstract Factory class that will return the sub-class, based on the input factory class.

- In fact, An abstract factory is a factory that returns factories. Why is this layer of abstraction useful? A normal factory can be used to create sets of related objects. An abstract factory returns factories. Hence, an abstract factory is used to return factories that can be used to create sets of related objects.

-  The Abstract Factory Pattern is a way of organizing how you create groups of things that are related to each other. It provides a set of rules or instructions that let you create different types of things without knowing exactly what those things are. This helps you keep everything organized and lets you switch between different types easily.


Program :

// package Practise;

/*

2. Abstract Factory Pattern:
   - The Abstract Factory Pattern is also a creational pattern, but it 
    provides an interface for creating families of related or dependent 
    objects without specifying their concrete classes.
   - It's an extension of the Factory Pattern and works around the concept of
     families of objects rather than just individual objects.
   - It defines an abstract factory interface that declares methods for 
    creating the various objects. Each concrete implementation of this 
    interface represents a different factory that produces objects of a 
    particular family.
   - Abstract Factory Pattern is used when you need to create families of 
    related or dependent objects, ensuring that the created objects are 
    compatible.
   - It often involves multiple Factory Patterns working together, with each 
    factory responsible for creating a different kind of object within a 
    family.

Here's a simple analogy to differentiate the two:
- Factory Pattern: Think of a pizza restaurant where you have a single chef 
    (factory) responsible for making different types of pizzas based on the
     customer's order.
- Abstract Factory Pattern: Now, consider a scenario where you have a pizza 
    restaurant chain. Each branch of the chain has its own chef (factory) but
     follows a specific style of making pizzas (e.g., Italian, American). The 
    Abstract Factory Pattern helps you manage these different styles of 
    pizza-making across different branches of the chain.


                Bike interface
        Mounting Bike       Sport Bike


                AbstractFactoryClass
            MountingFactory     SportFactory
        
        
        Bikefactory    ( static Method for Bike(abstractfactory)
*/


interface Bike
{
    String name();
    int price();
}

class SportBike implements Bike
{
    public String name()
    {
        return "Sport Bike";
    }

    public int price()
    {
        return 80000;
    }
}


class MoutantBike implements Bike
{
    public String name()
    {
        return "Mountant Bike";
    }

    public int price()
    {
        return 60000;
    }
}


abstract class AbstractFactoryBike
{
    public abstract Bike createBike();
}


class SportBikeFactory extends AbstractFactoryBike
{
    public Bike createBike()
    {
        return new SportBike();
    }
}



class MountantBikeFactory extends AbstractFactoryBike
{
    public Bike createBike()
    {
        return new MoutantBike();
    }
}


class Bikefactory
{
    public static Bike getBike(AbstractFactoryBike abstractFactoryBike)
    {
        return abstractFactoryBike.createBike();
    } 
}

public class AbstactFactory {
    public static void main(String[] args) {
        

        Bike b1 =  Bikefactory.getBike(new SportBikeFactory());
        System.out.println("Bike type :  "+b1.name() +" , Price is :"+b1.price() );
        System.out.println(b1.hashCode());

        Bike b2 =  Bikefactory.getBike(new MountantBikeFactory());
        System.out.println("Bike type :  "+b2.name() +" , Price is :"+b2.price() );
        System.out.println(b2.hashCode());
    

    }

}

// Output

Bike type : Sport Bike , Price is :80000 1746572565 Bike type : Mountant Bike , Price is :60000 321001045





Comments

Popular posts from this blog

Introduction Design Pattern