(1) simple factory Model

Source: Internet
Author: User

(1) simple factory Model
(1) simple factory model 1. Why do I learn the design model?

Design pattern is a set of summary of code Design experiences that are repeatedly used, known to most people, classified and catalogued. The design pattern is used to make code reusable, make it easier for others to understand, and ensure code reliability. There is no doubt that the design pattern is win-win for others and the system; The design pattern enables code compilation to be truly engineered; The design pattern is the cornerstone of the software engineering, just like the structure of the building.

2. Why is the first simple learning factory?

Simple Factory is the creator of a class, also known as the Static Factory Method ). A simple factory is determined by a factory object to create a cashier product instance. The first simple factory learning is because this is the most basic creation mode. Learning a simple factory is helpful for us to learn the factory method well.

3. Introduction of simple factories

Taking farm companies as an example, we sell a variety 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 eventually be eaten. A natural practice is to establish an interface applicable to various fruits to distinguish them from other plants on the farm.


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + y665 + 7 XEvdO/tests + tests/tests + CjxwPiA8L3A + cjxwpsuufu907/tests + PGJyPgo8L3A + uploads = "brush: java; "> Public interface Fruit {// growth void grow (); // harvest void harvest (); // planting void plant ();}

The class diagram describing the apple category is as follows:



Apple is a kind of fruit, so it implements all the methods declared by the fruit interface. In addition, because Apple is a perennial plant, it has a treeAge to describe the age of the apple tree.


Public class Apple implements Fruit {private int treeAge; // growth public void grow () {log ("Apple if growing... ");} // get public void harvest () {log (" Apple has been harvest. ")} // planting public void plant () {log (" Apple has been planted. ")} // auxiliary method public static log (String msg) {System. out. println (msg);} // Method for Determining the tree age public int getTreeAge () {return treeAge;} // Method for assigning values to the tree age public int setTreeAge (int treeAge) {this. treeAge = treeAge ;}}

Grapes and strawberries are similar to apples. Here we will not give them one by one.

The farm gardener (here as a simple factory) is also part of the system, naturally represented by a suitable class. This class is the FruitGardener class, and Its Structure




FruitGardener creates different fruit objects, such as Apple, based on client requirements ).

The gardener code is as follows:


Public class FruitGardener {// simple factory public static Fruit factory (String which) {if (which. equalsIgnoreCase ("Apple") {return new Apple;} if (which. inclusignorecase ("strawberry") {return new strawberry;} if (which. repeated signorecase ("grape") {return new grape ;}}}

4. Simple factory Structure

A simple factory involves three roles: factory role, abstract Product role, and specific product role:

Creator: it is the core of a simple factory and contains the logic closely related to the application. The factory class creates product objects under the call. Abstract Product: the class that assumes this role is the parent class of the object created by a simple factory, or they have a common interface. Concrete Product: Any object created in a simple factory is an instance of this role.


Factory source code


Public class Creator {// simple factory public static Product factory () {return new ConcreteProduct ();}}

Abstract product source code


public interface Product {}

Specific products


public class ConcreteProduct implements Product{   public ConcreteProduct(){}}



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.