JavaScript design mode 8

Source: Internet
Author: User

Bridging mode (isolates abstraction from its implementation so that they vary independently)

function Sendinfo (Element) {    var id=element.id;    Ajax ("GET", "info.json?id=" +id,function(result) {        //...     });     // ...};

The above example Ajax request and Sendinfo function can be split apart

function Sendinfo (Element) {    var id=element.id,    callback=function(Result) {        //...     };    Sendinfobridge (Id,callback);} function Sendinfobridge (id,callback) {    ajax ("GET", "info.json?id=" +id,function(result) {        callback (result);    });}

Combined mode (a command that fires complex or recursive behavior on multiple objects), combining objects and leaf objects

Condition One: There is a group of objects organized into some hierarchical system

Condition Two: You want to manipulate this batch of objects or some of them

Group objects

varComposite=NewInterface (' Composite ', [' Add ', ' Remove ', ' getchild ']);vargalleryitem=NewInterface (' Galleryitem ', [' Hide ', ' show ']);vardynamicgallery=function(ID) { This. children=[];  This. element=document.createelement (' div ');  This. element.id=ID;  This. Element.classname= ' Dynamic-gallery ';} Dynamicgallery.prototype={add:function(Child) {interface.ensureimplements (Child,composite,galleryitem);  This. Children.push (child);  This. Element.appendchild (Child.getelement ()); }, remove:function(child) { for(varNode,i=0;node= This. Getchild (i); i++){            if(node==Child ) {                 This. Children.splice (i,1);  Break; }        }         This. Element.removechild (Child.getelement ()); }, Getchild:function(i) {return  This. Children[i]; }, Hide:function(){         for(varNode,i=0;node= This. Getchild (i); i++) {node.hide (); }         This. element.style.display= ' None '; }, Show:function(){         This. element.style.display= ' Block ';  for(varNode,i=0;node= This. Getchild (i); i++) {node.show (); }}, GetElement:function(){        return  This. Element; }};

Leaf Object

varGalleryimage=function(SRC) { This. Element=dicument.createelement (' img ');  This. Element.classname= ' Gallery-image ';  This. element.src=src;} Galleryimage.prototype={add:function() {}, remove:function() {}, Getchild:function() {}, Hide:function(){         This. element.style.display= ' None '; }, Show:function(){         This. element.style.display= '; }, GetElement:function(){        return  This. Element; }};
vartopgallery=NewDynamicgallery (' Top-gallery '); Topgallery.add (NewGalleryimage ('/img/image-1.jpg ')); Topgallery.add (NewGalleryimage ('/img/image-2.jpg ')); Topgallery.add (NewGalleryimage ('/img/image-3.jpg '));varvacationphotos=NewDynamicgallery (' Vacation-photos '); for(vari=0;i<30;i++) {Vacationphotos.add (NewGalleryimage ('/img/vac/image-' +i+ '. jpg '));} Topgallery.add (Vacationphotos); Topgallery.show (); Vacationphotos.hide ();

JavaScript design mode 8

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.