Simple factory pattern and factorypattern

Source: Internet
Author: User

Simple factory pattern and factorypattern

In simple factory mode, a factory object is used to determine the product type instance (object) to be created ), A factory class determines the object or instance of a product to be created based on the input parameters.

This model mainly involves three roles: factory role, abstract product, and specific product.

The core of this model is the business logic closely related to applications,

Abstract Product is the parent class of the Product to be created. It is generally implemented by a java interface abstract class.

The instance of the Product to be created for the specific Product (Concrete Product)

The source code is as follows:

1: Abstract Product

public interface Fruit {    void grow();    void plant();}
2: Product 1

public class Apple implements Fruit {    public Apple() {        System.out.println("Apple.Apple");    }    @Override    public void grow() {        System.out.println("Apple.grow");    }    @Override    public void plant() {        System.out.println("Apple.plant");    }}

3: product 2
public class FruitGardener {    public static Fruit factory(String which) {        if (which.equalsIgnoreCase("apple")) {            return new Apple();        } else {            return new StrawBerry();        }    }}
4: Core factories

public class StrawBerry implements Fruit {    public StrawBerry() {        System.out.println("StrawBerry.StrawBerry");    }    @Override    public void grow() {        System.out.println("StrawBerry.grow");    }    @Override    public void plant() {        System.out.println("StrawBerry.plant");    }}
5: Test class

public class Tests {    @Test    public void testSimpleFactory() {        FruitGardener.factory("APPLE");        FruitGardener.factory("strawberry");    }}


6. Test results:

Apple.AppleStrawBerry.StrawBerryProcess finished with exit code 0


7: Note: This project is built based on maven, and the testing framework uses JUnit.

<dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.11</version>        </dependency>
8: the source code will be added later





What is the factory mode in java?

Are you asking about the simple factory model?
Simple Factory Pattern, also known as Static Factory Pattern ). Here are two examples to quickly understand the simple factory mode in Java:

The nvwa
Saying: "The world is open, there are no people, And the nvwa community is a human ." Nvwa needs to create people by nature, but before nvwa creates a person, the concept of man only exists in nvwa's thoughts.
This is the application of the simple factory model.

First, there are several important roles in this idea: nvwa itself, the concept of abstract people, and the specific people created by nvwa.
1) nvwa is a factory, that is, the core role of the simple factory model.

2) Retired individuals, including Zhang San and Li Si. These people are specific product roles in the simple factory model.
3.) Abstract people are an idea that only existed in nvwa's mind at the earliest. All specific people created by nvwa according to this idea comply with the definition of this abstract person. In other words, this abstract idea specifies the interfaces (features or functions) that all specific people must have)
  

Note: nvwa, Zhang San, and Li Si belong to creating dependencies. The arrows should be dotted lines (implementation represents associations ). Zhang San and Li Si have an implementation relationship with people, and the arrow at the triangle header should also be a dotted line (solid line represents an inheritance relationship ).
After understanding the above things, let's take a look at the following example. I believe that after reading this article, I will have a good understanding of the java simple factory model:

There is a farm company that specializes in selling all kinds of fruits to the market. In this system, we need to describe the following fruits:
Grape
Strawberry
Apple
Different from other plants, fruit can finally be picked and eaten. A natural practice is to establish an interface applicable to various fruits to distinguish them from plants on other farms,
The following Code declares an interface for the fruit class:
Public interface Fruit
{
// Planting
Public void plant ();

// Growth
Public void grow ();

// Gains
Public void harvest ();
}
The fruit interface specifies the interfaces that all fruits must implement, including the methods required by any fruit class, plant (), grow (), and harvest ();

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, a treeAge exists to describe the age of the apple. The Code is as follows:
/**
*
* Class Name: Apple
* Category Description: Farm Apple category
*
*/
Public class Apple implements Fruit
{
/**
* Apple is a perennial plant.
*/
Private int treeAge;
/* (Non-Javadoc)
* @ See fac. Fruit # grow ()
*/
@ Override
Public void grow ()
{
System. out. println ("Apple started to grow... ");
}
/* (Non-Javadoc)
* @ See fac. Fruit # harvest ()
*/
@ Override
Public void harvest ()
{
System. out. println ("Apple started to harvest... ");
}
/* (Non-Javadoc)
* @ See fac. Fruit # ...... the remaining full text>


In Java, what are the 23 design patterns that must be understood?

1. Simple Factory Pattern)
2. Builder Pattern)
3. Strategy Mode
4. Factory Method Pattern)
5. Abstract Factory)
6. Command Pattern)
7. Template Method)
8. Single Pattern)
9. Prototype Pattern)

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.