Simple factory, factory method and abstract factory Model

Source: Internet
Author: User

Simple factories, factory methods, and abstract factories all belong to the Creation Mode in the design mode. Its main function is to help us extract the instantiation part of the object, optimize the system architecture, and enhance the scalability of the system.

This article is my summary of these three models and my understanding of the differences between them.

Simple Factory

Factory classes in simple factory mode generally use static methods to return different object instances by receiving different parameters.

Without modifying the code, it cannot be expanded.

 

Factory method

The factory method provides a factory class for each product. Use different factory instances to create different product instances.

Any product can be added to the same level structure.

 

Abstract Factory

Abstract Factory should deal with the concept of product family. For example, every automobile company may want to produce cars, trucks, and buses at the same time. Every factory must create cars, trucks, and buses.

It is easy to add new product lines, but new products cannot be added.

 

Summary

★In the factory model, the most important thing is the factory class, not the product class. Product classes can be in multiple forms, multi-level inheritance, or a single class. But it should be clear that only one type of instance is returned for the interface in the factory mode. This is worth noting when designing the product class. It is best to have a parent class or a commonly implemented interface.

★In factory mode, the returned instance must be created by the factory, rather than obtained from other objects.

★The instances returned in factory mode can not be newly created, and the returned instances created by the factory can also be.

Differences

Simple Factory: used to produce any product in the same level structure. (Unable to add new products)

Factory model: used to produce fixed products in the same level structure. (Any product can be added)
Abstract Factory: used to produce all products of different product families. (There is nothing to do with adding new products; Support for adding product families)

The above three factory methods have different levels of support in the hierarchical structure and product family. Therefore, you should consider which method should be used based on the actual situation.

 

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/superbeck/archive/2009/08/14/4446177.aspx

 

 

Hypothetical scenario: hp and apple are world-renowned computer manufacturers. Their Respective computer operating systems are windows and macintosh, respectively. microsoft is the leader in the software industry. To attract more customers to purchase computers, hp and apple ask ms to develop two of the most commonly used software, office software and instant messaging tools for them. After finishing the order, ms developed office and msn for hp and apple respectively.

The above scenario actually contains our common design patterns of thinking, code is cheap, and code comes.

I. Simple Factory

View plaincopy to clipboardprint?
Using System;
// Ms Product
Public class Software
{
Private string softwareName;

Public virtual string SoftwareName
{
Get {return softwareName ;}
Set {softwareName = value ;}
}
Public string GetSoftwareName ()
{
Return softwareName;
}
}
// Ms product: office
Public class Office: Software
{
Public override string GetSoftwareName ()
{
This. SoftwareName = "Office ";
Return "Office ";
}
}
// Ms product: msn
Public class Msn: Software
{
Public override string GetSoftwareName ()
{
This. SoftwareName = "Msn ";
Return "Msn ";
}
}
/// <Summary>
/// Simple factory
/// </Summary>
Public class MsSoftwareFactory
{
Public static Software CreateSoftware (string softwareName)
{
Software msSoftware = null;
Switch (softwareName)
{
Default:
Break;
Case "office ":
MsSoftware = new Office ();
Break;
Case "msn ":
MsSoftware = new Msn ();
Break;
}
Return msSoftware;
}
}

/// <Summary>
/// Client call
/// </Summary>
Public class Client {
Static void Main (string [] args)
{
Software msSoftware = MsSoftwareFactory. CreateSoftware ("office ");
Console. WriteLine (msSoftware. GetSoftwareName ());
}
}
Using System;
// Ms Product
Public class Software
{
Private string softwareName;

Public virtual string SoftwareName
{
Get {return softwareName ;}
Set {softwareName = value ;}
}
Public string GetSoftwareName ()
{
Return softwareName;
}
}
// Ms product: office
Public class Office: Software
{
Public override string GetSoftwareName ()
{
This. SoftwareName = "Office ";
Return "Office ";
}
}
// Ms product: msn
Public class Msn: Software
{
Public override string GetSoftwareName ()
{
This. SoftwareName = "Msn ";
Return "Msn ";
}
}
/// <Summary>
/// Simple factory
/// </Summary>
Public class MsSoftwareFactory
{
Public static Software CreateSoftware (string softwareName)
{
Software msSoftware = null;
Switch (softwareName)
{
Default:
Break;
Case "office ":
MsSoftware = new Office ();
Break;
Case "msn ":
MsSoftware = new Msn ();
Break;
}
Return msSoftware;
}
}

/// <Summary>
/// Client call
/// </Summary>
Public class Client {
Static void Main (string [] args)
{
Software msSoftware = MsSoftwareFactory. CreateSoftware ("office ");
Console. WriteLine (msSoftware. GetSoftwareName ());
}
}

The preceding simple factory has a switch... case in a method body, which violates the principle of "open to extension and closed to modification", that is, "open-closed principle ". The following factory method overcomes the disadvantages of a simple factory and reduces the coupling between client programs and product objects.

