Summary Manufacturing unit






Creational Design Sample



When to make use of

  • To help households of associated or dependent objects.
  • To encapsulate platform dependencies to make an software transportable.
  • To forestall shopper code from utilizing the ‘new’ operator.
  • To simply swap the underlying platform with minimal adjustments.



Intent

Present an interface for creating households of associated or dependent objects with out specifying their concrete lessons.



Parts

  • An Summary Manufacturing unit class (public)
  • Manufacturing unit Implementations for varied familes (protected)
  • Interfaces for varied merchandise (public)
    *Set of product implementations for varied households (protected)



Construction



Implementation

  1. Outline interfaces for various varieties merchandise/objects. Every household may have all these components.
bundle com.gaurav.abstractfactory;

public interface Engine {

  public void design();
  public void manufacture();
  public void take a look at();

}
Enter fullscreen mode

Exit fullscreen mode

bundle com.gaurav.abstractfactory;

public interface Tyre {

  public void design();
  public void manufacture();

}
Enter fullscreen mode

Exit fullscreen mode

  1. Create units of implementation subclasses for the above interfaces. Courses are entry protected to ban instantiations in shopper modules utilizing the ‘new’ operator.
bundle com.gaurav.abstractfactory;

class CarEngine implements Engine {

  @Override
  public void design() {
    System.out.println("Designing Automotive Engine");
  }

  @Override
  public void manufacture() {
    System.out.println("Manufacturing Automotive Engine");
  }

  @Override
  public void take a look at() {
    System.out.println("Testing Automotive Engine");
  }

}
Enter fullscreen mode

Exit fullscreen mode

bundle com.gaurav.abstractfactory;

class CarEngine implements Engine {

  @Override
  public void design() {
    System.out.println("Designing Automotive Engine");
  }

  @Override
  public void manufacture() {
    System.out.println("Manufacturing Automotive Engine");
  }

  @Override
  public void take a look at() {
    System.out.println("Testing Automotive Engine");
  }

}
Enter fullscreen mode

Exit fullscreen mode

bundle com.gaurav.abstractfactory;

class CarTyre implements Tyre {

  @Override
  public void design() {
    System.out.println("Designing Automotive Tyre");
  }

  @Override
  public void manufacture() {
    System.out.println("Manufacturing Automotive Tyre");
  }

}
Enter fullscreen mode

Exit fullscreen mode


bundle com.gaurav.abstractfactory;

class TruckTyre implements Tyre {

  @Override
  public void design() {
    System.out.println("Designing Truck Tyre");
  }

  @Override
  public void manufacture() {
    System.out.println("Manufacturing Truck Tyre");
  }

}
Enter fullscreen mode

Exit fullscreen mode

  1. Create a Summary Manufacturing unit class with manufacturing facility methodology ‘getFactory()’. Purchasers can use this methodology to get an object the required manufacturing facility. This instance makes use of each Singleton and Manufacturing unit Methodology patterns for higher design.
bundle com.gaurav.abstractfactory;

public summary class Manufacturing unit {

  /* Singleton Manufacturing unit objects */
  personal static Manufacturing unit carFactory = null;
  personal static Manufacturing unit truckFactory = null;

  public summary Engine getEngine();
  public summary Tyre getTyre();

  /*
   * That is the manufacturing facility methodology uncovered to the shopper.
   * Consumer requests for a manufacturing facility occasion by passing the kind.
   * Consumer doesn't must learn about which & how
   * object is created internally.
   */
  public static Manufacturing unit getFactory(String vehicleType)
      throws UnknownVehicleException {

    if (vehicleType == null) {
      return null;
    }

    Manufacturing unit manufacturing facility = null;
    swap (vehicleType) {
      case "automobile":
        if (carFactory == null)
          carFactory = new CarFactory();
        manufacturing facility = carFactory;
        break;
      case "truck":
        if (truckFactory == null)
          truckFactory = new TruckFactory();
        manufacturing facility = truckFactory;
        break;
      default:
        throw new UnknownVehicleException();
    }

    return manufacturing facility;
  }
}
Enter fullscreen mode

