Simple Factory)

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.