What Is Manufacturing Flora Method Blueprint Pattern Inwards Coffee Amongst Illustration - Tutorial

Factory blueprint pattern inwards Java i of the centre blueprint pattern which is used heavily non entirely inwards JDK but besides inwards diverse Open Source framework such every bit Spring, Struts in addition to Apache along amongst decorator blueprint pattern inwards Java. Factory Design pattern is based on Encapsulation object oriented concept. Factory method is used to create dissimilar object from manufacturing flora often refereed every bit Item in addition to it encapsulate the creation code. So instead of having object creation code on customer side nosotros encapsulate within Factory method inwards Java. One of the best examples of manufacturing flora pattern inwards Java is BorderFactory Class of Swing API. In this Design pattern tutorial nosotros volition encounter What is Factory method blueprint pattern inwards Java, What are principal advantages of manufacturing flora pattern inwards Java , Code illustration of Factory blueprint pattern in addition to What work Factory pattern solves inwards Java or when to role Factory blueprint pattern.  This article is inwards continuation of my blueprint pattern article every bit 10 OOPS in addition to SOLID blueprint principles coffee programmer should know and How to role Observer pattern inwards Java

 

What is static manufacturing flora method or manufacturing flora blueprint pattern

Class inwards Java in addition to it provides loose coupling in addition to high cohesion. Factory pattern encapsulate object creation logic which makes it tardily to alter it later on when y'all alter how object gets created or y'all tin fifty-fifty innovate novel object amongst simply alter inwards i class. In GOF pattern listing Factory pattern is listed every bit Creation blueprint pattern. Factory should live on an interface in addition to clients get-go either creates manufacturing flora or larn manufacturing flora which later on used to create objects.



Example of static manufacturing flora method inwards JDK

 Best Example of Factory method blueprint pattern is valueOf() method which is at that topographic point inwards String in addition to wrapper classes similar Integer in addition to Boolean in addition to used for type conversion i.e. from converting String to Integer or String to double inwards java..

Some to a greater extent than examples of manufacturing flora method blueprint pattern from JDK is :

valueOf() method which returns object created past times manufacturing flora equivalent to value of parameter passed.

getInstance() method which creates instance of Singleton class.

newInstance() method which is used to create in addition to render novel instance from manufacturing flora method every fourth dimension called.

getType() in addition to newType() equivalent of getInstance() in addition to newInstance() manufacturing flora method but used when manufacturing flora method resides inwards carve upwards class.

Problem which is solved past times Factory method Pattern inwards Java

Whenever nosotros utter most object oriented language it volition based upon to a greater extent than or less concept similar abstraction, polymorphism etc in addition to on that encapsulation and delegation are of import concept whatever blueprint volition live on called practiced if line of piece of work are delegated to dissimilar object in addition to to a greater extent than or less form of encapsulation is there.

Some fourth dimension our application or framework volition non know that what form of object it has to create at run-time it knows entirely the interface or abstract class in addition to every bit nosotros know nosotros tin non create object of interface or abstract class in addition to hence principal work is frame operate knows when it has to create but don’t know what kind of object.

Whenever nosotros create object using new() nosotros violate principle of programming for interface rather than implementation which eventually upshot inwards inflexible code in addition to hard to alter inwards maintenance. By using Factory blueprint pattern inwards Java nosotros larn rid of this problem.

Another work nosotros tin confront is class needs to comprise objects of other classes or class hierarchies within it; this tin live on really easily achieved past times simply using the novel keyword in addition to the class constructor. The work amongst this approach is that it is a really hard coded approach to create objects every bit this creates dependency betwixt the 2 classes.

So factory pattern solve this work really easily past times model an interface for creating an object which at creation fourth dimension tin permit its subclasses determine which class to instantiate, Factory Pattern promotes loose coupling past times eliminating the withdraw to bind application-specific classes into the code. The factory methods are typically implemented every bit virtual methods, in addition to hence this pattern is besides referred to every bit the “Virtual Constructor”. These methods create the objects of the products or target classes.

When to role Factory blueprint pattern inwards Java

  • Static Factory methods are mutual inwards frameworks where library code needs to create objects of types which may live on sub classed past times applications using the framework.        
  • Some or all concrete products tin live on created inwards multiple ways, or nosotros desire to larn out opened upwards the selection that inwards the hereafter at that topographic point may live on novel ways to create the concrete product.
  • Factory method is used when Products don't withdraw to know how they are created.
  • We  tin role manufacturing flora pattern where nosotros bring to create an object of whatever i of sub-classes depending on the information provided

