Design mode Factory - buy goods (original)

xiaoxiao2021-03-06  75

Design mode Factory - buy goods articles

Today, my wife let me go to the market to buy some fruits, I have to buy anything I have, I have, and my wife is put!). When I came to the market, I found mainly some fruits: Apple (Apple), Grape (Grape) and Pear.

What is it better? I am thinking. As the saying goes: "A smoke after the meal, play live gods. After meals, I eat Apple, Xi Shi, I hide." For the beautiful woman, I decided to buy Apple.

Ok, the words come forward and start, start buying!

There are three of the following three Factory modes:

Simple Factory mode

Specialize in defining a class to create an instance of other classes, and the examples created usually have a common parent class.

Factory Method mode

Completion of a standard method of creating an object to a standard method defined in the parent class, not its constructor, what objects should be created by specific subclasses to determine.

Abstract Factory mode

Provide a common interface to create multiple objects associated with each other.

First, Simple Factory mode:

1. Here, let's define the Fruit Interface:

Public interface fruit {

Void Plant (); // Fruit is planted

Void enableeat (); // fruit can eat

}

2, Apple is the implementation of the Fruit Interface:

Public class apple imports fruit {

Public void plant () {

System.out.println ("Available Apple!");

}

Public void enableeat () {

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

}

}

3, grape (grape) is the implementation of the Fruit Interface:

Public class grape imports fruit {

Public void plant () {

System.out.println ("Variety!");

}

Public void enableeat () {

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

}

}

4, Yali (PEAR) is the implementation of the Fruit Interface:

Public class pear imports fruit {

Public void plant () {

System.out.println ("Snaps!");

}

Public void enableeat () {

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

}

}

5, define buy fruit (Buyfruit) process class:

