Skip to content

Instantly share code, notes, and snippets.

@SPONGE-JL
Last active July 18, 2021 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SPONGE-JL/e0035d7790afe00bdbb757479f0db392 to your computer and use it in GitHub Desktop.
Save SPONGE-JL/e0035d7790afe00bdbb757479f0db392 to your computer and use it in GitHub Desktop.
Static Factory Method Example (using Java)
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class ExampleToUseToyFactory {
public static void main( String[] args ) {
// PREVENTED TO USE `new` KEYWORD
// Toy toy = new Toy( "???" );
// Use STATIC FACTORY METHOD
Toy woody = Toy.Factory( "Woody_Pride", 34000, "main-product" );
Toy buzz = Toy.Factory( "Buzz_Lightyear", 28000, "main-product" );
Toy potato = Toy.Factory( "Mr._Potato", -999999 );
List<Toy> toyTray = new ArrayList<>();
toyTray.add( woody );
toyTray.add( buzz );
toyTray.add( potato );
String myFavoriteToyName = "Woody_Pride";
selectMyFavoriteToy( toyTray, myFavoriteToyName );
}
private static void selectMyFavoriteToy( List< Toy> toyTray, String myFavoriteToyName ) {
if ( Objects.isNull( myFavoriteToyName ) || myFavoriteToyName.isBlank() ) {
System.out.println( "Oops! Missing myFavoriteToyName..." );
return;
}
for ( Toy toyFromTray : toyTray ) {
toyFromTray.printToyInfo();
if ( myFavoriteToyName.equals( toyFromTray.toyName ) ) {
System.out.println( "I Love " + myFavoriteToyName + "!! :D" );
return;
}
}
System.out.println( "My Favorite Toy is not in this tray..." );
}
}
import java.util.Objects;
public class Toy {
/*
* --------------------------
* IMMUTABLE MEMBER VARIABLES
* --------------------------
*/
public final String toyName;
public final int toyPrice;
public final String toyDesc;
/*
* --------------------------------
* STATIC FACTORY METHOD
* > ALLOW TO ALLOCATE NEW INSTANCE
* --------------------------------
*/
public static Toy Factory( String toyName, int toyPrice, String toyDesc ) {
toyName = checkBeforeAllocateToyName(toyName);
toyPrice = checkBeforeAllocateToyPrice(toyPrice);
final Toy toy = isInvalidString( toyDesc )
? new Toy( toyName, toyPrice )
: new Toy( toyName, toyPrice, toyDesc );
return postStep(toy);
}
/*
* --------------------------------------------------------------------
* PRIVATE OVERLOADED CONSTRUCTORS
* > DENY TO ALLOCATE NEW INSTANCE WITH `new` KEYWORD FROM OUT OF CLASS
* --------------------------------------------------------------------
*/
private Toy( String toyName, int toyPrice ) {
this.toyName = toyName;
this.toyPrice = toyPrice;
this.toyDesc = "(none)";
}
private Toy( String toyName, int toyPrice, String toyDesc ) {
this.toyName = toyName;
this.toyPrice = toyPrice;
this.toyDesc = toyDesc;
}
/*
* ----------------------------------------------------
* PRIVATE METHOD FOR VALIDATION BEFORE ALLOCATE VALUES
* ----------------------------------------------------
*/
private String checkBeforeAllocateToyName( String toyName ) {
if ( isInvalidString( toyName ) ) {
System.out.println( "> WARN | Toy-Check | Invalid ToyName: " + toyName + " > set default: " + getDefaultToyName() );
return getDefaultToyName();
}
System.out.println( ">DEBUG | Toy-Check | ToyName: " + toyName );
return toyName;
}
private boolean isInvalidString( String str ) {
return Objects.isNull( str ) || toyName.isBlank();
}
private String getDefaultToyName() {
return "NO_NAMED";
}
private int checkBeforeAllocateToyPrice(int toyPrice) {
boolean isNeedToSetDefault = toyPrice <= 0;
if (isNeedToSetDefault) {
System.out.println( "> WARN | Toy-Check | Invalid ToyPrice: " + toyPrice + " > set default: " + getDefaultToyPrice() );
return getDefaultToyPrice();
}
System.out.println( ">DEBUG | Toy-Check | ToyPrice: " + toyPrice );
return toyPrice;
}
private int getDefaultToyPrice() {
return 25000;
}
private Toy postStep(Toy newToy) {
// Something to common post-step (after calling constructor)
newToy.printToyInfo();
return newToy;
}
/*
* ------------------------------------
* PUBLIC METHOD FOR ALLOCATED INSTANCE
* ------------------------------------
*/
public void printToyInfo() {
System.out.println( toString() );
}
@Override
public String toString() {
StringBuilder toyInfo = new StringBuilder();
toyInfo.append( "> " + toyName );
toyInfo.append( " (" + toyPrice + " KRW)" );
return toyInfo.toString();
}
}
@SPONGE-JL
Copy link
Author

SPONGE-JL commented Jul 17, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment