10. Adapter mode in Javascript design mode -- Adapter

Source: Internet
Author: User

 

10. Adapter mode for JavaScript design mode ---- Adapter

The adapter mode is a very practical design mode. This article will introduce this design mode through examples.

The purpose of the adapter mode is to convert an interface of a class to another interface that the customer wants.

The adapter mode allows the classes that cannot work together due to interface incompatibility to work together. If you want to understand it more easily, you can understand the adapter as a middleware, through which two things that could not have been linked together can be truly linked.

Preface
  1. I have a Nokia e63 mobile phone with a 2 GB memory card but no data cable. What should I do if I want to copy a few software from my computer and put it into the memory card of my mobile phone?
    There are mobile phones, cards, computers, and software I want, but there is no data cable. How can I do?
  2. I used my friend's digital camera to take a lot of photos in Tiananmen. They were all in the camera's memory card, but my friend didn't give me a data cable (-_-!!! There is no data cable.) How can I take the photo out of the camera?

Occasionally, I am a programmer. Fortunately, I know that there is an adapter. For the above situation, I only need to buy a multi-port card reader (¥10.00) in the supermarket next to it ), the problem is completely solved.

The card reader mentioned above is actually an adapter used.

Adapter Mode

Next, I will use the program to simulate the whole process of the adapter to solve the problem.

Memory card
// Define the memory card. There are three operations: one is the insert slot (dedicated slot of the memory card), the other is the fetch slot, and the other is the keep data var imemorycard = new interface ("imemory ", ["insertslot", "outslot", "savedata"]); var memorycard2gb = function () {// The required slot is the dedicated slot of the memory card this. slot = "memory card slot" ;}; implements (memorycard2gb, imemorycard); memorycard2gb. prototype = {insertslot: function (slotplug) {If (slotplug. slot! = This. slot) {alert ("slot mismatch");} else {alert ("the memory card has been inserted into the slot") ;}, outslot: function () {alert ("the memory card has been removed from the slot") ;}, savedata: function (data) {// save data... alert ("data is successfully stored on the storage card ");}};
Nokia e63 mobile phone
// Defines the Nokia e63 mobile phone. It has two operations: one is to install (insert) the memory card, the other is to uninstall (remove) the memory card var inokiae63 = new interface ("inokiae63 ", ["installmemorycard", "uninstallmemorycard"]); var nokiae63 = function () {// memory card dedicated slot this. slot = "memory card slot" ;}; implements (nokiae63, inokiae63); nokiae63.prototype = {installmemorycard: function (memcard) {If (memcard. slot! = This. slot) {alert ("the phone cannot recognize this memory card");} else {alert ("the phone already has a memory card inserted") ;}, uninstallmemorycard: function (memcard) {alert ("the memory card in the mobile phone has been taken out ");}};
Computer
// Define the computer, which has three operations: one is to connect the hardware device, the other is to disconnect the hardware device, and the other is to output data to the external device var icomputer = new interface ("icomputer ", ["connecthardware", "disconnecthardware", "outputdata"]); var computer = function () {// USB 2.0 socket this. plug = "USB 2.0" ;}; implements (compute, icomputer); computer. prototype = {connecthardware: function (usbplug) {If (usbplug. plug! = This. plug) {alert ("this computer does not recognize this external device");} else {//... alert ("identifying external devices connected to computer") ;}}, disconnecthardware: function (usbplug) {If (usbplug. plug! = This. plug) {alert ("Unrecognized external device");} else {//... alert ("external devices have been safely removed from the computer") ;}, outputdata: function (data, hardware) {If (hardware) {// here we think, data can be transmitted as long as the plug-in is consistent //... hardware. transdata (data); // all external devices will use this method alert ("Data Transmission completed ");}}};
Card Reader
// Define the card reader class, and install a memory card with a recognizable slot, and then install a computer-recognizable USB 2.0 plug, okvar cardreader = function () {This. slot = "memory card slot"; this. plug = "USB 2.0"; this. card = NULL; // card this. computer = NULL; // computer (can be understood as all devices that can read cards)}; // three operations are added for the computer. One is used as the plug-in card and the other is used as the plug-in computer, one data transmission cardreader. prototype = {installcard: function (card) {If (card. slot! = This. slot) {alert ("slot mismatch");} else {This. card = card alert ("the memory card has been inserted into the slot") ;}, connectcomputer: function (Computer) {If (computer. plug! = This. plug) {alert ("USB interface mismatch");} else {This. computer = computer; alert ("a recognizable external device is connected to a computer") ;}}, transdata: function (data) {// call the memorycard method to save the data this. card. savedata (data );}};
Use Card Reader
// Memory card var memcard = new memorycard (); // computer var computer = new computer (); // card reader var cardreader = new cardreader (); // Insert the card into the card reader cardreader. installcard (memcard); // connect the card reader to the computer cardreader. connectcomputer (Computer); // data can be transmitted here. outputdata ({software: "qq2010"}, cardreader );

Ah, it's all. You can't waste the company's power any more. Just get off work.

If you have misstated this, please do not care about it.

 

 

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.