"JavaScript design pattern" Lesson Two magic Magician-simple Factory mode

Source: Internet
Author: User

The so-called simple factory pattern is a way to decide exactly which instance of the class to create, and these instances often have the same interface. Simple Factory mode mainly use a few situations (add personal Understanding) 1, the construction of the object is regular, most of the same only a few different, duplicate code more
2, need to rely on the specific environment to create different instances
3. Handle large numbers of small objects with the same attributes
In particular, we look at a small example: we often encountered in the development process login module, the first to see the following specific requirements: 1, the user entered in the input box does not conform to the specification, you need to define a prompt box: "You entered the content does not conform to the specification, please re-enter"
2, user login Prompt user does not exist, prompting the user "account does not exist, please re-enter, if there is no account, you can click the" Register "button to register", equivalent to a conform prompt box
3, the user login success, prompted the user "login success, please enter your mood today."
For the above requirements, the first equivalent of a prompt box, the second equivalent to the conform box, with the user there is a choice of interaction, the third equivalent of the input conform box, for three kinds of requirements, we can design the following classes to implement the function
  
 
  1. //定义一个提示类
  2. var LoginTip = function (context) {
  3. this.context = context;
  4. }
  5. LoginTip.prototype.show = function(){
  6. //显示提示框
  7. }
  8. //定义一个conform类
  9. var LoginConform = function (context) {
  10. this.context = this.context;
  11. }
  12. LoginConform.prototype.show = function () {
  13. //显示确认框
  14. }
  15. //定义一个输入conform框
  16. var LoginInputConform = function(context){
  17. this.context = context;
  18. }
  19. LoginInputConform.prototype.show = function(){
  20. //显示输入确认框
  21. }
In this way we only need to call the corresponding class and then new one corresponding instance to achieve the function requirement when we use the box. But in the system's team development, other places if has the similar function to call the corresponding method to implement the same function, such code cannot achieve the reuse function, and the other people also need to know your method's implementation then is good to call the corresponding method to implement the corresponding function,   At this point, the corresponding function can be realized by the simple Factory mode. First of all, the other developers in the team need to implement the corresponding function, they just need to know a function method, and then through this function method can create the desired object so it would not be better. This allows us to add a functional approach to achieve the above requirements:
  
 
  1. //定义一个方法 根据用户输入的需求来获得对应的对象
  2. var PopFactory = function(name,context){
  3. switch (name){
  4. case ‘tip‘:
  5. return new LoginTip(context);
  6. case ‘conform‘:
  7. return new LoginConform(context);

  8. case ‘inputConform‘:
  9. return new LoginInputConform();
  10. }
  11. }
So that we can implement just remember popfactory this method, and then pass in the corresponding parameters can be implemented to create the object to be returned
But looking at the three requirements above, we found that there are a lot of the same three classes, so we can abstract them out for common use, and then not similar ones for processing.
  
 
  1. var createPop = function(type,txt){
  2. var o = new object();
  3. o.content = txt;
  4. o.show = function(){
  5. //显示方法
  6. }
  7. if(type == ‘tip‘){
  8. //提示框差异的部分
  9. }
  10. if(type == ‘conform‘){
  11. //conform差异的部分
  12. }
  13. if(type == ‘inputConform‘){
  14. // inputConform差异的部分
  15. }
  16. return o;
  17. }



From for notes (Wiz)

"JavaScript design pattern" Lesson Two magic Magician-simple 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.