PHP design mode JS Implementation Viewer Mode C Viewer mode Android Viewer mode iOS Viewer module

Source: Internet
Author: User
Observer Mode Observer

Achieving the goal: transform the Select box to change the content area and ad area
General Ideas
1: Judging the selected value
2: Order down, modify the content area according to the value
3: Edit the ad area again.

if'男'{   内容区背景变灰   广告区内容变成男人话题}elseif'女'){   内容区背景变粉   广告区内容变成女人话题}

So what are the questions:
1: Let you choose to cause 3 changes in the area, whether to modify the If/elseif section
2: If you choose female style, but do not change the powder, now popular purple, whether to modify if ElseIf part?
The problem is that the control logic is heavily coupled to the manipulated object.

This is the need for observer mode
JS Implementation Viewer Mode

We let the div object observe the change of select, and the two objects are notified when select changes. and causes these 2 objects to change, realizes the observer pattern.

The bottom is the solution effect
The Select object is responsible for attach,detach,notifyobservers, decoupled from the specific object changes.
1.index.html

<body><Selectname="id=" "><optionvalue=' male '>Men's Style 
     Option
 ><optionvalue=' female '>Ladies ' style 
     Option
 > 
     Select
 ><inputtype=' button 'value= 'Watch learning area 'onclick ="T1 ();" ><inputtype=' button 'value= 'no longer viewing the learning area 'onclick ="T2 ();" ><divid=' content '>I am the content 
     Div
 ><divid=' ad '>I'm an ad. 
     Div
 ><divid=' study '>I'm a study area. 
     Div
 > 
     Body
 >

JS part of 2.index.html

<script>//Get Objectsvarsel = document.getElementsByTagName (' SELECT ')[0];//Definition methodsel.observers={};//Add a methodSel.attach = function(key,obj){ This. observers[key] = obj; }//Remove a methodSel.detach = function(key){Delete This. Observers[key]; }//NotificationSel.onchange = Sel.notify = function(){ for(varKeyinch This. Observers) { This. Observers[key].update ( This);//Go to call the object's Update method}    }//ClientvarContent = document.getElementById (' content ');varAD = document.getElementById (' ad ');//Content area Change effectContent.update = function(Observer){if(observer.value==' Male '){ This. style.backgroundcolor=' Gray '; }Elseif(observer.value==' Female '){ This. style.backgroundcolor=' Pink '; }    }//Advertising area Change effectAd.update = function(Observer){if(observer.value==' Male '){ This. innerhtml=' car '; }Elseif(observer.value==' Female '){ This. innerhtml=' Weight loss '; }    }//Determine who will monitor the changeSel.attach (' content ', content); Sel.attach (' ad ', AD); Study = document.getElementById (' study '); Sel.attach (' study ', study);//default to Observation learning areaStudy.update = function(Observer){if(observer.value==' Male '){ This. innerhtml=' Learning computer '; }Elseif(observer.value==' Female '){ This. innerhtml=' Learn to lose weight '; }    }//Increase the observation of the learning area functiont1(){Sel.attach (' study ', study); }//Cancel the observation of the learning area functionT2(){Sel.detach (' study '); } 
     Script
 >

Summarize:

    1. When another observer joins, just add one.
    2. Reduced coupling between subject objects and observer objects
    3. A good design pattern does not go directly into your code, but into your brain.

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above introduces the PHP design mode JS Implementation of the observer pattern, including the observer pattern, design patterns of content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.