Technology Sharing

Java Design Patterns --- (Creational Patterns) Factory, Singleton, Builder, Prototype

2024-07-11

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina


Preface

Creational pattern (4 types): used to describe "how to create an object", its main feature is "separating the creation and use of objects".

1. Factory Pattern

1.1 Factory Method

1.1.1 Common factory method pattern

The common factory method pattern is to establish a factory class to create instances of some classes that implement the same interface. First, look at the relationship diagram:
insert image description here
Take an example of sending emails and text messages. First, create a common interface for both:

public interface Sender {
	public void Send();
}