Design patterns to enjoy with frequently used Java libraries --Factory pattern

Design patterns are also hidden in the Java libraries that you use most often. It's easy to overlook the busy daily work, but once in a while, let's take a closer look at the beautiful design, which can be said to be a kind of art.

This art


URL url = new URL("http:String starting with");
URLConnection connection = url.openConnection();

It is a scene that creates an object that connects to the URL specified by the argument, but when I watch it again, I am fascinated by the mysterious interface.

Points of appreciation

URLConnection To create an instance, use new URLConnection (url); Instead, it goes through the URL class. However, I still find the beauty of the simplicity that does not make the programmer feel uncomfortable. Let's explore the feelings of the designer.

When not using the Factory pattern

Let's first create an object to connect with HTTP with the most intuitive code.

//Caution:You can't actually write it like this because the constructor is protected
URLConnection connection = new HttpURLConnection(url);

If you only assume HTTP as the connection method, this method should be fine. However, in the future, if it becomes necessary to connect using the XXX protocol other than HTTP, it will be necessary to be aware of either HttpURLConnection or XXXURLConnection **.

URLConnection connection = new HttpURLConnection(url);


URLConnection connection = new XXXURLConnection(url);

Multiple types of instance generation code will be written in various parts of the project.

Also, if you decide to unify the classes you use from HttpURLConnection to XXXURLConnection, you will have to ** fix all applicable code **. This is not beautiful.

In fact, there is also a subclass of ʻURLConnection called JarURLConnection`. (This class expects to access a local jar file [^ 1].)

When using the Factory pattern

The code at the beginning uses the Factory pattern.

If you set the constructor of ʻURL to a string that starts with http :, ʻurl.openConnection (); returns an instance of the HttpURLConnection class.

URL url = new URL("http:String starting with"); //Factory class
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); //Can be cast to HttpURLConnection

And if you set a string that starts with jar :, an instance of the JarURLConnection class will be returned.

URL url = new URL("jar:String starting with"); //Factory class
JarURLConnection connection = (JarURLConnection)url.openConnection(); //Can be cast to JarURLConnection

With the Factory pattern, you can ** let the Factory class decide which class to instantiate. In addition, ** new instance code is hidden **, so you can change the internal implementation while keeping the same interface. How elegant!

Expert comments on the Factory pattern

Many experts have also commented on the Factory pattern.

Yoshihara Hidehiko

The user of the object simply asks the factory to create it, and does not need to be aware of the object creation procedure or type, and can get the desired object in a usable state. If the type of class you generate or the procedure for creating it changes, you only have to rework the factory.

From Reverse Lookup Design Patterns that Monkeys Can Understand

Alan Shalloway / James R. Trott

There are general rules to follow when considering the creation and management of objects. It says, "An object can only create and / or manage other objects, or use other objects, and not both."

["Object-oriented mind to learn with design patterns"]( % E3% 83% 91% E3% 82% BF% E3% 83% BC% E3% 83% B3% E3% 81% A8% E3% 81% A8% E3% 82% 82% E3% 81% AB% E5 % AD% A6% E3% 81% B6% E3% 82% AA% E3% 83% 96% E3% 82% B8% E3% 82% A7% E3% 82% AF% E3% 83% 88% E6% 8C % 87% E5% 90% 91% E3% 81% AE% E3% 81% 93% E3% 81% 93% E3% 82% 8D-Software-patterns-% E3% 82% A2% E3% 83% A9% From E3% 83% B3-% E3% 82% B7% E3% 83% A3% E3% 83% AD% E3% 82% A6% E3% 82% A7% E3% 82% A4 / dp / 4894716844 /)

Difference from GoF Factory Method pattern

The GoF design pattern introduces the Factory Method pattern instead of the Factory pattern. In the Factory Method pattern, the Factory class (URL class in this case) becomes an abstract class, and its concrete class XXXFactory class (HttpURL / JarURL class [^ 2] in this case) corresponds to the corresponding instance (in this case). Will create HttpURLConnection / JarURLConnection).

If there is a high possibility that the number of derived classes of ʻURLConnection such as XXXURLConnection will increase in the future, I think it is better to make ʻURL an abstract class so that a Factory class such as XXXURL can be created. I will. However, the designer probably thought it was unlikely. The current design is simpler and easier for users to use.


It's the real pleasure of programmers to be able to enjoy intellectual enjoyment by just looking at a few lines of code without having to go to the museum.

If you are an engineer who sympathizes with the artistry of the Factory pattern, please contact our recruiting staff (Qualysite Technologies Inc.)!

Related article

Create an instance

--Design patterns to enjoy with frequently used Java libraries --Factory patterns -Design patterns to enjoy with frequently used Java libraries --Builder patterns -Design patterns to enjoy with frequently used Java libraries --Abstract Factory pattern

Simplify the interface

-Design patterns to enjoy with Java libraries that are often used --Facade patterns -Design pattern to enjoy with Java library used frequently --Adapter pattern

Leave it to another class

-Design patterns to enjoy with frequently used Java libraries --Template Method patterns -Design patterns to enjoy with frequently used Java libraries --Strategy patterns

Reference URL

-How to get the Jar containing the class from the class in Java -Jar file memo (Hishidama's java-archive Memo)

[^ 1]: See the reference URL for an example of how to use the JarURLConnection class. [^ 2]: It doesn't actually exist.

Recommended Posts

Design patterns to enjoy with frequently used Java libraries --Factory pattern
Rethinking design patterns with Java8 lambda expressions & Stream --Builder pattern -
Introduction to Design Patterns (Abstract Factory)
Java beginner design pattern (Factory Method pattern)
Java design pattern
Java Design Patterns
Design pattern ~ Abstract Factory ~
Frequently used Java generics
Introduction to design patterns (introduction)
I used to make nc (netcat) with JAVA normally
Java design pattern summary
[Java] Change the process according to the situation with the Strategy pattern
[Personal memo] Frequently used Java grammar updated from time to time
Introduction to Design Patterns (Builder)
Java to play with Function
Introduction to Design Patterns (Composite)
[Design pattern] Java core library
Introduction to design patterns (Flyweight)
Connect to DB with Java
Connect to MySQL 8 with Java
Introduction to design patterns Prototype
Introduction to Design Patterns (Iterator)
[Java] Summary of design patterns
Introduction to Design Patterns (Strategy)
Design patterns to try with Swift-Iterator patterns that support Arrays and Dictionary
Learning Ruby with AtCoder 11 How to receive frequently used standard input
[Java] Points to note with Arrays.asList ()
Dare to challenge Kaggle with Java (1)
I tried to interact with Java
Java, arrays to start with beginners
Introduction to Docker (1) Frequently used commands
Important design patterns to improve maintainability