Research on the factory of design pattern

Source: Internet
Author: User
Tags abstract define interface modify pear return
Design




Today, I go to the market, to decide whether to buy fruit products, or choose the product of fruit. Specific how to operate their own choice. Coming to the market, I found that there were mainly some fruits: apple, Grape (Grape) and pear (pear). What's the best thing to buy? I pondered. As the saying goes: "After dinner a cigarette, race live immortal." When I eat an apple after dinner, Xi Shi sees me hiding. "I decided to buy an apple for my wife's beauty," he said.







The following is the beginning of the factory model study, of course, using the examples I have cited above to illustrate.

Simple Factory mode





A class is specifically defined to create instances of other classes, and the instances that are created typically have common parent classes.

Factory method Mode





The creation of an object is done by a standard method defined in the parent class, not its constructor, and what object should be created is determined by the specific child class.

Abstract Factory mode





Provides a common interface to create multiple objects that are interrelated.







First, simple Factory mode:





1. Here, we first define the fruit (Fruit) interface:

Public interface Fruit {

void plant (); Fruit is grown.

void Enableeat (); Fruit can Eat

}

2. Apple is an implementation of the fruit (Fruit) interface:

public class Apple implements fruit{

public void plant () {

System.out.println ("Grow apples!");

}

public void Enableeat () {

System.out.println ("Apple is delicious!");

}

}

3. Grape (Grape) is the realization of the fruit (Fruit) interface:

public class Grape implements fruit{

public void plant () {

System.out.println ("Grow grapes!");

}

public void Enableeat () {

System.out.println ("Grapes are delicious!");

}

}

4, pear (pear) is the realization of the fruit (Fruit) interface:

public class Pear implements fruit{

public void plant () {

System.out.println ("Kind of pear!");

}

public void Enableeat () {

System.out.println ("Pear delicious!");

}

}

5, the definition buys the fruit (buyfruit) This process class:

public class Buyfruit {

/**

Simple Factory method

*/

public static Fruit Buyfruit (String which) {

if (Which.equalsignorecase ("Apple")) {//If an apple, return the Apple instance

Return to New Apple ();

}

else if (which.equalsignorecase ("pear")) {//If it is pear, return the case of Pear

return new Strawberry ();

}

else if (which.equalsignorecase ("Grape")) {//if grape, return grape instance

return new Grape ();

}

else{

return null;

}

}

}

6, write test class:

public class Fruittest {

public static void Main (String args[]) {

Buyfruit buy = new Buyfruit (); The process of starting to buy fruit

Buy.buyfruit ("Apple"). Enableeat (); Call the Apple Enableeat () method

}

}

7. Description:

A: I want to buy Apple, just to the factory role (Buyfruit) request. The factory role, upon request, will decide which product to create and provide.

B: But for the factory character (Buyfruit), adding new products (such as adding strawberries) is a painful process. Factory roles must know each product, how to create them, and when to provide them to clients. In other words, accepting new products means modifying the factory.

C: Therefore, simple Factory mode is relatively poor in openness.

Is there any way to solve the problem? Then you need to factory method mode to serve us.

Second, Factory method mode:





1, again, we first define the fruit (Fruit) interface:

Public interface Fruit {

void plant (); Fruit is grown.

void Enableeat (); Fruit can Eat

}

2. Apple is an implementation of the fruit (Fruit) interface:

public class Apple implements fruit{

public void plant () {

System.out.println ("Grow apples!");

}

public void Enableeat () {

System.out.println ("Apple is delicious!");

}

}

3. Grape (Grape) is the realization of the fruit (Fruit) interface:

public class Grape implements fruit{

public void plant () {

System.out.println ("Grow grapes!");

}

public void Enableeat () {

System.out.println ("Grapes are delicious!");

}

}

4, pear (pear) is the realization of the fruit (Fruit) interface:

public class Pear implements fruit{

public void plant () {

System.out.println ("Kind of pear!");

}

public void Enableeat () {

System.out.println ("Pear delicious!");

}

}

5, here we will buy fruit (buyfruit) defined as the interface class:

public interface buyfruit{

/**

Factory method

*/

Public Fruit buyfruit (); Define the process of buying a fruit

}

6, buy Apple is (buyapple) to buy fruit (buyfruit) The realization of this interface

public class Buyapple implements buyfruit{

Public Fruit Buyfruit () {

Return to New Apple (); Back to Apple instance

}

}

