[JAVA-14174] Renamed paterns to paterns-module (#12718)

* [JAVA-14174] Renamed paterns to paterns-module

* [JAVA-14174] naming fixes

Co-authored-by: panagiotiskakos <[email protected]>
This commit is contained in:
panos-kakos
2022-09-19 12:14:14 +05:30
committed by GitHub
co-authored by panagiotiskakos
parent 74dcaa0935
commit 63a9bfbad9
518 changed files with 32 additions and 32 deletions
@@ -0,0 +1,10 @@
### Relevant Articles:
- [Singletons in Java](https://www.baeldung.com/java-singleton)
- [Introduction to Creational Design Patterns](https://www.baeldung.com/creational-design-patterns)
- [Abstract Factory Pattern in Java](https://www.baeldung.com/java-abstract-factory-pattern)
- [Flyweight Pattern in Java](https://www.baeldung.com/java-flyweight)
- [Double-Checked Locking with Singleton](https://www.baeldung.com/java-singleton-double-checked-locking)
- [Java Constructors vs Static Factory Methods](https://www.baeldung.com/java-constructors-vs-static-factory-methods)
- [Automatic Generation of the Builder Pattern with FreeBuilder](https://www.baeldung.com/java-builder-pattern-freebuilder)
- [How to Replace Many if Statements in Java](https://www.baeldung.com/java-replace-if-statements)
- [Prototype Pattern in Java](https://www.baeldung.com/java-pattern-prototype)
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>design-patterns-creational</artifactId>
<version>1.0</version>
<name>design-patterns-creational</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>patterns-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.inferred</groupId>
<artifactId>freebuilder</artifactId>
<version>${freebuilder.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>${javax.annotations.version}</version>
</dependency>
</dependencies>
<properties>
<freebuilder.version>2.4.1</freebuilder.version>
<javax.annotations.version>3.0.2</javax.annotations.version>
</properties>
</project>
@@ -0,0 +1,12 @@
package com.baeldung.constructorsstaticfactorymethods.application;
import com.baeldung.constructorsstaticfactorymethods.entities.User;
public class Application {
public static void main(String[] args) {
User user1 = User.createWithDefaultCountry("John", "[email protected]");
User user2 = User.createWithLoggedInstantiationTime("John", "[email protected]", "Argentina");
User user3 = User.getSingletonInstance("John", "[email protected]", "Argentina");
}
}
@@ -0,0 +1,56 @@
package com.baeldung.constructorsstaticfactorymethods.entities;
import java.time.LocalTime;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class User {
private static volatile User instance = null;
private static final Logger LOGGER = Logger.getLogger(User.class.getName());
private final String name;
private final String email;
private final String country;
public static User createWithDefaultCountry(String name, String email) {
return new User(name, email, "Argentina");
}
public static User createWithLoggedInstantiationTime(String name, String email, String country) {
LOGGER.log(Level.INFO, "Creating User instance at : {0}", LocalTime.now());
return new User(name, email, country);
}
public static User getSingletonInstance(String name, String email, String country) {
if (instance == null) {
synchronized (User.class) {
if (instance == null) {
instance = new User(name, email, country);
}
}
}
return instance;
}
private User(String name, String email, String country) {
this.name = name;
this.email = email;
this.country = country;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getCountry() {
return country;
}
}
@@ -0,0 +1,5 @@
package com.baeldung.creational.abstractfactory;
public interface AbstractFactory<T> {
T create(String type) ;
}
@@ -0,0 +1,18 @@
package com.baeldung.creational.abstractfactory;
public class AbstractPatternDriver {
public static void main(String[] args) {
AbstractFactory abstractFactory;
//creating a brown toy dog
abstractFactory = FactoryProvider.getFactory("Toy");
Animal toy =(Animal) abstractFactory.create("Dog");
abstractFactory = FactoryProvider.getFactory("Color");
Color color =(Color) abstractFactory.create("Brown");
String result = "A " + toy.getType() + " with " + color.getColor() + " color " + toy.makeSound();
System.out.println(result);
}
}
@@ -0,0 +1,6 @@
package com.baeldung.creational.abstractfactory;
public interface Animal {
String getType();
String makeSound();
}
@@ -0,0 +1,16 @@
package com.baeldung.creational.abstractfactory;
public class AnimalFactory implements AbstractFactory<Animal> {
@Override
public Animal create(String animalType) {
if ("Dog".equalsIgnoreCase(animalType)) {
return new Dog();
} else if ("Duck".equalsIgnoreCase(animalType)) {
return new Duck();
}
return null;
}
}
@@ -0,0 +1,10 @@
package com.baeldung.creational.abstractfactory;
public class Brown implements Color {
@Override
public String getColor() {
return "brown";
}
}
@@ -0,0 +1,5 @@
package com.baeldung.creational.abstractfactory;
public interface Color {
String getColor();
}
@@ -0,0 +1,16 @@
package com.baeldung.creational.abstractfactory;
public class ColorFactory implements AbstractFactory<Color> {
@Override
public Color create(String colorType) {
if ("Brown".equalsIgnoreCase(colorType)) {
return new Brown();
} else if ("White".equalsIgnoreCase(colorType)) {
return new White();
}
return null;
}
}
@@ -0,0 +1,15 @@
package com.baeldung.creational.abstractfactory;
public class Dog implements Animal {
@Override
public String getType() {
return "Dog";
}
@Override
public String makeSound() {
return "Barks";
}
}
@@ -0,0 +1,15 @@
package com.baeldung.creational.abstractfactory;
public class Duck implements Animal {
@Override
public String getType() {
return "Duck";
}
@Override
public String makeSound() {
return "Squeks";
}
}
@@ -0,0 +1,15 @@
package com.baeldung.creational.abstractfactory;
public class FactoryProvider {
public static AbstractFactory getFactory(String choice){
if("Toy".equalsIgnoreCase(choice)){
return new AnimalFactory();
}
else if("Color".equalsIgnoreCase(choice)){
return new ColorFactory();
}
return null;
}
}
@@ -0,0 +1,10 @@
package com.baeldung.creational.abstractfactory;
public class White implements Color {
@Override
public String getColor() {
return "White";
}
}
@@ -0,0 +1,64 @@
package com.baeldung.creational.builder;
public class BankAccount {
private String name;
private String accountNumber;
private String email;
private boolean newsletter;
//The constructor that takes a builder from which it will create object
//the access to this is only provided to builder
private BankAccount(BankAccountBuilder builder) {
this.name = builder.name;
this.accountNumber = builder.accountNumber;
this.email = builder.email;
this.newsletter = builder.newsletter;
}
public static class BankAccountBuilder {
private String name;
private String accountNumber;
private String email;
private boolean newsletter;
//All Mandatory parameters goes with this constructor
public BankAccountBuilder(String name, String accountNumber) {
this.name = name;
this.accountNumber = accountNumber;
}
//setters for optional parameters which returns this same builder
//to support fluent design
public BankAccountBuilder withEmail(String email) {
this.email = email;
return this;
}
public BankAccountBuilder wantNewsletter(boolean newsletter) {
this.newsletter = newsletter;
return this;
}
//the actual build method that prepares and returns a BankAccount object
public BankAccount build() {
return new BankAccount(this);
}
}
//getters
public String getName() {
return name;
}
public String getAccountNumber() {
return accountNumber;
}
public String getEmail() {
return email;
}
public boolean isNewsletter() {
return newsletter;
}
}
@@ -0,0 +1,16 @@
package com.baeldung.creational.builder;
public class BuilderPatternDriver {
public static void main(String[] args) {
BankAccount newAccount = new BankAccount
.BankAccountBuilder("Jon", "22738022275")
.withEmail("[email protected]")
.wantNewsletter(true)
.build();
System.out.println("Name: " + newAccount.getName());
System.out.println("AccountNumber:" + newAccount.getAccountNumber());
System.out.println("Email: " + newAccount.getEmail());
System.out.println("Want News letter?: " + newAccount.isNewsletter());
}
}
@@ -0,0 +1,16 @@
package com.baeldung.creational.factory;
public class FactoryDriver {
public static void main(String[] args) {
Polygon p;
PolygonFactory factory = new PolygonFactory();
//get the shape which has 4 sides
p = factory.getPolygon(4);
System.out.println("The shape with 4 sides is a " + p.getType());
//get the shape which has 4 sides
p = factory.getPolygon(8);
System.out.println("The shape with 8 sides is a " + p.getType());
}
}
@@ -0,0 +1,10 @@
package com.baeldung.creational.factory;
public class Heptagon implements Polygon {
@Override
public String getType() {
return "Heptagon";
}
}
@@ -0,0 +1,10 @@
package com.baeldung.creational.factory;
public class Octagon implements Polygon {
@Override
public String getType() {
return "Octagon";
}
}
@@ -0,0 +1,10 @@
package com.baeldung.creational.factory;
public class Pentagon implements Polygon {
@Override
public String getType() {
return "Pentagon";
}
}
@@ -0,0 +1,5 @@
package com.baeldung.creational.factory;
public interface Polygon {
String getType();
}
@@ -0,0 +1,22 @@
package com.baeldung.creational.factory;
public class PolygonFactory {
public Polygon getPolygon(int numberOfSides) {
if(numberOfSides == 3) {
return new Triangle();
}
if(numberOfSides == 4) {
return new Square();
}
if(numberOfSides == 5) {
return new Pentagon();
}
if(numberOfSides == 7) {
return new Heptagon();
}
else if(numberOfSides == 8) {
return new Octagon();
}
return null;
}
}
@@ -0,0 +1,10 @@
package com.baeldung.creational.factory;
public class Square implements Polygon {
@Override
public String getType() {
return "Square";
}
}
@@ -0,0 +1,10 @@
package com.baeldung.creational.factory;
public class Triangle implements Polygon {
@Override
public String getType() {
return "Triangle";
}
}
@@ -0,0 +1,13 @@
package com.baeldung.creational.singleton;
public class Singleton {
private Singleton() {}
private static class SingletonHolder {
public static final Singleton instance = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.instance;
}
}
@@ -0,0 +1,8 @@
package com.baeldung.creational.singleton;
public class SingletonDriver {
public static void main(String[] args) {
Singleton instance = Singleton.getInstance();
System.out.println(instance.toString());
}
}
@@ -0,0 +1,82 @@
package com.baeldung.flyweight;
import java.awt.Color;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Represents a car. This class is immutable.
*
* @author Donato Rimenti
*/
public class Car implements Vehicle {
/**
* Logger.
*/
private final static Logger LOG = LoggerFactory.getLogger(Car.class);
/**
* The car's engine.
*/
private Engine engine;
/**
* The car's color.
*/
private Color color;
/**
* Instantiates a new Car.
*
* @param engine
* the {@link #engine}
* @param color
* the {@link #color}
*/
public Car(Engine engine, Color color) {
this.engine = engine;
this.color = color;
// Building a new car is a very expensive operation!
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
LOG.error("Error while creating a new car", e);
}
}
/*
* (non-Javadoc)
*
* @see com.baeldung.flyweight.Vehicle#start()
*/
@Override
public void start() {
LOG.info("Car is starting!");
engine.start();
}
/*
* (non-Javadoc)
*
* @see com.baeldung.flyweight.Vehicle#stop()
*/
@Override
public void stop() {
LOG.info("Car is stopping!");
engine.stop();
}
/*
* (non-Javadoc)
*
* @see com.baeldung.flyweight.Vehicle#getColor()
*/
@Override
public Color getColor() {
return this.color;
}
}
@@ -0,0 +1,31 @@
package com.baeldung.flyweight;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Engine for a vehicle.
*
* @author Donato Rimenti
*/
public class Engine {
/**
* Logger.
*/
private final static Logger LOG = LoggerFactory.getLogger(Engine.class);
/**
* Starts the engine.
*/
public void start() {
LOG.info("Engine is starting!");
}
/**
* Stops the engine.
*/
public void stop() {
LOG.info("Engine is stopping!");
}
}
@@ -0,0 +1,29 @@
package com.baeldung.flyweight;
import java.awt.Color;
/**
* Interface for a vehicle.
*
* @author Donato Rimenti
*/
public interface Vehicle {
/**
* Starts the vehicle.
*/
public void start();
/**
* Stops the vehicle.
*/
public void stop();
/**
* Gets the color of the vehicle.
*
* @return the color of the vehicle
*/
public Color getColor();
}

Some files were not shown because too many files have changed in this diff Show More