Code Example of Factory Design Pattern inwards Java:

Let’s encounter an illustration of how manufacturing flora pattern is implemented inwards Code.We bring requirement to create multiple currency e.g. INR, SGD, USD in addition to code should live on extensible to adapt novel Currency every bit well. Here nosotros bring made Currency every bit interface in addition to all currency would live on concrete implementation of Currency interface. Factory Class volition create Currency based upon dry soil in addition to render concrete implementation which volition live on stored inwards interface type. This makes code dynamic in addition to extensible.

Here is consummate code illustration of Factory pattern inwards Java:

interface Currency {
       String getSymbol();
}
// Concrete Rupee Class code
class Rupee implements Currency {
       @Override
       public String getSymbol() {
              return "Rs";
       }
}

// Concrete SGD class Code
class SGDDollar implements Currency {
       @Override
       public String getSymbol() {
              return "SGD";
       }
}

// Concrete the States Dollar code
class USDollar implements Currency {
       @Override
       public String getSymbol() {
              return "USD";
       }
}

// Factroy Class code
class CurrencyFactory {

       public static Currency createCurrency (String country) {
       if (country. equalsIgnoreCase ("India")){
              return new Rupee();
       }else if(country. equalsIgnoreCase ("Singapore")){
              return new SGDDollar();
       }else if(country. equalsIgnoreCase ("US")){
              return new USDollar();
        }
       throw new IllegalArgumentException("No such currency");
       }
}

// Factory customer code
public class Factory {
       public static void main(String args[]) {
              String dry soil = args[0];
              Currency rupee = CurrencyFactory.createCurrency(country);
              System.out.println(rupee.getSymbol());
       }
}


Advantage of Factory method Pattern inwards Java:

Factory pattern inwards Java is heavily used everywhere including JDK, opened upwards origin library in addition to other frameworks.In next are principal advantages of using Factory pattern inwards Java:

1) Factory method blueprint pattern decouples the calling class from the target class, which upshot inwards less coupled in addition to highly cohesive code?
E.g.: JDBC is a practiced illustration for this pattern; application code doesn't withdraw to know what database it volition live on used with, in addition to hence it doesn't know what database-specific driver classes it should use. Instead, it uses manufacturing flora methods to larn Connections, Statements, in addition to other objects to operate with. Which gives y'all flexibility to alter your back-end database without changing your DAO layer inwards instance y'all are using ANSI SQL features in addition to non coded on DBMS specific feature?

2) Factory pattern inwards Java enables the subclasses to supply extended version of an object, because creating an object within manufacturing flora is to a greater extent than flexible than creating an object straight inwards the client. Since customer is working on interface degree whatever fourth dimension y'all tin lift the implementation in addition to render from Factory.

3) Another do goodness of using Factory blueprint pattern inwards Java is that it encourages consistency inwards Code since every fourth dimension object is created using Factory rather than using dissimilar constructor at dissimilar customer side.

4) Code written using Factory blueprint pattern inwards Java is besides easy to debug and troubleshoot because y'all bring a centralized method for object creation in addition to every customer is getting object from same place.


Some to a greater extent than advantages of manufacturing flora method blueprint pattern is:
1. Static manufacturing flora method used inwards manufacturing flora blueprint pattern enforces role of Interface than implementation which itself a practiced practice. for example:

Map synchronizedMap = Collections.synchronizedMap(new HashMap());

2. Since static manufacturing flora method bring render type every bit Interface, it allows y'all to supervene upon implementation amongst amend functioning version inwards newer release.
3. Another payoff of static manufacturing flora method pattern is that they tin cache ofttimes used object in addition to eliminate duplicate object creation. Boolean.valueOf() method is practiced illustration which caches truthful in addition to faux boolean value.
4. Factory method pattern is besides recommended past times Joshua Bloch inwards Effective Java.
5 Factory method pattern offers choice agency of creating object.
6. Factory pattern tin besides live on used to shroud information related to creation of object.

That’s all on Factory blueprint patten inwards Java for now. This is i of the most used patterns inwards Java library in addition to dissimilar Java frameworks. Summary is elbow grease to role Factory pattern whenever y'all encounter an chance to encapsulate object creation code in addition to encounter adventure of creating dissimilar object inwards close future.

Further Learning
Design Pattern Library
From 0 to 1: Design Patterns - 24 That Matter - In Java
Java Design Patterns - The Complete Masterclass


Sumber https://javarevisited.blogspot.com/

0 Response to "What Is Manufacturing Flora Method Blueprint Pattern Inwards Coffee Amongst Illustration - Tutorial"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel