Overview:
Factory method ):The factory method uses an abstract class to process all the products.CodeOnly product building methods are written as abstract methods. A specific class that inherits this abstract class only overrides its construction method, so that the same processing logic is reused for different constructed products. The factory method is applicable to the situations where the operation object to be instantiated must be determined in the subclass, and these operation objects reuse the same operation logic.
Class diagram:
Sample Code:
1. Product abstract classes in the factory
/// <Summary>
///Product abstract class in the factory
/// </Summary>
Public Abstract ClassProduct {}
2. Specific Products
/// <Summary>
///Specific products
/// </Summary>
Public ClassConcreteproduct: Product {}
3. Create abstract business classes containing factory methods
/// <Summary>
///Abstract business class containing factory methods
/// </Summary>
Public Abstract ClassCreator
{
VoidFactorymethod ();
}
4. Specific factory method implementation class
///
/// factory method implementation class
///
Public class concretecreator: creator
{< br> void factorymethod ();
}
Other examples:
The procedure is simple. To prevent you from forgetting it, you can extract an example from the blog to improve your understanding.
View code
///
/// Product Interface
///
Public Abstract Class Iproduct
{
Public Abstract Void Behavior ();
}
///
/// Type A Products
///
Class Producta: iproduct
{
Public Override Void Behavior ()
{
Console. writeline ( " Product A's behavior " );
}
}
///
/// Type B Products
///
Class Productb: iproduct
{
Public Override Void Behavior ()
{
Console. writeline ( " Behaviors of product B " );
}
}
///
/// Abstract business class containing factory methods
///
Public Abstract Class Acreator
{
Public Abstract Iproduct factorymethod ();
Public Void Opreatemethod ()
{
Iproduct Product = Factorymethod ();
// A series of operations on Product
}
}
///
/// Specific business class for producing type A Products
///
Public Class Creatora: acreator
{
Public Override Iproduct factorymethod ()
{
Return New Producta ();
}
}
///
/// Business type of B type product
///
Public Class Creatorb: acreator
{
Public Override Iproduct factorymethod ()
{
Return New Productb ();
}
}
Client call:
Acreator create= NewCreatora ();
Iproduct Product=Create. factorymethod ();
Product. behavior ();
Example of adding terrylee
The original error has been corrected.
View code
Namespace Designpatternlab. factorymethodpackage
{
Class Factorymethodsample4
{
}
/// <Summary>
/// Define abstract Interfaces
/// </Summary>
Public Abstract Class Log
{
Public Abstract Void Write ();
}
/// <Summary>
/// Implementation class
/// </Summary>
Public Class EventLog: Log
{
Public Override Void Write ()
{
Console. writeline ( " EventLog write success " );;
}
}
/// <Summary>
/// Implementation class
/// </Summary>
Public Class Filelog: Log
{
Public Override Void Write ()
{
Console. writeline ( " Filelog write success " );
}
}
/// <Summary>
/// Define abstract method Interfaces
/// </Summary>
Public Abstract Class Logfactory
{
Public Abstract Log create ();
}
/// <Summary>
/// Concrete implementation of the instantiation class
/// </Summary>
Public Class Eventfactory: logfactory
{
Public Override Log create ()
{
Return New EventLog ();
}
}
/// <Summary>
/// Classes to be instantiated
/// </Summary>
Public Class Filefactory: logfactory
{
Public Override Log create ()
{
Return New Filelog ();
}
}
}
// The client calls the following:
Logfactory Factory = New Eventfactory ();
Log = Factory. Create ();
Log. Write ();
Summary:
There are many application models in the factory. I hope to remember that I can use them well and write them at least during the interview.