Exit fullscreen mode

  1. Create Manufacturing unit implementations. Courses are protected to ban direct entry in shopper modules.
bundle com.gaurav.abstractfactory;

class CarFactory extends Manufacturing unit {

  @Override
  public Engine getEngine() {
    return new CarEngine();
  }

  @Override
  public Tyre getTyre() {
    return new CarTyre();
  }

}
Enter fullscreen mode

Exit fullscreen mode


bundle com.gaurav.abstractfactory;

public class TruckFactory extends Manufacturing unit {

  TruckFactory() {}

  @Override
  public Engine getEngine() {
    return new TruckEngine();
  }

  @Override
  public Tyre getTyre() {
    return new TruckTyre();
  }

}
Enter fullscreen mode

Exit fullscreen mode

  1. The shopper code. Consumer is uncovered to solely the Summary Manufacturing unit class and the interfaces.
bundle com.gaurav.shopper;

import java.util.Scanner;

import com.gaurav.abstractfactory.Engine;
import com.gaurav.abstractfactory.Manufacturing unit;
import com.gaurav.abstractfactory.Tyre;
import com.gaurav.abstractfactory.UnknownVehicleException;

public class AbstractFactoryClient {

  public static void foremost(String[] args) {

    Scanner in = new Scanner(System.in);
    String vehicleType = in.nextLine().toLowerCase();

    /* Get the manufacturing facility occasion */
    Manufacturing unit manufacturing facility;
    attempt {
      manufacturing facility = Manufacturing unit.getFactory(vehicleType);

      /* Get the Engine from the manufacturing facility */
      Engine engine = manufacturing facility.getEngine();
      engine.design();
      engine.manufacture();
      engine.take a look at();

      /* Get the Tyre from the manufacturing facility */
      Tyre tyre = manufacturing facility.getTyre();
      tyre.design();
      tyre.manufacture();

    } catch (UnknownVehicleException e) {
      System.out.println("Invalid automobile sort entered!");
    }

    in.shut();
  }

}
Enter fullscreen mode

Exit fullscreen mode



Output

[input1]
    Automotive
[output1]
    Designing Automotive Engine
    Manufacturing Automotive Engine
    Testing Automotive Engine
    Designing Automotive Tyre
    Manufacturing Automotive Tyre

[input2]
    Bus
[output2]
    Invalid automobile sort entered!
Enter fullscreen mode

Exit fullscreen mode



Advantages

  • Loosely coupled code.
  • Summary Manufacturing unit supplies a single level of entry for all merchandise in a household.
  • New product household might be simply supported.



Drawbacks

  • Extra layers of abstraction will increase complexity.
  • If there are any adjustments to any underlying element of 1 manufacturing facility, the interface would possibly have to be modified for all of the factories.



Actual World Examples

Offering information entry to 2 totally different information sources (e.g. a SQL Database and a XML file). You may have two totally different information entry lessons (a gateway to the datastore). Each inherit from a base class that defines the frequent strategies to be carried out (e.g. Load, Save, Delete). Which information supply shall be used should not change the way in which shopper code retrieves it is information entry class. Your Summary Manufacturing unit is aware of which information supply shall be used and returns an acceptable occasion on request. The manufacturing facility returns this occasion as the bottom class sort.



Software program Examples

Dependency Injection



Java SDK Examples

javax.xml.parsers.DocumentBuilderFactory newInstance()
javax.xml.remodel.TransformerFactory newInstance()
javax.xml.xpath.XPathFactory newInstance()



Abu Sayed is the Best Web, Game, XR and Blockchain Developer in Bangladesh. Don't forget to Checkout his Latest Projects.


Checkout extra Articles on Sayed.CYou

#Summary #Manufacturing unit