Post

Factory

  • Loose coupling
  • OC principle
  • default in interface

๐Ÿ› ๏ธ When to use factory

  • When you want to create several types of smth with different values
  • give the creation role to a factory
  • ๐Ÿ‘€ You have class Circle and Square extending Interface Shape
  • you want to create Circle or Square using one constructor
  • ๐Ÿ‘€ You have class WhiteShip and BlackShip extending Ship
  • you want to create WhiteShip and BlackShip just with one method, without using else-if for checking the names and settings colorsโ€ฆ

โœ… Factory Pattern

  • factory์•ˆ์—์„œ ๊ตฌ์ฒด์ ์ธ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค
  • โญ๏ธ Loosely coupled: make coupling between class and constructor loose
  • <<Interface>> will be responsible for constructing a class
  • delegate object creation to a factory
  • โญ๏ธ Open/Closed principle: open to extension, closed to modification

๐Ÿ‘Ž๐Ÿป If there was no factory pattern

  • โœ”๏ธ Class Ship
1
2
3
4
5
6
7
8
public class Ship {
    private String name;
    private String color;
    private String logo;

  //getters
  //setters
}
  • โœ”๏ธ Class ShipFactory
  • ๐Ÿ‘Ž๐Ÿป need to check name and set logo and color accordingly
1
2
3
4
5
6
7
8
9
10
11
12
13
        // Customizing for specific name
        if (name.equalsIgnoreCase("whiteship")) {
            ship.setLogo("\uD83D\uDEE5๏ธ");
        } else if (name.equalsIgnoreCase("blackship")) {
            ship.setLogo("โš“");
        }

        // coloring
        if (name.equalsIgnoreCase("whiteship")) {
            ship.setColor("whiteship");
        } else if (name.equalsIgnoreCase("blackship")) {
            ship.setColor("black");
        }

โœ… After factory pattern

Screenshot-2026-02-19-at-15-45-43.png

  • โœ”๏ธ Class Ship
1
2
3
4
5
6
7
8
public class Ship {
    private String name;
    private String color;
    private String logo;

  //getters
  //setters
}
  • โœ”๏ธ Class WhiteShip and BlackShip
  • โญ๏ธ How to set attributes of parent without using constructor
1
2
3
4
5
6
7
public class WhiteShip extends Ship {
    public WhiteShip() {
        setName("White Ship");
        setLogo("aaa");
        setColor("white");
    }
}
1
2
3
4
5
6
7
public class BlackShip extends Ship{
    public BlackShip() {
        setName("BlackShip");
        setLogo("bbbb");
        setColor("black");
    }
}
  • โœ”๏ธ Interface ShipFactory
1
2
3
4
5
6
7
8
9
10
11
12
public interface ShipFactory {
  //default, could/could not be overrided
    default Ship orderShip(String name, String email){
        validate(name, email);
        Ship ship = createShip(); //use createShip();
        sendEmailTo(email, ship);
        return ship;
    }

    Ship createShip(); //not defined, must be overrided

}
  • validate(), sendEmailTo() are methods I want to run as soon as I create Ship

  • โœ”๏ธ Class WhiteShipFactory and BlackShipFactory

1
2
3
4
5
6
public class WhiteShipFactory implements ShipFactory{
    @Override
    public Ship createShip() {
        return new WhiteShip();
    }
}
1
2
3
4
5
6
public class BlackShipFactory implements ShipFactory{
    @Override
    public Ship createShip() {
        return new BlackShip();
    }
}
  • โœ”๏ธ CLient code
1
2
3
4
5
6
public class Client {
    public static void main(String[] args) {
        Ship whiteShip = new WhiteShipFactory().orderShip("WhiteShip", "aaa@gmail.com");
        Ship blackShip = new BlackShipFactory().orderShip("BlackShip", "bbb@gmail.com");
    }
}

๐Ÿ‘๐Ÿป Advantages ๐Ÿ‘Ž๐Ÿป Disadvantages

  • ๐Ÿ‘๐Ÿป do not need to use else-if
  • ๐Ÿ‘๐Ÿป open/closed principle: open for extension, closed for modification
    • can create another function/interface without modifying the old code
    • this is bc of coupling between class and constructor is loose
  • ๐Ÿ‘๐Ÿป can use default to write a default method in interface

  • ๐Ÿ‘Ž๐Ÿป need to create more classes

default and private in interface

  • can use default to define a method in interface
  • and the helper methods for the default method should be private
  • as it would be used only inside the interface
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public interface ShipFactory {
    default Ship orderShip(String name, String email){
        validate(name, email);
        prepareFor(name);
        Ship ship = createShip();
        sendEmailTo(email, ship);
        return ship;
    }

  //helper methods inside default method
  //access modifier: private
  private void validate(String name, String email){

    }

    private void prepareFor(String name) {

    }


This post is licensed under CC BY 4.0 by the author.