Design pattern: Summary and difference of simple factory, factory method, abstract factory (turn)

Source: Internet
Author: User

Simple factories, factory methods, abstract factories are all created patterns in design mode. Its main function is to help us to extract the instantiation of the object, optimize the architecture of the system, and enhance the expansibility of the system.

This article is my understanding of these three patterns after learning a summary and the difference between them.

Simple Factory

Factory classes in simple Factory mode typically use static methods to return different object instances by varying the parameters that are received.

If you do not modify the code, you cannot extend it.

Factory method

The factory approach is to provide a factory class for each product. Create different product instances from different factory instances.

Support for adding any product in the same hierarchy.

Abstract Factory

Abstract factories deal with the concept of product family. For example, every car company may have to produce cars, trucks, and buses at the same time, so every factory has a way to create cars, vans and buses.

It is easy to add new product lines to the product family concept, but it is not possible to add new products.

Summary

★ Factory mode, the important thing is the factory class, not the product class. Product classes can be in many forms, multiple layers of inheritance, or a single class are all possible. But to be clear, the factory-mode interface will only return an instance of a type, which should be noted when designing a product class, preferably with a parent class or an interface that is implemented together.

★ Using Factory mode, the returned instance must have been created by the factory and not taken from other objects.

★ Factory Mode The returned instance can not be newly created, and it is also possible to return the instance created by the factory.

Difference

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

Factory mode: Used to produce fixed products in the same grade structure. (Support for adding any product)
Abstract Factory: Used to produce all products of different product families. (for adding new products, powerless; Support for adding product families)

The above three plant methods have different levels of support in two directions: hierarchical structure and product family. So consider which method you should use depending on the situation.

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------

code example:

Looking at a lot of ways on the web about design patterns, some patterns look similar, but the nature is still very different. There is a clear distinction between factory methods and abstract factories like simple factories.
There is a better understanding of the example, in this with you to introduce:
1. If a backyard garden is only a vegetable type, then a simple factory can be used.
2. If there is a large variety of vegetables in the backyard. You have to use the factory method to abstract out the common things.
3. If the size of the rear garden is to be enlarged, such as one in the north and one in the south, so that the factory method cannot be achieved, it should be done with an abstract factory, a variety of plants, and a back garden.
So I personally think that the simple factory is a factory only produce a class of products, in the face of the specific class, the factory method can produce different products, the public method is abstracted, and then to create a variety of products. Abstract factories draw together several products, and abstract the interdependent objects As long as these interfaces are implemented, different products can be obtained.
Specific examples:
1. Simple factory:
Using System;
public interface ICar
{
void run ();
}

public class Bmwcar:icar
{
public void Run ()
{
Console.WriteLine ("Bmwcar Run");
}
}

public class Benzcar:icar
{
public void Run ()
{
Console.WriteLine ("Benzcar Run");
}
}

public class Driver
{
public static ICar Drivercar (String cartype)
{
Switch (cartype)
{
Case "Bmwcar": Return new Bmwcar ();
Case "Benzcar": Return new Benzcar ();
Default:throw new Exception ();
}
}
}

public class Client
{
public static void Main ()
{
ICar MyCar = Driver.drivercar ("Benzcar");
Mycar.run ();
Console.read ();
}
}

Experience: The advantage is that as long as the common interface can realize the different ways of car running. But the disadvantage is to determine which kind of car, causing to modify the driver class
2. Factory Method:
Using System;
public interface ICar
{
void run ();
}

public class Bmwcar:icar
{
public void Run ()
{
Console.WriteLine ("Bmwcar Run");
}
}

public class Benzcar:icar
{
public void Run ()
{
Console.WriteLine ("Benzcar Run");
}
}
Public abstract class Driver
{
Public abstract ICar Drivercar ();
}

public class Bmwdriver:driver
{
public override ICar Drivercar ()
{
return new Bmwcar ();
}

}
public class Benzdriver:driver
{
public override ICar Drivercar ()
{
return new Benzcar ();
}
}

Class Client
{
public static void Main ()
{
Driver mydriver = new Benzdriver ();
ICar MyCar = Mydriver.drivercar ();
Mycar.run ();
Console.read ();
}
}

Experience: The advantages are in line with the open-closed principle (OCP), the overall can not see any shortcomings.

3. Abstract Factory:

Using System;

public interface Ibusinesscar
{
void run ();
}

public interface Isportcar
{
void run ();
}

public class Bmwbusinesscar:ibusinesscar
{
public void Run ()
{
Console.WriteLine ("Bmwcar Run");
}

}

Public class Benzbusinesscar:ibusinesscar
{
    public void run ()
     {
        console.writeline ("BenzBusinessCar run");
    }
}

public class Bmwsportcar:isportcar
{
public void run ()
{
Console.WriteLine ("Bmwsportcar Run ");
}
}

public class Benzsportcar:isportcar
{
public void run ()
{

Console.WriteLine ("Benzsportcar Run");
}
}

public interface Idriver
{
Ibusinesscar Businesscardriver ();
Isportcar Sportcardriver ();
}

public class Bmwdriver:idriver
{
Public Ibusinesscar Businesscardriver ()
{
return new Bmwbusinesscar ();
}
Public Isportcar Sportcardriver ()
{
return new Bmwsportcar ();
}
}

public class Benzdriver:idriver
{
Public Ibusinesscar Businesscardriver ()
{
return new Benzbusinesscar ();
}

Public Isportcar Sportcardriver ()
{
return new Benzsportcar ();
}
}

Class Client
{
public static void Main ()
{
Idriver mydriver =new benzdriver ();
Isportcar MyCar = Mydriver.sportcardriver ();
Mycar.run ();
Console.read ();
}

}

Tip: Abstract methods seem to have reached a perfect level. Abstract the public methods of driving a Mercedes-Benz driver and driving a BMW driver, and create different classes for different drivers, whichever driver you drive with. The only thing they have in common is driving.

Design pattern: Summary and difference of simple factory, factory method, abstract factory (turn)

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.