For a simple factory Its factory can only look like this
Public class simplyfactory {
/**
* Static factory Method
*/
Public static prouct Factory (string which) Throw nosuchproductexcption
{
If (which. ~ignorecase ("product1 "))
{
Return new product1 ();
}
Else if (which. equalsignorecase ("product2 "))
{
Return new product2 ();
}
Else if (which. equalsignorecase ("product3 "))
{
Return new product3 ();
}
Else throw nosuchproductexcption ("nosuchproduct ");
}
}
}
For products such as product1, product2, and product3, You can execute the product interface or do not execute the product interface (of course, this is not good). This product interface is only used to abstract specific products.
Public interface Product
{
Void productmethod1 (); // These are
Void productmethod2 ();
Void productmethod3 ();
}
For the factory, as long as there is such a product, it is generally necessary to have its production method in the factory, otherwise it will throw an exception, but the factory production, it is also necessary to issue commands for the production of products, at least to send signals to the factory, so that the factory is sufficient to distinguish what products are to be produced, otherwise the factory does not know which products to produce,
For a simple factory, it is necessary to enumerate all the products in the factory, so the simple factory is very stupid.
If (which. equalignorecase ("product1") is only used to identify the tag value of the product to be produced. (It can also be determined based on other product attributes, such as the product type, product size, in short, we can distinguish between the product attributes, or the variables related to the product attributes.) or if the tag value is A, the product A is produced, or the factory does not define this, I want to produce product B, or something special. If I want to produce product A + product B, return New producta () + new productb.
In this way, we can see a problem. To be produced by a simple factory, we must have a method definition that can be produced in a simple factory, of course, the definition of this specific product class is also required, so that the nosuchproduct exception will not be thrown when it is new in a simple factory.
For the factory method
In essence, it enables the factory to implement Abstract Factory interfaces. It puts how to produce a specific thing in a specific factory for implementation. The so-called "delay to implementation in the subclass"
Public interface creator
{
Public prouct Factory ();
}
Public subcreator1 implent creator
{
Public prouct Factory ()
{
Return new concreteproduct1 ();
}
}
Public subcreator2 implent creator
{
Public prouct Factory ()
{
Return new concreteproduct2 ();
}
}
Note: The return type is product type !!
In this way, the client calls a new instance of a specific factory and then commands it to produce the instance. For the parent class of a specific factory (both the factory interface, the interface can be completely implemented by inheriting the parent class as a subclass, but this is not good and does not comply with the OO principle). It does not know what products are produced, even if it is instantiated by a specific factory, it does not know.
Abstract Factory Model
The abstract factory pattern is intended to "provide an interface for creating a series of related or mutually dependent objects without specifying their specific classes" or for specific customers (or situations) provides objects of a specific series.
Public interface creator
{
Public proucta factorya ();
Public prouctb factoryb ();
}
Public interface producta // producta Interface
{
}
Public interface productb // productb Interface
{
}
Public class concreator1 implent creator
{
Public proucta factorya ()
{
Return new concreteproducta1 ();
}
Public prouctb factoryb ()
{
Return new concreteproductb1 ();
}
}
Public class concreator2 implent creator
{
Public proucta factorya ()
{
Return new producta2 ();
}
Public prouctb factoryb ()
{
Return new productb2 ();
}
}
Public class producta1 implements producta
{
Public producta1 ()
{
}
}
Public class producta2 implements producta
{
Public producta2 ()
{
}
}
Public class productb1 implements productb
{
Public productb1 ()
{
}
}
Public class productb2 implements productb
{
Public productb2 ()
{
}
}
Actually, this is the case.
1. Two factory classes concreator1 and concreator2 both implement the Creator interface.
2. proucta1 and producta2 all implement the producta interface.
3. prouctb1 and productb2 all implement the productb interface.
4. concreator1 is responsible for producing products of the producta type (including producta1 and productb1)
5. concreator2 is responsible for producing productb products (including producta2 and productb2)
6. The factory method also has this feature. That is to say, the Creator does not know what is produced, or even whether concreator1 or concreator2 is instantiated, because the client is happy to call that factory, that is to say, what the factory can produce is visible to the client. There is even another situation where the client is happy to produce producta1 and I will not produce producta2, because they are still loose and not bound together in the above example. ---------------------------- Summary ----------------------------
Factory method mode:
An abstract product class can be derived from multiple specific product classes.
An abstract factory class can be derived from multiple factory classes.
Each factory class can only create one instance of a specific product class.
Abstract Factory mode:
multiple abstract product classes, each of which can be derived from multiple specific product classes.
An abstract factory class that can be derived from multiple factory classes.
you can create instances of multiple product classes for each factory class.
Differences:
the factory method mode has only one abstract product class, while the abstract factory mode has multiple.
A factory class in factory method mode can only create an instance of a specific product class, abstract Factory mode can create multiple.