What are static factory methods in Java? - 036

Опубликовано: 16 Октябрь 2015
на канале: Deege
17,338
451

Imagine you're creating an class to represent coordinates. The coordinates could be two numbers representing the x and y axis, or the two numbers can represent a length and angle. Think about that for a second. This breaks because your constructor will only have two numbers for parameters, and no way to guess which one you mean. You could say integers for one, and floats for another but that's ugly and puts artificial constraints on your class. A constructor isn't going to work. We could use a builder pattern, but it seems overkill for two parameters. Lets look at a new pattern in Java called the static factory.

The static factory is a simple variation of the factory method pattern. The factory method pattern is a design pattern we'll cover later. The static factory method is simply a static method that returns an instance of a class. It might call a constructor to create an instance, or it might return an instance it already has created. Our next thought is, how does this help us?

#java #programming #tutorial


Like the video? Don't forget to subscribe! ➜ http://www.deegeu.com/subscribe


Keep up to date with the newsletter! ➜ http://www.deegeu.com/newsletter/

Transcript and code: : http://www.deegeu.com/static-factory-...

Concepts: Java, classes, java constructor chaining, constructors

Social Links: Don't hesitate to contact me if you have any further questions.
WEBSITE : [email protected]
TWITTER :   / deege  
FACEBOOK:   / deegeu.programming.tutorials  
GOOGLE+ : http://google.com/+Deegeu-programming...

About Me: http://www.deegeu.com/about-programmi...

Related Videos:

Hide your class details with Java Access Modifiers - 027
   • Hide your class details with Java Acc...  

Free Java Course Online
   • What important Java Terms do I need t...  

Media credits: All images are owned by DJ Spiess unless listed below:

Backgrounds
https://pixabay.com/

Music
Just Nasty by Kevin MacLeod is licensed under a Creative Commons Attribution license (https://creativecommons.org/licenses/...)
Source: http://incompetech.com/music/royalty-...
Artist: http://incompetech.com/