把已经的接口适配成所需要的接口
    image.png

    现有 API,用于画点

    1. let drawPoint = function(point){
    2. process.stdout.write('.');
    3. }

    console.log = function (d){ process.stdout.write(d + ‘\n’); }; console.log() 是带换行的

    现需要一个画矩形的类

    1. class Point{
    2. constructor(x, y){
    3. this.x = x;
    4. this.y = y;
    5. }
    6. toString(){
    7. return `(${this.x}, ${this.y})`;
    8. }
    9. }
    10. class Line{
    11. constructor(start, end){
    12. this.start = start;
    13. this.end = end;
    14. }
    15. toString(){
    16. return `${this.start.toString()} ${this.end.toString()}`;
    17. }
    18. }
    19. class VectorObject extends Array{
    20. }
    21. class VectorRectangle extends VectorObject{
    22. constructor(x, y, width, height){
    23. super();
    24. this.push(new Line(new Point(x, y), new Point(x + width, y)));
    25. this.push(new Line(new Point(x + width, y), new Point(x + width, y + height)));
    26. this.push(new Line(new Point(x, y), new Point(x, y + height)));
    27. this.push(new Line(new Point(x, y + height), new Point(x + width, y + height)));
    28. }
    29. }
    30. let vectorObjects = [
    31. new VectorRectangle(1, 1, 10, 10),
    32. new VectorRectangle(5, 5, 20, 20)
    33. ]

    构造把点转为直线的构造器,一直是一组点所构成

    1. class LineToPointAdapter{
    2. constructor(line){
    3. this.hash = JSON.stringify(line).hashCode();
    4. if(LineToPointAdapter.cache[this.hash]){
    5. return
    6. }
    7. let points = [];
    8. console.log(`${LineToPointAdapter.count++}: 正在为从 ${line.toString} 的直线生成点(没有使用缓存)`);
    9. let left = Math.min(line.start.x, line.end.x);
    10. let right = Math.max(line.start.y, line.end.y);
    11. let top = Math.max(line.start.y, line.end.y);
    12. let bottom = Math.min(line.start.y, line.end.y);
    13. if(right - left === 0){
    14. for(let y = bottom, y <= top; y++){
    15. points.push(new Point(left, y));
    16. }
    17. } else if (top - bottom === 0){
    18. for(let x = left, x <= right; x++){
    19. points.push(new Point(x, top));
    20. }
    21. }
    22. LineToPointAdapter.cache[this.hash] = points;
    23. }
    24. get items(){
    25. return LineToPointAdapter.cache[this.hash];
    26. }
    27. }
    28. LineToPointAdapter.count = 0;
    29. LineToPointAdapter.cache = {};
    30. let drawPoints = function(){
    31. for(let vo of vectorObjects){
    32. for(let line of vo){
    33. let adapter = new LineToPointerAdapter(line);
    34. adapter.items.forEach(drawPoint);
    35. }
    36. }
    37. }
    38. drawPoints();
    39. drawPoints();
    1. String.prototype.hashCode = function(){
    2. if(Array.prototype.reduce){
    3. return this.split("").reduce(function(a,b){
    4. a=((a<<5)-a)+b.charCodeAt(0);
    5. return a&a;
    6. }, 0)
    7. }
    8. let hash = 0;
    9. if(this.length ===0) return hash;
    10. for(let i=0;i<this.length;i++){
    11. count character = this.charCodeAt(i);
    12. hash = ((hash<<5)-hash)+character;
    13. hash = hash & hash;
    14. }
    15. return hash;
    16. }