Simple Factory)
The simple Factory mode is the class creation mode, also called the Static Factory Method mode. The simple factory mode is determined by a factory object to create a product instance.
Structure of the simple factory model:
The Creator role is the core of the factory method model and contains the business logic closely related to the application. The factory class creates a product object under direct calls from the client. It is often implemented by a specific java class.
Abstract Product role: the class that assumes this role is the parent class of the object created by the factory method mode or the interfaces they share. Abstract product roles can be implemented using a java interface or java Abstract class.
Concrete Product role: Any object created in the factory method mode is an instance of this role. A specific Product is implemented by a specific java class.
For example:
There is a farm company dedicated to selling all kinds of fruits to the market. In this system, you need to describe the following fruits:
Grape
Strawberry
Apple
Fruits are very different from other plants, so they can be picked and eaten. A natural practice is to create an interface for various fruits to be separated from other plants on the farm, as shown in.
The fruit interface specifies all interfaces that must be implemented, including methods required for any fruit: planting plant (), growing grow (), and harvesting harvest ().
Code List:
Abstract Product role
VcfJqyjGu7n7KTwvcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20150309/20150309082301140.png" alt = "\">
Specific product roles (grape)
Specific product role (Strawberry)
Fruit exceptions
Static Factory
Test class
There are already too many other