"Design mode" C # version of the three major factories of simple factory, factory method and abstract factory

Source: Internet
Author: User

Introduction

Simple factories, factory methods, and abstract factories all belong to the creation pattern in design patterns. Its main function is to help us to abstract the instantiation part of the object, optimize the architecture of the system, and enhance the extensibility of the system. This blog is the author of the completion of these three models after a small sum

Simple Factory

Factory classes in simple Factory mode typically use static methods that return different object instances by receiving different parameters. cannot be extended without modifying the code


Advantage: The client can dispense with the responsibility of directly creating the product object, but simply "consume" the product. The simple factory model realizes the division of responsibility by this approach

disadvantage: because the factory class centralizes the creation logic of all instances, it violates the cohesion well-structured classes responsibility allocation principle, centralizes all the creation logic into a factory class, the classes it can create can only be considered beforehand, and if you need to add a new class, you need to change the factory class.

Factory Method

The factory approach is to provide a factory class for each product. Create a different product instance from a different factory instance. Support for adding arbitrary products in the same hierarchy


Benefits: allows the system to introduce new products without modifying the specific factory role

disadvantage: As each addition of a product, it is necessary to add a product factory class, increased the amount of additional development

Abstract Factory

Abstract Factory is to deal with the concept of product family. It is easy to add new product lines to the concept of product family, but it is not possible to add new products. 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, trucks and buses.


Advantage: Provides an interface to the client that enables the client to create product objects in multiple product families without having to specify the product specific type

Disadvantage: Adding a new product hierarchy is complex, the need to modify the abstract factory and all the specific factory class, the "open and closed principle" support is tilted

Simple Factory and factory methods

The factory method pattern is a further abstraction and generalization of the simple factory model, because of the use of polymorphism, the factory method pattern maintains the advantage of the simple factory model, and overcomes its shortcoming, completely conforms to the opening and closing principle

Factory method and abstract factory

1. Factory method

An abstract product class that can derive multiple specific product classes. An abstract factory class that can derive multiple specific factory classes. Only one instance of a specific product class can be created for each specific factory class

2. Abstract Factory

Multiple abstract product classes, each abstract product class can derive multiple specific product classes. An abstract factory class that can derive multiple specific factory classes. Each specific factory class can create instances of multiple specific product classes

3. The difference between factory method and abstract factory

The factory method pattern has only one abstract product class, while the abstract factory pattern has multiple. Factory-mode-specific factory classes can create only one instance of a specific product class, while abstract factory patterns create multiple Epilogue

About the three Factory mode code here I do not too much to show that the evening there are many beginner's tutorials, most of them are similar, there is a need to search the Internet. The author is currently just a beginner of design mode, how to apply these patterns in the project is not too understanding, in the latter part of the in-depth study,

This article is just a summary of the three major factory model, for the three factory model has a clear understanding. Changes in UML diagrams can also be seen from simple factory patterns to factory method patterns to abstract factory patterns.


This article is just a basic knowledge to do a small summary, do not delve into. If there is a difference, please correct your opinion.

all contents of this article are original author, if reproduced, please indicate the source

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.