JS Factory mode, simple mode, abstract mode

Source: Internet
Author: User

A simple factory model , also known as a static factory method, is a factory object that decides to create an instance of a Product object class, primarily to create the same class of objects. In fact, the idea of Factory mode is mainly to extract the same/similar objects or classes, so that you can avoid writing too many repetitive code. Let's take a look at the following examples:

function Creatbook (name, time, type) {
Create an object and extend the properties and methods on the object
var o =new Object ();
O.name = name;
O.time = time;
O.type =type;
O.getname = function () {
Console.log (THIS.name)
}
Returns an object

Return o;//This must not be lost.
}
var Book1 = Creatbook (' js book ', "JS")
var book2 = Creatbook (' CSS book ', ' Page ', ' CSS ')
Book1.getname ();
Book2.getname ();

When we have many kinds of books, each book has the author, the title, the publication time, etc., if you want to create an object for each book, then we will create a lot of duplicate code. We can build an object that contains the properties common to these books, and then we call the function by creating an instance of each book.

Factory Method Mode : The creation of a business by abstracting the product class is primarily responsible for creating instances of the multi-class product. is to defer the actual creation of the object to the subclass, the factory method pattern is aimed at the time when more requirements, mainly dealing with the creation of multi-class objects, by adding classes in the factory prototype, and then in use, directly in the factory to find it.

//Factory classes created in Safe mode        varFactory =function (type, content) {if( Thisinstanceof Factory) {                vars =New  This[TypeError] (content); returns; } Else {                return NewFactory (type, content)}} //set the base class for creating all types of data objects in a factory prototypeFactory.prototype ={java:function (content) {}, Js:function (content) {}, Ui:function (content) {  This. Content =content; (function (content) {vardiv = document.createelement ('Div'); Div.innerhtml=content; Div.style.border='1px solid Red'; document.getElementById ('Container'). appendchild (Div)}) (content)}, php:function (content) {}} for(vari =6; I >=0; i--) {Factory (S[i].type, s[i].content)}

Abstract Factory mode: An abstract class is a class that declares but cannot be used, and when you use it, you get an error.

First, an abstract class is declared as a parent class to summarize the characteristics required by a particular class of products, and inheriting the subclass of the parent class requires implementing the methods declared in the parent class to implement the functionality declared in the parent class, as shown in the following example:

/** * Implement subtype class inheritance of abstract classes of supertype types in factory classes * @param subtype class to inherit * @param abstract class in Supertype factory class type*/ConstVehiclefactory =function (subtype, supertype) {if(typeofVehiclefactory[supertype] = = ='function') {function F () { This. Type ='Vehicle'} f.prototype=NewVehiclefactory[supertype] () subtype.constructor=subtype Subtype.prototype=NewF ()//because subclass subtype not only need to inherit the prototype method of Supertype corresponding class, but also inherit its object property}Else Throw NewError ('the abstract class does not exist')}vehiclefactory.car=function () { This. Type ='Car'}vehiclefactory.car.prototype={getprice:function () {return NewError ('abstract methods are not available')}, Getspeed:function () {return NewError ('abstract methods are not available')  }}ConstBMW =function (price, speed) { This. Price = Price This. Speed =Speed } Vehiclefactory (BMW,'Car')//inherit car abstract classBMW.prototype.getPrice = function () {//Overwrite GetPrice methodConsole.log (' BWM price is${ This. Price} ')}bmw.prototype.getspeed=function () {console.log (' BWM speed is${ This. Speed} ')}ConstBaomai5 =NewBMW ( -, About) Baomai5.getprice ()//BWM Price isBaomai5 instanceof Vehiclefactory.car//true

JS Factory mode, simple mode, abstract mode

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.