Understanding the design patterns in JavaScript (1)

Source: Internet
Author: User

Introduction:Reliable Design Patterns are the cornerstone of maintenance software. If you have been in a technical interview, you may be asked about design patterns. In the following guide, we will learn some design patterns that can be used today.

What is the design model?

Simply put, the design pattern is a software solution for reusing specific types of problems. These problems are often encountered during software development. Through years of practice, experts have summarized some methods for some similar issues. These methods are encapsulated into a design model, so:

A pattern is a proven solution for solving software development problems.

Patterns are scalable because they are often structured and you need to follow certain rules.

Similar problems can be reused.

In the next tutorial, we will provide some examples of the design pattern.

Types of design patterns

In software development, design patterns are generally divided into several categories. In this tutorial, we will focus on the following three types:

1. The Creation Mode focuses on building objects or classes. Object creation sounds simple (in some cases), but large applications need to control the object creation process.

2. The structural design model focuses on the relationship between management objects so that applications can be scaled. The key aspect of the structural model is to ensure that some changes in the application do not affect other parts.

3. behavior patterns focus on communication between objects

You may still think that there is a problem after reading the description of these profiles. This is normal. Once we have read the in-depth introduction of these models, the problem will become clearer, so let's take a look.

Note:

When we read the design pattern, you often mention classes and objects. This is confusing because JavaScript does not have a true "class" structure. A more appropriate term is "data type ".

Data Types in JavaScript:

JavaScript is an object-oriented language. An object inherits from other objects. This concept is known for prototype inheritance. A data type can be created through constructors, just like:

 
 
  1. function Person(config) {  
  2.     this.name = config.name;  
  3.     this.age = config.age;  
  4. }   
  5.    
  6. Person.prototype.getAge = function() {  
  7.     return this.age;  
  8. };   
  9.    
  10. var tilo = new Person({name:"Tilo", age:23 });  
  11. console.log(tilo.getAge()); 

When defining a method in the Person data type, pay attention to the use of prototype. Because multiple Person objects reference the same prototype, getAge () is allowed () this method can be shared by all Person-type instances. Instead of redefining each instance, any data type inherited from Person can access the getAge () method.


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.