Based on Singleton in the design mode, JS implements the function of adding, deleting, modifying, and querying data by encapsulation. The design mode is singleton.

Source: Internet
Author: User

Based on Singleton in the design mode, JS implements the function of adding, deleting, modifying, and querying data by encapsulation. The design mode is singleton.

This article describes how to add, delete, modify, and query data by using the Singleton mode in the design mode of JS. We will share this with you for your reference. The details are as follows:

Singleton Mode

The core structure of the singleton mode only contains a special class called Singleton. The Singleton mode ensures that only one instance is available for one class in the system.

The initial definition of Singleton mode appears in design patterns (aisavisli, 1994): "ensure that a class has only one instance and provide a global access point to it ."

Singleton mode definition:"A class has only one instance and is self-instantiated to provide to the entire system."

var Singleton = (function(){ SingletonClass() { } var singleton = null; return {  getInstance: function() {   if (singleton == null) {  singleton = new singletonClass();   } else {  return singleton;   }  } }})();Singleton.getIntance();

The frontend often uses some interface-related addition, deletion, modification, and query asynchronous operations. For example, I often add the modify/delete function when operating the data list. Some solutions use synchronous (refresh the page), and the user experience is better with asynchronous;

The Code is as follows:

Added features

$ (". Add "). click (function () {$. ajax ({type: "post" dataType: "json", url: "http://www.bkjia.com/", data: {name: "csdn blog", dir: "web Front-end "}, success: function (result) {if (result. status) {alert ("added successfully! ")} Else {alert (" failed to add ") }}, error: function () {alert (" added asynchronous, please add or contact the technical administrator ");}});});

Delete feature

$ (". Del "). click (function () {$. ajax ({type: "post" dataType: "json", url: "http://www.bkjia.com/", data: {id: "1"}, success: function (result) {if (result. status) {alert ("deleted successfully! ")} Else {alert (" failed to delete ") }}, error: function () {alert (" added asynchronous, please add or contact the technical administrator ");}});});

The above two code snippets briefly describe the JS Code for adding and deleting functions. Some people found that they have something in common, that is, some of the ajax requests are the same, and what if the delete function is used elsewhere ?, Write the same code elsewhere. Uncomfortable

Let's improve it.

Var SingletonCRUD = (function () {SingletonClass () {} SingletonClass. prototype = {constructor: SingletonClass, add: function (data) {$. ajax ({type: "post" dataType: "json", url: "http://www.bkjia.com/", data: data, success: function (result) {if (result. status) {alert ("added successfully! ")} Else {alert (" failed to add ") }}, error: function () {alert (" added asynchronous, please add or contact the technical administrator ") ;}}}, remove: function (data) {$. ajax ({type: "post" dataType: "json", url: "http://www.bkjia.com/", data: data, success: function (result) {if (result. status) {alert ("deleted successfully! ")} Else {alert (" failed to delete ") }}, error: function () {alert (" added asynchronous, please add or contact the technical administrator ") ;}}}}var singleton = null; return {getInstance: function () {if (singleton = null) {singleton = new singletonClass () ;}else {return singleton ;}}}) (); var curd = SingletonCRUD. getIntance (); $ (". add "). click (function () {var data = {"name": "name"}; curd. add (data) ;}); $ (". del "). click (function () {var data = {"id": 1}; curd. remove (data );});

The Singleton instance is often used as some Tool classes;

Advantages of the design mode: strong coupling, readability, and clear code structure;

In the preceding example, the acquired data (click Event function) in the click event is separated from the operation data (ajax request;

The code after the singleton mode is optimized:

Var SingletonCRUD = (function () {SingletonClass () {} SingletonClass. prototype = {constructor: SingletonClass, ajax: function (url, data success) {$. ajax ({type: "post" dataType: "json", url: url, data: data, success: success, error: function () {alert ("added asynchronous, please add or contact the technical administrator ") ;}}, add: function (data) {this. ajax ("http://www.bkjia.com/", data, function (result) {if (result. status) {alert ("New Added successfully! ")} Else {alert (" failed to add ") }}, remove: function (data) {this. ajax ("http://www.bkjia.com/", data, function (result) {if (result. status) {alert ("deleted successfully! ")} Else {alert (" failed to delete ") }}) ;}var singleton = null; return {getInstance: function () {if (singleton = null) {singleton = new singletonClass () ;}else {return singleton ;}}}})();

The ajax method in SingleClass is also equivalent to a Facade, which hides internal details and exposes an interface;

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.