Public class buzz {

/ **

* Simple factory method

* /

Public Static Fruit Buyfruit (String Which) {

IF (Which.equalsignorecase ("apple")) {// If it is Apple, return Apple instance

Return new apple ();

}

Else IF (Which.equalsignorecase ("pear")) {// If it is a pear, return to the pear instance

Return new strawberry ();

Else If (Which.equalsignorecase ("grape")) {// If it is grape, return to grape usage

Return new grape ();

}

Else {

Return NULL;

}

}

}

6, write test class:

Public class fruittest {

Public static void main (string args []) {

Buyfruit Buy = New Buyfruit (); // Start buying fruit this process

Buy.buyfruit ("apple"). Enableeat (); // Call Apple's enableeat () method

}

}

7. Description:

A: I want to buy Apple, just request the factory role (Buyfruit). After the factory role is after receiving the request, it will judge which product created and supplied.

B: However, for factories (Buyfruit), add 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 the client. In other words, accepting new products means modifying this plant.

C: So the openness of the Simple Factory mode is relatively poor.

Is there any way to solve this problem? Then you need Factory Method mode to serve us.

Second, Factory Method mode:

1. Similarly, let's define the fruit interface:

Public interface fruit {

Void Plant (); // Fruit is planted

Void enableeat (); // fruit can eat

}

2, Apple is the implementation of the Fruit Interface:

Public class apple imports fruit {

Public void plant () {

System.out.println ("Available Apple!");

}

Public void enableeat () {

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

}

}

3, grape (grape) is the implementation of the Fruit Interface:

Public class grape imports fruit {

Public void plant () {

System.out.println ("Variety!");

}

Public void enableeat () {

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

}

}

4, Yali (PEAR) is the implementation of the Fruit Interface:

Public class pear imports fruit {

Public void plant () {

System.out.println ("Snaps!");

}

Public void enableeat () {

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

}

}

5. Here we will buy fruit (Buyfruit) as an interface class:

Public interface buyfruit {

/ **

* Factory method

* /

Public Fruit Buyfruit (); // Define the process of buying fruit

}

6. Buy Apple is a (Buyapple) to buy fruit (Buyfruit) implementation public class Buyapple Implements Buyfruit {

Public Fruit Buyfruit () {

Return new apple (); // Return Apple

}

}

7, buy Yali is the implementation of this interface to buy fruit (Buyfruit)

Public class Buypear Implements Buyfruit {

Public Fruit Buypear () {

Return new pear (); // Return to the pear instance

}

}

8. Buying grapes is the implementation of this interface to buy fruit (Buyfruit)

Public class buygrape imports buyfruit {

Public Fruit Buygrape () {

Return new grape (); // Return to grape collections

}

}

9, write test classes:

Public class fruittest {

Public static void main (string args []) {

Buyapple Buy = new Buyapple (); // Start buying fruit this process

Buy.buyfruit (). enableeat (); // Call Apple's enableeat () method

}

}

10. Description:

A: Factory Mode Mode and Simple Factory Model differ in structural differences. The core of the factory method model is an abstract factory class, while simple factory model puts the core in a specific class. Factory method model allows many specific factories to inherit the creation of behaviors from abstract factory classes, so that it can become a combination of multiple simple factory models, and promote simple factory models.

B: Factory method mode can become very similar to simple factory models. Imvolution If you really need a system only a specific factory class, you may wish to merge the abstract factory class into the specific factory class. Since there is only one specific factory class, it is possible to change the factory method as a static method, and this time you get a simple factory model. C: If you need to join a new fruit, you only need to join a new fruit class and the factory class it corresponds to it. There is no need to modify the client, nor is therefore necessary to modify the image role or other specific factory roles. This system fully supports the "open-closed" principle for increasing new fruits.

D: For the Factory Method mode, it is just for a category (such as the fruit struit in this example), but if we still want to buy meat, then you can't, this is necessary to help the Abstract Factory Method mode. .

Third, Abstract Factory mode

1. Similarly, let's define the fruit interface:

Public interface fruit {

Void Plant (); // Fruit is planted

Void enableeat (); // fruit can eat

}

2, Apple is the implementation of the Fruit Interface:

Public class apple imports fruit {

Public void plant () {

System.out.println ("Available Apple!");

}

Public void enableeat () {

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

}

}

3, grape (grape) is an implementation of Fruit Interface: Public Class Grape Implements Fruit {

Public void plant () {

System.out.println ("Variety!");

}

Public void enableeat () {

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

}

}

4, Yali (PEAR) is the implementation of the Fruit Interface:

Public class pear imports fruit {

Public void plant () {

System.out.println ("Snaps!");

}

Public void enableeat () {

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

}

}

5, define meat (MEAT) interface:

Public interface meat {

Void feed (); // Meat is feeding

Void enableeat (); // can eat

}

6, pork (Bigmeat) is the implementation of the meat interface:

Public class bigmeat imports meat {

Public void feed () {

System.out.println ("Pig!");

}

Public void enableeat () {

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

}

}

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

Public class cowmeat imports meat {

Public void feed () {

System.out.println ("Nursing!");

}

Public void enableeat () {

System.out.Println ("Beef is delicious!");

}

}

8, we can define the buyer (Buyer) interface:

Public interface budy {

/ **

* Buy fruit factory method

* /

Public Fruit Buyfruit (Fruit Whichfruit);

/ **

* Factory method to buy meat

* /

Public Meat Buymeat; MEAT WHICHMEAT;

}

9. I (MyBuyer) is the implementation of the buying goods:

Public class mybuyer imports buyer {

/ **

* Buy fruit factory method

* /

Public Fruit Buyfruit (Fruit Whichfruit) {

Return Whichfruit;

}

/ **

* Factory method to buy meat

* /

Public Meat Buymeat (Meat Whichmeat) {

Return Whichmeat;

}

}

10, write test class:

Public class mybuyerabstractTractTest {

Public static void main (string args []) {

FRUIT Apple = new apple (); // Apple instance

MEAT big = new bigmeat (); // Pork instance

Mybuyer my = new mybuyer (); // I am an example of buyer

My.buyfruit (apple) .enableeeat (); // I bought Apple my.buymeat (big) .Enableeeat (); // I bought pork

}

}

11. Description:

A: Abstract factory mode can provide an interface to the client, enabling the client to create product objects in multiple product families without having to specify the specific type of product. This is the intention of abstract factory model.

B: Abstract factory model is the most abstract and most general form of all forms of factory models.

C: The biggest difference between abstract factory mode and factory method mode is that factory method model is a product (Fruit) level structure; and abstract factory models need to face multiple product rating structures (Fruit, MEAT).

转载请注明原文地址:https://www.9cbs.com/read-108841.html

New Post(0)