7, buy Pear is (buypear) to buy fruit (buyfruit) The realization of this interface

public class Buypear implements buyfruit{

Public Fruit Buypear () {

return new Pear (); Back to the case of pear

}

}

8, buy grapes is (buygrape) to buy fruit (buyfruit) The realization of this interface

public class Buygrape implements buyfruit{

Public Fruit Buygrape () {

return new Grape (); Back to grapes example

}

}

9, write test class:

public class Fruittest {

public static void Main (String args[]) {

Buyapple buy = new Buyapple (); The process of starting to buy fruit

Buy.buyfruit (). Enableeat (); Call the Apple Enableeat () method

}

}

10. Description:

A: The structural differences between the factory method model and the simple factory model are obvious. The core of the factory method pattern is an abstract factory class, while the simple factory model puts the core on a specific class. The factory method pattern allows many specific factory classes to inherit from the abstract factory class, thereby becoming a synthesis of multiple simple factory patterns, which in turn promotes simple factory patterns.

B: The factory method model can be degraded to resemble a simple factory model. Imagine merging abstract factory classes into specific factory classes if it is very certain that a system requires only one specific factory class. Since there is only one specific factory class, it is possible to change the factory method to a static method, and then get a simple factory model.

C: If you need to add a new fruit, then only need to add a new fruit class and its corresponding factory class. There is no need to modify the client or modify the abstract factory role or other existing specific factory roles. The system fully supports the "open-closed" principle for adding new fruit classes.

D: For Factory method mode, it is only for one category (such as the fruit in this example fruit), but if we still want to buy meat, then it is not, it is necessary to abstract Factory method mode to help.

Iii. Abstract Factory Model





1, again, we first define the fruit (Fruit) interface:

Public interface Fruit {

void plant (); Fruit is grown.

void Enableeat (); Fruit can Eat

}

2. Apple is an implementation of the fruit (Fruit) interface:

public class Apple implements fruit{

public void plant () {

System.out.println ("Grow apples!");

}

public void Enableeat () {

System.out.println ("Apple is delicious!");

}

}

3. Grape (Grape) is the realization of the fruit (Fruit) interface:

public class Grape implements fruit{

public void plant () {

System.out.println ("Grow grapes!");

}

public void Enableeat () {

System.out.println ("Grapes are delicious!");

}

}

4, pear (pear) is the realization of the fruit (Fruit) interface:

public class Pear implements fruit{

public void plant () {

System.out.println ("Kind of pear!");

}

public void Enableeat () {

System.out.println ("Pear delicious!");

}

}

5. Define meat (meat) interface:

Public interface Meat {

void feed (); Meat is fed.

void Enableeat (); Meat can Eat

}

6, Pork (bigmeat) is the implementation of the meat (meat) interface:

public class Bigmeat implements meat{

public void Feed () {

SYSTEM.OUT.PRINTLN ("Pig!");

}

public void Enableeat () {

System.out.println ("Pork is delicious!");

}

}

7, Beef (cowmeat) is the implementation of the meat (meat) interface:

public class Cowmeat implements meat {

public void Feed () {

System.out.println ("Cattle-raising!");

}

public void Enableeat () {

System.out.println ("Delicious beef!");

}

}

8. We can define the buyer (Buyer) interface:

Public interface Buyer {

/**

How to buy Fruit factory

*/

Public Fruit buyfruit (Fruit whichfruit);

/**

The factory method of buying meat

*/

Public meat buymeat (meat whichmeat);

}

9, I (Mybuyer) is to the buyer (Buyer) interface implementation:

public class Mybuyer implements buyer{

/**

How to buy Fruit factory

*/

Public Fruit buyfruit (Fruit whichfruit) {

return whichfruit;

}

/**

The factory method of buying meat

*/

Public meat buymeat (meat whichmeat) {

return whichmeat;

}

}

10, write test class:

public class Mybuyerabstracttest {

public static void Main (String args[]) {

Fruit Apple = new Apple (); Apple example

Meat big = new bigmeat (); Pork example

Mybuyer my = new Mybuyer (); I'm an example of a buyer.

My.buyfruit (apple). Enableeat (); I buy apples.

My.buymeat (BIG). Enableeat (); I buy pork.

}

}

11. Description:

A: Abstract Factory mode provides an interface to clients that enables clients to create product objects in multiple product families without having to specify the specific type of product. This is the intention of the abstract factory model.

B: The abstract factory pattern is the most abstract and most general form of the factory pattern in all forms.




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.