The gof Design Model of the factory model is roughly divided into three methods:
A simple factory Model
B Factory method mode
C Abstract Factory Model
A simple factory mode: The entire mode is centered on the factory class, and the factory class creates objects of the specified type.
The convenience is shown in the following figure:
Code implementation:
// Fruit
Interface friut {
Public friut get ();
}
// Apple
Class Apple implements friut {
Public friut get (){
Return new Apple ();
}
}
// Orange
Class orange implements friut {
Public friut get (){
Return new orange ();
}
}
// Banana
Class banana implements friut {
Public friut get (){
Return new banana ();
}
}
// Fruit Factory
Class friutfactory {
Public static friut make (friut ){
Return friut. Get ();
}
}
// Fruit store
Class friutshop {
Public static friut take (string name ){
If ("apple". Equals (name )){
Return friutfactory. Make (new Apple ());
} Else if ("orange". Equals (name )){
Return friutfactory. Make (New Orange ());
} Else if ("banana". Equals (name )){
Return friutfactory. Make (new banana ());
} Else {
System. Out. println ("no fruit for sale now ");
Return NULL;
}
}
Public static void main (string [] ARGs ){
System. Out. println ("enter the store to buy fruit :");
Take ("apple ");
Take ("banana ");
Take ("orange ");
}
}
B Factory method mode
From the simple factory above, we can see that if only a few simple categories need to be produced, it would be very convenient. If a fruit shop needs to sell 1000 million fruits, there are a lot of logic judgments in the factory .. this leads to the factory method mode.
Factory method mode: the Core factory class is no longer responsible for object creation, and the creation of specific objects is delayed to a specific factory class. The core factory class is only responsible for the interface that the sub-factory class must implement, while blocking the creation details.
In this way, the fruit point business is expanded, the variety is complete, and specific production is implemented.
Code implementation:
Interface friut {
Public friut get ();
}
Class Apple implements friut {
Public friut get (){
Return new Apple ();
}
}
Class orange implements friut {
Public friut get (){
Return new orange ();
}
}
Class banana implements friut {
Public friut get (){
Return new banana ();
}
}
Interface friutfactory {
Public friut makefriut ();
}
Class applefactory implements friutfactory {
@ Override
Public friut makefriut (){
Return new Apple ();
}
}
Class orangefactory implements friutfactory {
@ Override
Public friut makefriut (){
Return new orange ();
}
}
Class bananafactory implements friutfactory {
@ Override
Public friut makefriut (){
Return new banana ();
}
}
Class friutshop {
Public static friut take (friutfactory factory ){
Return factory. makefriut ();
}
Public static void main (string [] ARGs ){
System. Out. println ("enter the store to buy fruit :");
Take (New applefactory ());
Take (New bananafactory ());
Take (New orangefactory ());
}
}
C Abstract Factory method
The factory method has been very specific. Why do we need to abstract the factory?
After the size of the original fruit store, not only one kind of fruit is sold, but now it is necessary to sell a complete set of products (apple + banana = "Everyday beauty clothes)
From now on, each factory produces different types of products (the difference may not be obvious here, but it is represented by Apple and apple juice bands. Can you understand ?)
This is the difference between the factory method and the abstract factory, which is extended from the creation of a single object.
Code implementation:
Interface friut {
Public friut get ();
}
Interface drink {
Public drink get ();
}
Class applecider implements drink {
@ Override
Public drink get (){
Return new applecider ();
}
}
Class Apple implements friut {
Public friut get (){
Return new Apple ();
}
}
Class orange implements friut {
Public friut get (){
Return new orange ();
}
}
Class orangecider implements drink {
@ Override
Public drink get (){
Return new orangecider ();
}
}
Class bananacider implements drink {
@ Override
Public drink get (){
Return new bananacider ();
}
}
Class banana implements friut {
Public friut get (){
Return new banana ();
}
}
Interface shopfactory {
Public friut makefriut ();
Public drink makedrink ();
}
Class applefactory implements shopfactory {
@ Override
Public friut makefriut (){
Return new Apple ();
}
@ Override
Public drink makedrink (){
Return new applecider ();
}
}
Class orangefactory implements shopfactory {
@ Override
Public friut makefriut (){
Return new orange ();
}
@ Override
Public drink makedrink (){
Return new orangecider ();
}
}
Class bananafactory implements shopfactory {
@ Override
Public friut makefriut (){
Return new banana ();
}
@ Override
Public drink makedrink (){
Return new bananacider ();
}
}
Class friutshop {
Public static string take (shopfactory factory ){
System. Out. println ("it would be nice to have a name. Remember to be a product ");
Return factory. makefriut (). tostring () + factory. makedrink (). tostring ();
}
Public static void main (string [] ARGs ){
System. Out. println ("enter the store to buy fruit :");
Take (New applefactory ());
Take (New bananafactory ());
Take (New orangefactory ());
}
}
Let's assume that the juice and the corresponding fruit are combined ..
This is some of my understanding. If you have any questions, please point out.