In this article, we'll delve into a race among various animals using the Builder Design Pattern in Java. We'll walk through the specific steps to construct the 'Animal' classes, reuse the 'AnimalController' and 'TestAnimal' classes, and then analyze the advantages and disadvantages of the Builder Design Pattern.
Building the 'Animal' Class with Builder Design Pattern
The 'Animal' class represents each animal participating in the race. We'll employ the Builder Design Pattern to create 'Animal' objects with optional attributes such as name, speed, and flying ability.
public class Animal {
private String name;
private int speed;
private boolean isFly;
public Animal() {
}
public String getName() {
return name;
}
public int getSpeed() {
return speed;
}
public boolean isFly() {
return isFly;
}
public static class Builder {
private String name = null;
private int speed = 0;
private boolean isFly = false;
public Builder() {
}
public Builder setName(String name) {
this.name = name;
return this;
}
public Builder setSpeed(int speed) {
this.speed = speed;
return this;
}
public Builder setFly(boolean fly) {
isFly = fly;
return this;
}
public Animal build(){
Animal animal = new Animal();
animal.name = this.name;
animal.speed = this.speed;
animal.isFly = this.isFly;
return animal;
}
}
@Override
public String toString() {
return name + " {" +
"speed= " + speed +
", flyAble= " + isFly +
'}';
}
}
Reusing the 'AnimalController'
As mentioned in previous articles, we've already initialized these classes, so let's refer to them again.
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AnimalController {
public void winnerSpeedWithoutWings(List<Animal> animalList){
Map<Integer, String> mapAnimal = new HashMap<>();
for (Animal animal : animalList) {
mapAnimal.put(animal.getSpeed(), animal.getName());
}
int maxSpeed = Collections.max(mapAnimal.keySet());
for (Map.Entry<Integer, String> entry : mapAnimal.entrySet()) {
if(entry.getKey() == maxSpeed){
System.out.printf("Winner is %s, with the speed is %d", entry.getValue(), entry.getKey());
break;
}
}
}
}
The 'TestAnimal' Class
import java.security.SecureRandom;
import java.util.Arrays;
public class TestAnimal {
public static void main(String[] args) {
Animal horse = new Animal.Builder().setName("Horse").setSpeed(new SecureRandom().nextInt(100)).build();
System.out.println(horse.toString());
Animal dog = new Animal.Builder().setName("Dog").setSpeed(new SecureRandom().nextInt(50)).build();
System.out.println(dog.toString());
Animal eagle = new Animal.Builder().setName("Eagle").setSpeed(new SecureRandom().nextInt(100)).setFly(true).build();
System.out.println(eagle.toString());
Animal duck = new Animal.Builder().setName("Eagle").setSpeed(new SecureRandom().nextInt(50)).setFly(true).build();
System.out.println(duck.toString());
Animal lion = new Animal.Builder().setName("Dog").setSpeed(new SecureRandom().nextInt(100)).build();
System.out.println(lion.toString());
AnimalController animalController = new AnimalController();
animalController.winnerSpeedWithoutWings(Arrays.asList(horse, dog, eagle, duck, lion));
}
}
Conclusion
Advantages:
Flexibility and Scalability: The Builder Pattern allows us to create complex objects with optional attributes in a flexible and easily scalable manner.
Readability and Usability: The structure of the Builder Pattern makes the source code more readable and user-friendly, especially when dealing with multiple optional attributes.
Disadvantages:
Requires More Work: Implementing the Builder Pattern entails creating a separate Builder class for each class that needs to be constructed, which can introduce complexity into the source code.
Increased Number of Classes: Using the Builder Pattern may lead to an increase in the number of classes in the project, potentially adding to the workload and source code management.
Here's how we utilize the Builder Design Pattern to organize a race among different animal species. Despite certain drawbacks, the Builder Pattern remains a powerful tool for creating complex objects in Java programming.
Comments