- Mastering Microservices with Java 9(Second Edition)
- Sourabh Sharma
- 241字
- 2021-07-02 21:54:50
Factory
A factory is required when a simple constructor is not enough to create the object. It helps to create complex objects, or an aggregate that involves the creation of other related objects.
A factory is also a part of the life cycle of domain objects, as it is responsible for creating them. Factories and repositories are in some way related to each other, as both refer to domain objects. The factory refers to newly created objects, whereas the repository returns the already existing objects either from the memory, or from external storage.
Let's see how control flows, by using a user creation process application. Let's say that a user signs up with a username user1. This user creation first interacts with the factory, which creates the name user1 and then caches it in the domain using the repository, which also stores it in the storage for persistence.
When the same user logs in again, the call moves to the repository for a reference. This uses the storage to load the reference and pass it to the requestor.
The requestor may then use this user1 object to book the table in a specified restaurant, and at a specified time. These values are passed as parameters, and a table booking record is created in storage using the repository:

Repository object flow
The factory may use one of the object-oriented programming patterns, such as the factory or abstract factory pattern, for object creation.
- Boost.Asio C++ Network Programming(Second Edition)
- Unity 2020 Mobile Game Development
- Rake Task Management Essentials
- PLC編程及應用實戰
- AutoCAD VBA參數化繪圖程序開發與實戰編碼
- 劍指Java:核心原理與應用實踐
- 51單片機C語言開發教程
- C和C++游戲趣味編程
- Building Microservices with .NET Core
- 用案例學Java Web整合開發
- Swift 4從零到精通iOS開發
- Scratch趣味編程:陪孩子像搭積木一樣學編程
- 微信小程序開發實戰:設計·運營·變現(圖解案例版)
- MyBatis 3源碼深度解析
- Learning Android Application Testing