适配器模式

适配器模式:

我国规定民用交流电压是220V(+10% -15%),就是说交流220V电压正常范围在187V--242V

但是每种电器的接收的电压都不一致:

1.电脑

2手机

3.空调

虽然输入电压是相同的220V 但是电器内部少有能直接接收220V电压的零件。 

再举个例子。或者说你的手机出国也能充上电,国外的电压可能是更低或者更高。这就意味着你的手机充电器,电脑能适配不同的电压。

这个就是适配!

 

例子:从画点 到画矩形

class Point
    {
        public int X;
        public int Y; 
        public Point(int x, int y)
        {
            X = x;
            Y = y;
        }
        public override string ToString()
        {
            return $"X:{X},Y:{Y}";
        }
    }
    class Line
    {
        public Point startPoint, endPoint;

        public Line(Point startPoint, Point endPoint)
        {
            this.startPoint = startPoint;
            this.endPoint = endPoint;
        }
    }
    class VectorObject : Collection<Line>
    { 
    }
    class VectorRectange : VectorObject
    {
        public VectorRectange(int x,int y,int width,int height)
        {
            Add(new Line(new Point(x, y), new Point(x + width, y)));
            Add(new Line(new Point(x, y), new Point(x, y + height)));
            Add(new Line(new Point(x + width, y), new Point(x + width, y + height)));
            Add(new Line(new Point(x, y + height), new Point(x + width, y + height)));
        } 
    }
    class LineToPointAdapter:Collection<Point>
    {
        private static int count;
        public LineToPointAdapter(Line line)
        {
            Console.WriteLine($"{++count}  StartPoint:{line.startPoint},EndPoint:{line.endPoint}");

            var left = Math.Min(line.startPoint.X, line.endPoint.X);
            var right = Math.Max(line.startPoint.X, line.endPoint.X);
            var top = Math.Min(line.startPoint.Y, line.endPoint.Y);
            var buttom = Math.Max(line.startPoint.Y, line.endPoint.Y);
            var rx = right - left;
            var ry = buttom - top;
            if (rx==0)
            {
                for (int i = top; i <= buttom; i++)
                {
                    Add(new Point(left, i));
                }
            }
            else if(ry==0)
            {
                for (int i = left; i <= right; i++)
                {
                    Add(new Point(i, top));
                }
            }
        }

    }

    class Program
    {
        public static void DrawPoint(Point p)
        {
            Console.Write(".");
        }
        private static List<VectorRectange> rectanges = new List<VectorRectange>()
        {
          new VectorRectange(1,1,10,10),
          new VectorRectange(3,3,5,5)
        };

        static void Main(string[] args)
        {
            foreach (var item in rectanges)
            {
                foreach (var cotr in item)
                {
                    LineToPointAdapter lines =  new LineToPointAdapter(cotr);
                    lines.ForEach(DrawPoint);
                }
            }
        }

 

 More Then。。

 class Point
    {
        public int X;
        public int Y; 
        public Point(int x, int y)
        {
            X = x;
            Y = y;
        }
        public override string ToString()
        {
            return $"X:{X},Y:{Y}";
        }
        protected bool Euqals(Point p)
        {
            return this.X == p.X && this.Y == p.Y;
        }
        public override bool Equals(object obj)
        {
            return Equals((Point)obj);
        }
    }
    class Line
    {
        public Point startPoint, endPoint;

        public Line(Point startPoint, Point endPoint)
        {
            this.startPoint = startPoint;
            this.endPoint = endPoint;
        }
    }
    class VectorObject : Collection<Line>
    { 
    }
    class VectorRectange : VectorObject
    {
        public VectorRectange(int x,int y,int width,int height)
        {
            Add(new Line(new Point(x, y), new Point(x + width, y)));
            Add(new Line(new Point(x, y), new Point(x, y + height)));
            Add(new Line(new Point(x + width, y), new Point(x + width, y + height)));
            Add(new Line(new Point(x, y + height), new Point(x + width, y + height)));
        } 
    }
    class LineToPointAdapter:IEnumerable<Point>
    {
        private static Dictionary<int, List<Point>> cache = new();
        private static int count;
        public LineToPointAdapter(Line line)
        {

            if (cache.ContainsKey(line.GetHashCode())) return;

            var code = line.GetHashCode();
            var list = new List<Point>();

            Console.WriteLine($"{++count}  StartPoint:{line.startPoint},EndPoint:{line.endPoint}");

            var left = Math.Min(line.startPoint.X, line.endPoint.X);
            var right = Math.Max(line.startPoint.X, line.endPoint.X);
            var top = Math.Min(line.startPoint.Y, line.endPoint.Y);
            var buttom = Math.Max(line.startPoint.Y, line.endPoint.Y);
            var rx = right - left;
            var ry = buttom - top;
            if (rx==0)
            {
                for (int i = top; i <= buttom; i++)
                {
                    list.Add(new Point(left, i));
                }
            }
            else if(ry==0)
            {
                for (int i = left; i <= right; i++)
                {
                    list.Add(new Point(i, top));
                }
            }
            cache[code] = list;
        }

        public IEnumerator<Point> GetEnumerator()
        {
            return cache.Values.SelectMany(x => x).GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }

    class Program
    {
        public static void DrawPoint(Point p)
        {
            Console.Write(".");
        }
        private static List<VectorRectange> rectanges = new List<VectorRectange>()
        {
          new VectorRectange(1,1,10,10),
          new VectorRectange(3,3,5,5)
        };

        static void Main(string[] args)
        {
            NewMethod();
            NewMethod();
        }

        private static void NewMethod()
        {
            foreach (var item in rectanges)
            {
                foreach (var cotr in item)
                {
                    LineToPointAdapter lines = new LineToPointAdapter(cotr);
                    lines.ForEach(DrawPoint);
                }
            }
        }
    }

 

posted @ 2022-05-13 10:38  后跳  阅读(294)  评论(0编辑  收藏  举报