What Is Mill Method Pattern Pattern Inward Coffee Amongst Illustration - Tutorial

Advertisement

Masukkan script iklan 970x90px

What Is Mill Method Pattern Pattern Inward Coffee Amongst Illustration - Tutorial

Jumat, 26 Juni 2020

Factory blueprint pattern inwards Java i of the substance blueprint pattern which is used heavily non exclusively inwards JDK but equally good inwards diverse Open Source framework such equally Spring, Struts as well as Apache along amongst decorator blueprint pattern inwards Java. Factory Design pattern is based on Encapsulation object oriented concept. Factory method is used to create unlike object from manufactory often refereed equally Item as well as 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 manufactory pattern inwards Java is BorderFactory Class of Swing API. In this Design pattern tutorial nosotros volition run across What is Factory method blueprint pattern inwards Java, What are nous advantages of manufactory pattern inwards Java , Code representative of Factory blueprint pattern as well as What occupation Factory pattern solves inwards Java or when to role Factory blueprint pattern.  This article is inwards continuation of my blueprint pattern article equally 10 OOPS as well as SOLID blueprint principles coffee programmer should know and How to role Observer pattern inwards Java

 

What is static manufactory method or manufactory blueprint pattern

Class inwards Java as well as it provides loose coupling as well as high cohesion. Factory pattern encapsulate object creation logic which makes it slow to modify it afterward when y'all modify how object gets created or y'all tin fifty-fifty innovate novel object amongst merely modify inwards i class. In GOF pattern listing Factory pattern is listed equally Creation blueprint pattern. Factory should last an interface as well as clients starting fourth dimension either creates manufactory or larn manufactory which afterward used to create objects.



Example of static manufactory method inwards JDK

 Best Example of Factory method blueprint pattern is valueOf() method which is in that location inwards String as well as wrapper classes similar Integer as well as Boolean as well as 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 manufactory method blueprint pattern from JDK is :

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

getInstance() method which creates instance of Singleton class.

newInstance() method which is used to create as well as render novel instance from manufactory method every fourth dimension called.

getType() as well as newType() equivalent of getInstance() as well as newInstance() manufactory method but used when manufactory method resides inwards dissever class.

Problem which is solved past times Factory method Pattern inwards Java

Whenever nosotros verbalize virtually object oriented language it volition based upon to a greater extent than or less concept similar abstraction, polymorphism etc as well as on that encapsulation and delegation are of import concept whatever blueprint volition last called skillful if delineate are delegated to unlike object as well as 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 exclusively the interface or abstract degree as well as equally nosotros know nosotros tin non create object of interface or abstract degree as well as hence nous occupation 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 resultant inwards inflexible code as well as hard to modify inwards maintenance. By using Factory blueprint pattern inwards Java nosotros larn rid of this problem.

Another occupation nosotros tin confront is degree needs to incorporate objects of other classes or degree hierarchies within it; this tin last rattling easily achieved past times merely using the novel keyword as well as the degree constructor. The occupation amongst this approach is that it is a rattling hard coded approach to create objects equally this creates dependency betwixt the ii classes.

So factory pattern solve this occupation rattling easily past times model an interface for creating an object which at creation fourth dimension tin allow its subclasses create upwards one's take away heed which degree to instantiate, Factory Pattern promotes loose coupling past times eliminating the require to bind application-specific classes into the code. The factory methods are typically implemented equally virtual methods, as well as hence this pattern is equally good referred to equally 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 last sub classed past times applications using the framework.        
  • Some or all concrete products tin last created inwards multiple ways, or nosotros desire to larn out opened upwards the selection that inwards the futurity in that location may last novel ways to create the concrete product.
  • Factory method is used when Products don't require to know how they are created.
  • We  tin role manufactory pattern where nosotros convey 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 run across an representative of how manufactory pattern is implemented inwards Code.We convey requirement to create multiple currency e.g. INR, SGD, USD as well as code should last extensible to adapt novel Currency equally well. Here nosotros convey made Currency equally interface as well as all currency would last concrete implementation of Currency interface. Factory Class volition create Currency based upon province as well as render concrete implementation which volition last stored inwards interface type. This makes code dynamic as well as extensible.

Here is consummate code representative 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 degree Code
class SGDDollar implements Currency {
       @Override
       public String getSymbol() {
              return "SGD";
       }
}

// Concrete U.S. of A. of America 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 province = 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 rootage library as well as other frameworks.In next are nous advantages of using Factory pattern inwards Java:

1) Factory method blueprint pattern decouples the calling degree from the target class, which resultant inwards less coupled as well as highly cohesive code?
E.g.: JDBC is a skillful representative for this pattern; application code doesn't require to know what database it volition last used with, as well as hence it doesn't know what database-specific driver classes it should use. Instead, it uses manufactory methods to larn Connections, Statements, as well as other objects to operate with. Which gives y'all flexibility to modify your back-end database without changing your DAO layer inwards representative y'all are using ANSI SQL features as well as non coded on DBMS specific feature?

2) Factory pattern inwards Java enables the subclasses to furnish extended version of an object, because creating an object within manufactory is to a greater extent than flexible than creating an object direct inwards the client. Since customer is working on interface bird whatever fourth dimension y'all tin get upwards the implementation as well as 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 unlike constructor at unlike customer side.

4) Code written using Factory blueprint pattern inwards Java is equally good easy to debug and troubleshoot because y'all convey a centralized method for object creation as well as every customer is getting object from same place.


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

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

2. Since static manufactory method convey render type equally Interface, it allows y'all to supervene upon implementation amongst meliorate functioning version inwards newer release.
3. Another wages of static manufactory method pattern is that they tin cache often used object as well as eliminate duplicate object creation. Boolean.valueOf() method is skillful representative which caches truthful as well as imitation boolean value.
4. Factory method pattern is equally good recommended past times Joshua Bloch inwards Effective Java.
5 Factory method pattern offers option agency of creating object.
6. Factory pattern tin equally good last 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 as well as unlike Java frameworks. Summary is travail to role Factory pattern whenever y'all run across an chance to encapsulate object creation code as well as run across conduct chances of creating unlike object inwards nigh future.

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