Ii. Factory method(Design principle: dependent on abstraction, not specific classes)
Defines an interface used to create objects, so that the subclass determines which class to instantiate. The factory method delays the instantiation of a class to its subclass.
View plaincopy to clipboardprint?
Using System;
// Ms Product
Public class Software
{
Private string softwareName;

Public string SoftwareName
{
Get {return softwareName ;}
Set {softwareName = value ;}
}
Public virtual string GetSoftwareName ()
{
Return softwareName;
}
}
// Ms product: office
Public class Office: Software
{
Public override string GetSoftwareName ()
{
This. SoftwareName = "Office ";
Return "Office ";
}
}
// Ms product: msn
Public class Msn: Software
{
Public override string GetSoftwareName ()
{
This. SoftwareName = "Msn ";
Return "Msn ";
}
}

Interface ISoftwareFactory
{
Software CreateSoftware ();
}

/// <Summary>
/// Office Factory
/// </Summary>
Public class OfficeFactory: ISoftwareFactory
{
Public Software CreateSoftware ()
{
Return new Office ();
}
}

/// <Summary>
/// Msn Factory
/// </Summary>
Public class MsnFactory: ISoftwareFactory
{
Public Software CreateSoftware ()
{
Return new Msn ();
}
}

/// <Summary>
/// Client call
/// </Summary>
Public class Client {
Static void Main (string [] args)
{
ISoftwareFactory factory = new OfficeFactory ();
Software msSoftware = factory. CreateSoftware ();
Console. WriteLine (msSoftware. GetSoftwareName ());
}
}
Using System;
// Ms Product
Public class Software
{
Private string softwareName;

Public string SoftwareName
{
Get {return softwareName ;}
Set {softwareName = value ;}
}
Public virtual string GetSoftwareName ()
{
Return softwareName;
}
}
// Ms product: office
Public class Office: Software
{
Public override string GetSoftwareName ()
{
This. SoftwareName = "Office ";
Return "Office ";
}
}
// Ms product: msn
Public class Msn: Software
{
Public override string GetSoftwareName ()
{
This. SoftwareName = "Msn ";
Return "Msn ";
}
}

Interface ISoftwareFactory
{
Software CreateSoftware ();
}

/// <Summary>
/// Office Factory
/// </Summary>
Public class OfficeFactory: ISoftwareFactory
{
Public Software CreateSoftware ()
{
Return new Office ();
}
}

/// <Summary>
/// Msn Factory
/// </Summary>
Public class MsnFactory: ISoftwareFactory
{
Public Software CreateSoftware ()
{
Return new Msn ();
}
}

/// <Summary>
/// Client call
/// </Summary>
Public class Client {
Static void Main (string [] args)
{
ISoftwareFactory factory = new OfficeFactory ();
Software msSoftware = factory. CreateSoftware ();
Console. WriteLine (msSoftware. GetSoftwareName ());
}
}

The disadvantage of the factory method is that each time a product is added, a corresponding product factory class needs to be created, adding additional development workload.

Iii. Abstract Factory

Provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.
The most classic application of the abstract factory is the database access program. There are many discussions about this on the Internet. Not much. Here, I will continue to describe the scenario described in the opening section.
We know that office and msn can be divided into windows and mac versions based on different operating systems. There may also be linux and unix versions. In the final analysis, the products here involve product series issues, abstract Factory can solve this problem:
View plaincopy to clipboardprint?
Using System;

// Ms product: office
Public class Office
{
Private string softwareName;

Public string SoftwareName
{
Get {return softwareName ;}
Set {softwareName = value ;}
}
}

Public interface IOffice
{
Office GetOffice ();
}

// Microsoft Office for windows
Public class WinOffice: IOffice
{
Public Office GetOffice ()
{
Return new Office ();
}
}

// Ms for the macintosh version of Office
Public class MacOffice: IOffice
{
Public Office GetOffice ()
{
Return new Office ();
}
}

// Ms product: msn
Public class Msn
{
Private string softwareName;

Public string SoftwareName
{
Get {return softwareName ;}
Set {softwareName = value ;}
}
}

Public interface IMsn
{
Msn GetMsn ();
}

// Ms for windows msn
Public class WinMsn: IMsn
{
Public Msn GetMsn ()
{
Return new Msn ();
}
}

// Ms for the macintosh version msn
Public class MacMsn: IMsn
{
Public Msn GetMsn ()
{
Return new Msn ();
}
}

Interface ISoftwareFactory
{
IOffice CreateOffice ();
IMsn CreateMsn ();
}

/// <Summary>
/// Windows Factory
/// </Summary>
Public class WindowsFactory: ISoftwareFactory
{
Public IOffice CreateOffice ()
{
Return new WinOffice ();
}
Public IMsn CreateMsn ()
{
Return new WinMsn ();
}
}

/// <Summary>
/// Factory of the macintosh version
/// </Summary>
Public class MacFactory: ISoftwareFactory
{
Public IOffice CreateOffice ()
{
Return new MacOffice ();
}
Public IMsn CreateMsn ()
{
Return new MacMsn ();
}
}

/// <Summary>
/// Client call
/// </Summary>
Public class Client {
Static void Main (string [] args)
{
ISoftwareFactory factory = new WindowsFactory ();
IOffice office = factory. CreateOffice ();
IMsn msn = factory. CreateMsn ();
}
}

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/gisfarmer/archive/2009/03/15/3992382.aspx

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.