Post

Creational_Abstract Factory

βœ… Abstract Factory

  • similar to factory,
  • but with a focus on the client
  • add client to factory pattern

  • interface creates the related classes instance
  • πŸ‘πŸ» can hide which concrete product to the client

βœ… Diagram

Abstract-factory-UML.jpg

πŸ‘€ ShipFactory example

Screenshot-2026-02-28-at-09-08-34.png

Factory πŸ†š AbstractFactory

  • 곡톡점: make creation of instance abstract with instance
  • Factory: hide the creation of instance in concrete factory
  • focus more on how to implement factory, interitance
  • move instance creation process to concrte factory class
  • πŸ‘‰πŸ» defines an interface for creating an object, but lets subclasses (or a factory class) decide which class to instantiate
  • πŸ†š create one product

  • AbstractFactory: client does not have to see the concrete factory
  • focus more on how to use factory, composition
  • create related instances without relying on a concrete class
  • πŸ‘‰πŸ» interface for creating families of related objects without specifying their concrete classes.
  • πŸ†š multiple related products
1
2
3
4
5
6
7
8
9
10
πŸ†š Factory
CarFactory β†’ Mercedes or Kia

πŸ†š Abstract Factory
GUIFactory
   β”œβ”€ createButton()
   └─ createCheckbox()

WindowsFactory β†’ WindowsButton + WindowsCheckbox
MacFactory β†’ MacButton + MacCheckbox

πŸ‘€

Screenshot-2026-03-08-at-10-04-04.png

βœ”οΈ Abstract product Interface

1
2
3
4
5
6
7
8
interface Button {
    //method that button can perform
    press();
}

interface Checkbox {
    tick();
}

βœ”οΈ Concete products

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class WindowsButton implements Button {
    //override method
    press();
}

class MacButton implements Button {
    //override method
    press();
}

class WindowsCheckbox implements Checkbox {
    //override method
    tick();
}

class MacCheckbox implements Checkbox {
    //override method
    tick();
}

βœ”οΈ Abstract Factory interface

1
2
3
4
interface GUIFactory {
    Button createButton();
    Checkbox createCheckbox();
}

βœ”οΈ Concete Factory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class WindowsFactory implements GUIFactory {

    public Button createButton() {
        return new WindowsButton();
    }

    public Checkbox createCheckbox() {
        return new WindowsCheckbox();
    }
}

class MacFactory implements GUIFactory {

    public Button createButton() {
        return new MacButton();
    }

    public Checkbox createCheckbox() {
        return new MacCheckbox();
    }
}

βœ”οΈ Client

1
2
Button windowButton = new WindowsFactory().createButton();
Checkbox windowCheckbox = new WindowsFactory().createCheckbox();

πŸ› οΈ

  • in javax.xml.parsers
  • DocumentBuilderFatory to create xml
1
2
3
4
5
6
public static void main(String[[ args) throws ParserConfigurationException, IOException, SAXException{
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = factory.newDocumentBuilder();
  Document document = builder.parse(new File("src/main/resource/config.xml"));
  System.out.println(document.getDocumentElement());
}
  • FactoryBean<T>
This post is licensed under CC BY 4.0 by the author.