Abstract Factory of design patterns

Source: Internet
Author: User

The factory method mode solves the problem of heavy factory responsibilities in the simple factory mode by introducing the factory level structure. However, because each factory in the factory method mode only produces one type of product, there may be a large number of factory classes in the system, which will inevitably increase the overhead of the system. At this time, we can consider forming related products into a "Product Family" which is uniformly produced by the same factory. This is the basic idea of the abstract factory model that we will learn in this article.

Abstract Factory (Abstract Factory mode): provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.




One day after many years, when you accompany your lover to go shopping malls to purchase household appliances for your home, is it a little dizzy to face the wide variety of products? Next we will talk about household appliances:



VcrHy/weight + nP87rN1 + 6 + weight/weight + 6087XEx/weight + a21LbguPay + weight/weight + dPQttTP88qxo6yz6c/weight + weight "http://www.2cto.com/uploadfile/Collfiles/20140617/20140617083751139.jpg" alt = "\">




Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace abstract Factory {// abstract Factory public abstract class AbstractFactory {public abstract Aircondition CreateAircondition (); public abstract extends er createcondier ();} // specific factory public class HaierFactory: abstractFactory {public override Aircondition CreateAircondition () {return new HaierAircondition ();} Public override aggreger createaggreger () {return new haierexterner () ;}} public class MedieaFactory: AbstractFactory {public override Aircondition CreateAircondition () {return new MideaAircondition ();} public override Washer CreateWasher () {return new MideaWasher () ;}// abstract product class public abstract class Aircondition {public abstract void Show ();} public abstract class Washer {public abstract vo Id Show () ;}// specific product class public class HaierAircondition: Aircondition {public override void Show () {Console. WriteLine ("Haier air conditioner: Haier technology, world famous brand! ") ;}} Public class HaierWasher: extends er {public override void Show () {Console. writeLine ("Haier washing machine: intelligent perception cleaning: Stop you control your life I control your clothes") ;}} public class MideaAircondition: Aircondition {public override void Show () {Console. writeLine ("Midea air conditioning: Wonderful Life created by me can be more beautiful, innovative, and beautiful air conditioning");} public class MideaWasher: extends er {public override void Show () {Console. writeLine ("meide washing machine: the original life can be more beautiful") ;}} class Program {static void Main (string [] args) {AbstractFactory Hfactory = new HaierFactory (); aircondition Haircon = Hfactory. createAircondition (); Haircon. show (); Washer Hwasher = Hfactory. createcycler (); hcycler. show (); Console. writeLine (); AbstractFactory Mfactory = new MedieaFactory (); Aircondition Maircon = Mfactory. createAircondition (); Maircon. show (); consumer er Mwasher = Mfactory. createaggreger (); Mwasher. show (); Console. read ();}}}


System behavior.
(2) When multiple objects in a product family are designed to work together, it ensures that the client always uses only objects in the same product family.
(3) it is convenient to add a new product family. You do not need to modify the existing system and comply with the "Open and Close principle ".


Disadvantages:
It is difficult to add a new product level structure. You need to make major changes to the original system or even modify the abstract layer code. This will obviously cause great inconvenience, it violates the "open and closed principle ".




Applicability
(1) A system must be independent of the creation, combination, and representation of its products.
(2) A system must be configured by one of multiple product generations.
(3) When you want to emphasize the design of a series of related product objects for joint use.

(4) When you provide a product library, you only want to display their interfaces instead of implementations.









Related Article

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.