Factory of design patterns-purchasing goods
Today, my wife asked me to go to the market to buy some fruit and decide what to buy (HA, my wife has granted permissions !). When I came to the market, I found that there were mainly fruits: Apple, grape and pear ).
What should I buy? I think for a while. As the saying goes: "After a meal, a smoke, a living fairy. Eat an apple after dinner, and Xi Shi sees me hiding ." I decided to buy an apple for my wife's beauty.
Okay, let's get back to business. Start buying it!
There are three main factory modes:
Simple factory Mode
A dedicated class is defined to create instances of other classes. The created instance usually has a common parent class.
Factory method mode
The object is created by a standard method defined in the parent class, rather than its constructor. The specific subclass determines the object to be created.
Abstract Factory Mode
Provides a common interface to create multiple associated objects.
I. Simple factory mode:
1. Here, we first define the fruit (fruit) interface:
Public interface fruit {
Void plant (); // the fruit is planted.
Void enableeat (); // you can eat fruit.
}
2. Apple is an implementation of the fruit interface:
Public class Apple implements fruit {
Public void plant (){
System. Out. println ("apple! ");
}
Public void enableeat (){
System. Out. println ("delicious apple! ");
}
}
3. Grape is the implementation of the fruit interface:
Public class grape implements fruit {
Public void plant (){
System. Out. println ("Grape Species! ");
}
Public void enableeat (){
System. Out. println ("grape is delicious! ");
}
}
4. Pear is an implementation of the fruit interface:
Public class pear implements fruit {
Public void plant (){
System. Out. println ("kind of pears! ");
}
Public void enableeat (){
System. Out. println ("it's delicious! ");
}
}
5. define the process of buying fruit (buyfruit:
Public class buyfruit {
/**
* Simple factory Method
*/
Public static fruit buyfruit (string which ){
If (which. equalsignorecase ("apple") {// if it is an apple, the apple instance is returned.
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 a grape, return the grape instance
Return new grape ();
}
Else {
Return NULL;
}
}
}
6. Compile the test class:
Public class fruittest {
Public static void main (string ARGs []) {
Buyfruit buy = new buyfruit (); // starts the fruit buying process.
Buy. buyfruit ("apple"). enableeat (); // call Apple's enableeat () method
}
}
7. Description:
A: If you want to buy an apple, you only need to request the factory role (buyfruit. After receiving the request, the factory role determines which product to create and provide.
B: But for a factory role (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 a new product means modifying the factory.
C: The openness of the simple factory mode is poor.
Is there any way to solve this problem? Then we need the factory method mode to serve us.
Ii. Factory method mode:
1. Similarly, we first define the fruit (fruit) interface:
Public interface fruit {
Void plant (); // the fruit is planted.
Void enableeat (); // you can eat fruit.
}
2. Apple is an implementation of the fruit interface:
Public class Apple implements fruit {
Public void plant (){
System. Out. println ("apple! ");
}
Public void enableeat (){
System. Out. println ("delicious apple! ");
}
}
3. Grape is the implementation of the fruit interface:
Public class grape implements fruit {
Public void plant (){
System. Out. println ("Grape Species! ");
}
Public void enableeat (){
System. Out. println ("grape is delicious! ");
}
}
4. Pear is an implementation of the fruit interface:
Public class pear implements fruit {
Public void plant (){
System. Out. println ("kind of pears! ");
}
Public void enableeat (){
System. Out. println ("it's delicious! ");
}
}
5. Here we define buyfruit as an interface class:
Public interface buyfruit {
/**
* Factory Method
*/
Public fruit buyfruit (); // defines the process of fruit buying.
}
6. Buy an apple (buyapple) to buy a fruit (buyfruit) interface implementation
Public class buyapple implements buyfruit {
Public fruit buyfruit (){
Return new Apple (); // return the apple instance
}
}
7. The implementation of the buypear Interface
Public class buypear implements buyfruit {
Public fruit buypear (){
Return New Pear (); // return the pear instance
}
}
8. buygrape is used to implement the buyfruit interface.
Public class buygrape implements buyfruit {
Public fruit buygrape (){
Return new grape (); // return the grape instance
}
}
9. Compile the test class:
Public class fruittest {
Public static void main (string ARGs []) {
Buyapple buy = new buyapple (); // starts the fruit buying process
Buy. buyfruit (). enableeat (); // call Apple's enableeat () method
}
}
10. Description:
A: The structure difference between the factory method mode and the simple factory mode is obvious. The core of the factory method mode is an abstract factory class, while the simple factory mode places the core on a specific class. The factory method mode allows many specific factory classes to inherit the creation behavior from the abstract factory class, which can be a combination of multiple simple factory models and promote the simple factory mode.
B: After the factory method model degrades, it can become much like a simple factory model. Imagine that if a system needs only one specific factory class, it would be better to merge the abstract factory class into a specific factory class. Since there is only one specific factory type, we can change the factory method to a static method. At this time, we get a simple factory model. C: to add a new fruit class, you 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 factory roles. This system fully supports the "open-close" principle for new fruit classes.
D: for the factory method mode, it is only for a category (such as fruit in this example), but if we still want to buy meat, it will not work, this requires the help of the factory method mode.
Iii. Abstract Factory Mode
1. Similarly, we first define the fruit (fruit) interface:
Public interface fruit {
Void plant (); // the fruit is planted.
Void enableeat (); // you can eat fruit.
}
2. Apple is an implementation of the fruit interface:
Public class Apple implements fruit {
Public void plant (){
System. Out. println ("apple! ");
}
Public void enableeat (){
System. Out. println ("delicious apple! ");
}
}
3. Grape is the implementation of the fruit interface:
Public class grape implements fruit {
Public void plant (){
System. Out. println ("Grape Species! ");
}
Public void enableeat (){
System. Out. println ("grape is delicious! ");
}
}
4. Pear is an implementation of the fruit interface:
Public class pear implements fruit {
Public void plant (){
System. Out. println ("kind of pears! ");
}
Public void enableeat (){
System. Out. println ("it's delicious! ");
}
}
5. Define the meat interface:
Public interface meat {
Void feed (); // meat is fed
Void enableeat (); // meat can be eaten
}
6. Pork (bigmeat) is an implementation of the meat (meat) interface:
Public class bigmeat implements meat {
Public void feed (){
System. Out. println ("pigs! ");
}
Public void enableeat (){
System. Out. println ("pork is delicious! ");
}
}
7. Beef (cowmeat) is an implementation of the meat (meat) interface:
Public class cowmeat implements meat {
Public void feed (){
System. Out. println ("yangniu! ");
}
Public void enableeat (){
System. Out. println ("beef is delicious! ");
}
}
8. We can define the buyer (buyer) interface:
Public interface buyer {
/**
* How to buy a Fruit Factory
*/
Public fruit buyfruit (fruit whichfruit );
/**
* How to buy meat
*/
Public meat buymeat (meat whichmeat );
}
9. mybuyer is the implementation of the buyer interface:
Public class mybuyer implements buyer {
/**
* How to buy a Fruit Factory
*/
Public fruit buyfruit (fruit whichfruit ){
Return whichfruit;
}
/**
* How to buy meat
*/
Public meat buymeat (meat whichmeat ){
Return whichmeat;
}
}
10. Compile the test class:
Public class mybuyerpolicacttest {
Public static void main (string ARGs []) {
Fruit Apple = new Apple (); // Apple instance
Meat big = new bigmeat (); // pork instance
Mybuyer my = new mybuyer (); // I am a buyer's instance
My. buyfruit (Apple). enableeat (); // I bought an apple
My. buymeat (BIG). enableeat (); // I bought pork.
}
}
11. Note:
A: The Abstract Factory mode can provide an interface to the client, so that the client creates product objects in multiple product families without specifying the specific product type. This is the intention of the abstract factory model.
B: The abstract factory model is the most abstract and general factory model in all forms.
C: The biggest difference between the abstract factory mode and the factory method mode is that the factory method mode is aimed at a product (fruit) level structure; abstract Factory models face multiple product level structures (fruit and meat ).
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.