定义

将一个类的接口转换为用户需要的另外一个接口,解决类(对象)之间接口不兼容的问题

类图

Snipaste_2020-09-14_22-37-59.png

代码

基本实例1

  1. // 这是需要被适配的类
  2. class Socket {
  3. output() {
  4. return "220V";
  5. }
  6. }
  7. // 转换插头的 类
  8. abstract class Power {
  9. abstract charge(): string;
  10. }
  11. class PowerAdaptor extends Power {
  12. constructor(public socket: Socket) {
  13. super();
  14. }
  15. charge(): string {
  16. return this.socket.output() + "转换为12v";
  17. }
  18. }
  19. let adaptor = new PowerAdaptor(new Socket());
  20. console.log(adaptor.charge());

axios类应用适配器

使用适配器处理浏览器和服务器端发送请求的统一处理

  1. // 使用适配器模式处理浏览器或者服务端接口请求
  2. let url = require("url");
  3. function axios(config) {
  4. let adaptor = getDefaultAdaptor();
  5. // 兼容浏览器和服务器端
  6. return adaptor(config);
  7. }
  8. function getDefaultAdaptor() {
  9. let adaptor;
  10. // 在浏览器端
  11. if (typeof XMLHttpRequest !== "undefined") {
  12. adaptor = xhrAxios;
  13. } else if (typeof process !== "undefined") {
  14. // 服务器端
  15. adaptor = httpAxios;
  16. }
  17. return adaptor;
  18. }
  19. function xhrAxios(config) {
  20. return new Promise((resolve, reject) => {
  21. let XHR = new XMLHttpRequest();
  22. XHR.open(config.method, config.url, true);
  23. XHR.onreadystatechange = function () {
  24. if (XHR.readyState == 4) {
  25. if (XHR.status == 200) {
  26. resolve(XHR.responseText);
  27. } else {
  28. reject("请求错误");
  29. }
  30. }
  31. };
  32. });
  33. }
  34. function httpAxios(config) {
  35. let http = require("http");
  36. let urlObj = url.parse(config.url);
  37. return new Promise((resolve, reject) => {
  38. const options = {
  39. hostname: urlObj.hostname,
  40. port: urlObj.port,
  41. path: urlObj.path,
  42. method: config.method,
  43. };
  44. let req = http.request(options, (response) => {
  45. let chunks = [];
  46. response.on("data", function (chunk) {
  47. chunks.push(chunk);
  48. });
  49. response.on("end", function () {
  50. let result = Buffer.concat(chunks).toString();
  51. resolve(result);
  52. });
  53. });
  54. req.on("error", function (error) {
  55. reject(error);
  56. });
  57. req.end();
  58. });
  59. }

多维数组扁平化

  1. let treeData = [
  2. {
  3. name: "河南",
  4. key: "henan",
  5. children: [
  6. { name: "郑州", key: "zhengzhou" },
  7. { name: "开封", key: "kaifeng" },
  8. { name: "洛阳", key: "luoyang" },
  9. {
  10. name: "新乡",
  11. key: "xinxiang",
  12. children: [
  13. { name: "辉县", key: "huixian" },
  14. { name: "长垣", key: "changyuan" },
  15. { name: "延津", key: "yanjin" },
  16. ],
  17. },
  18. ],
  19. },
  20. {
  21. name: "山东",
  22. key: "shandong",
  23. children: [
  24. { name: "济南", key: "jinan" },
  25. { name: "青岛", key: "qingdao" },
  26. ],
  27. },
  28. ];
  29. // 要把该数组对象放在函数外面,这样每次函数迭代是不会清空数组
  30. let arrData = [];
  31. function flattenData(treeData) {
  32. treeData.forEach((item) => {
  33. arrData.push({ name: item.name, key: item.key });
  34. if (item.children) {
  35. flattenData(item.children, arrData);
  36. }
  37. });
  38. return arrData;
  39. }
  40. function flattenData2(treeData, arr) {
  41. treeData.forEach((item) => {
  42. if (item.children) {
  43. flattenData2(item.children, arr);
  44. }
  45. arr.push({ name: item.name, key: item.key });
  46. });
  47. }
  48. console.log(flattenData(treeData));
  49. let flattenArr = [];
  50. flattenData2(treeData, flattenArr);
  51. console.log(flattenArr);