JavaScript design mode-Abstract Factory mode

Source: Internet
Author: User

Abstract Factory mode, which is a kind of creation pattern, is actually a method of implementing subclass inheriting parent class, in this method, by passing subclasses and inheriting the name of the parent class, and adding a judgment to the existence of the abstract class in the abstract factory method, if present, the subclass inherits the method of the parent class. The subclass then inherits through a parasitic type.

Abstract Factory is used to create subclasses, itself can be regarded as a class family, it has developed the structure of subclasses, let subclasses inherit abstract factory methods and properties, so the abstract Factory mode can create multi-class objects.

Example: Defining an abstract Factory method:

varVehiclefactory =function(Subfun, subclass) {if(typeofVehiclefactory[subclass] = = = ' function ') {        functionF () {} F.prototype=NewVehiclefactory[subclass] () subfun.constructor=Subfun Subfun.prototype=NewF () Console.log (Subfun.prototype)}Else {        Throw NewError (' class object not created ')    }}//Car abstract classVehiclefactory.cars =function () {     This. Type = ' car '}vehiclefactory.cars.prototype={getprice:function () {        return NewError (' abstract method cannot be called, please customize subclass method ')    }}

To generate subclasses with an abstract factory:

var function (Price) {    the. Price =' Cars 'function  () {       Returnthis. Price;} var New Jili (200000); Console.log (Jili.getprice ());     // 200000

JavaScript design mode-Abstract Factory mode

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.