"javascript Design pattern" Lesson Three factory method mode

Source: Internet
Author: User

In the development of the project often encountered on the Web page to display some of the same style but the content of different requirements, and this demand is still dynamic, as the changes in the requirements of the content may also be changing, we recommend: Factory mode: by abstracting the product class, it creates a business that is primarily used to create instances of Multi-Class Products.
For example, there is a need to do two ads, so that we can directly define two classes to achieve
  
 
  1. var Java = function (content) {
  2. //将内容备份到content中,以备日后使用
  3. this.content = content;
  4. //创建对象时,通过闭包直接执行,讲内容按照需求的模式插入到页面
  5. (function () {
  6. var div = document.createElement(‘div‘);
  7. div.innerHTML = content;
  8. div.style.color = ‘green‘;
  9. document.getElementById(‘container‘).appendChild(div);
  10. })(content);
  11. }
  12. var Php = function (content) {
  13. //将内容备份到content中,以备日后使用
  14. this.content = content;
  15. //创建对象时,通过闭包直接执行,讲内容按照需求的模式插入到页面
  16. (function () {
  17. var div = document.createElement(‘div‘);
  18. div.innerHTML = content;
  19. div.style.color = ‘blue‘;
  20. document.getElementById(‘container‘).appendChild(div);
  21. })(content);
  22. }

The requirements above have the same pattern, which is somewhat similar to the simple factory pattern, and we can use the Simple factory model to Achieve:
  
 
  1. var jobFactory = function (type, content) {
  2. switch (type){
  3. case ‘java‘:
  4. return new Java(content);
  5. case ‘php‘:
  6. return new Php(content);
  7. }
  8. }

This is also required to meet the requirements, but also to make the code look more hierarchical structure, but if the demand has changed, the number of published ads increased, the content and color has changed correspondingly, if we still use the simple factory model, we will find that we need to modify two places, Add a new class and then modify the simple Factory mode method, so that the simple Factory mode is not simplified and becomes more complex. It is recommended to use the factory Mode.
The so-called factory pattern is that the work of actually creating objects is left to the subclasses, so that the class becomes an abstract class.
  
 
  1. //安全模式创建的工厂类
  2. var Factory = function (type, content) {
  3. if(this instanceof Factory){
  4. var s =new this[type](content);
  5. return s;
  6. }else{
  7. return new Factory(type,content);
  8. }
  9. }
  10. //工厂原型中设置创建对象的基类
  11. Factory.prototype = {
  12. Java : function (content) {
  13. //将内容备份到content中,以备日后使用
  14. this.content = content;
  15. //创建对象时,通过闭包直接执行,讲内容按照需求的模式插入到页面
  16. (function () {
  17. var div = document.createElement(‘div‘);
  18. div.innerHTML = content;
  19. div.style.color = ‘green‘;
  20. document.getElementById(‘container‘).appendChild(div);
  21. })(content);
  22. },
  23. Php : function (content) {
  24. //将内容备份到content中,以备日后使用
  25. this.content = content;
  26. //创建对象时,通过闭包直接执行,讲内容按照需求的模式插入到页面
  27. (function () {
  28. var div = document.createElement(‘div‘);
  29. div.innerHTML = content;
  30. div.style.color = ‘green‘;
  31. document.getElementById(‘container‘).appendChild(div);
  32. })(content);
  33. }
  34. }

So if there is a new similar requirement in the future, we just need to add the corresponding base class implementation in the factory Prototype. This is the essence of the factory model.

From for Notes (Wiz)

"javascript Design pattern" Lesson Three factory method 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.