What Is Manufacturing Flora Method Blueprint Pattern Inwards Coffee Alongside Representative - Tutorial

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

 

What is static mill method or mill pattern 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 alter it after when y'all alter how object gets created or y'all tin terminate fifty-fifty innovate novel object amongst only alter inwards i class. In GOF pattern listing Factory pattern is listed equally Creation pattern pattern. Factory should live on an interface as well as clients showtime either creates mill or teach mill which after used to create objects.



Example of static mill method inwards JDK

 Best Example of Factory method pattern pattern is valueOf() method which is at that spot 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 mill method pattern pattern from JDK is :

valueOf() method which returns object created yesteryear mill 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 mill method every fourth dimension called.

getType() as well as newType() equivalent of getInstance() as well as newInstance() mill method but used when mill method resides inwards split upward class.

Problem which is solved yesteryear Factory method Pattern inwards Java

Whenever nosotros beak most object oriented language it volition based upon only about concept similar abstraction, polymorphism etc as well as on that encapsulation and delegation are of import concept whatever pattern volition live on called skilful if chore are delegated to unlike object as well as only about sort of encapsulation is there.

Some fourth dimension our application or framework volition non know that what sort of object it has to create at run-time it knows alone the interface or abstract cast as well as equally nosotros know nosotros tin terminate non create object of interface or abstract cast as well as thence primary 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 effect inwards inflexible code as well as hard to alter inwards maintenance. By using Factory pattern pattern inwards Java nosotros teach rid of this problem.

Another occupation nosotros tin terminate aspect upward is cast needs to incorporate objects of other classes or cast hierarchies within it; this tin terminate live on really easily achieved yesteryear only using the novel keyword as well as the cast constructor. The occupation amongst this approach is that it is a really hard coded approach to create objects equally this creates dependency betwixt the ii classes.

So factory pattern solve this occupation really easily yesteryear model an interface for creating an object which at creation fourth dimension tin terminate permit its subclasses create upward one's hear which cast to instantiate, Factory Pattern promotes loose coupling yesteryear eliminating the require to bind application-specific classes into the code. The factory methods are typically implemented equally virtual methods, as well as thence this pattern is too referred to equally the “Virtual Constructor”. These methods create the objects of the products or target classes.

When to role Factory pattern 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 yesteryear applications using the framework.        
  • Some or all concrete products tin terminate live on created inwards multiple ways, or nosotros desire to teach out opened upward the pick that inwards the hereafter at that spot may live on novel ways to create the concrete product.
  • Factory method is used when Products don't require to know how they are created.
  • We  tin terminate role mill 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 into an illustration of how mill pattern is implemented inwards Code.We convey requirement to create multiple currency e.g. INR, SGD, USD as well as code should live on extensible to adjust novel Currency equally well. Here nosotros convey made Currency equally interface as well as all currency would live on concrete implementation of Currency interface. Factory Class volition create Currency based upon province as well as render concrete implementation which volition live on stored inwards interface type. This makes code dynamic as well as 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 cast 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 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 upward beginning library as well as other frameworks.In next are primary advantages of using Factory pattern inwards Java:

1) Factory method pattern pattern decouples the calling cast from the target class, which effect inwards less coupled as well as highly cohesive code?
E.g.: JDBC is a skilful illustration for this pattern; application code doesn't require to know what database it volition live on used with, as well as thence it doesn't know what database-specific driver classes it should use. Instead, it uses mill methods to teach Connections, Statements, as well as 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 as well as 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 mill is to a greater extent than flexible than creating an object straight inwards the client. Since customer is working on interface score whatever fourth dimension y'all tin terminate heighten the implementation as well as render from Factory.

3) Another do goodness of using Factory pattern 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 pattern pattern inwards Java is too 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 mill method pattern pattern is:
1. Static mill method used inwards mill pattern pattern enforces role of Interface than implementation which itself a skilful practice. for example:

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

2. Since static mill method convey render type equally Interface, it allows y'all to supercede implementation amongst meliorate functioning version inwards newer release.
3. Another wages of static mill method pattern is that they tin terminate cache oft used object as well as eliminate duplicate object creation. Boolean.valueOf() method is skilful illustration which caches truthful as well as fake boolean value.
4. Factory method pattern is too recommended yesteryear Joshua Bloch inwards Effective Java.
5 Factory method pattern offers choice agency of creating object.
6. Factory pattern tin terminate too live on used to enshroud information related to creation of object.

That’s all on Factory pattern patten inwards Java for now. This is i of the most used patterns inwards Java library as well as unlike Java frameworks. Summary is attempt to role Factory pattern whenever y'all run into an chance to encapsulate object creation code as well as run into endangerment of creating unlike 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

Belum ada Komentar untuk "What Is Manufacturing Flora Method Blueprint Pattern Inwards Coffee Alongside Representative - Tutorial"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel