• 单例模式 Singleton Ambient Context


    单例模式:上下文语境

    using System;
    using System.Collections.Generic;
    
    namespace DesignParttern_Singleton_Context
    {
    
        public class BuildContext:IDisposable
        {
            public int Height;
            private static Stack<BuildContext> Stack = new Stack<BuildContext>();
            public static BuildContext Current => Stack.Peek();
            static BuildContext()
            {
                Stack.Push(new BuildContext(0));
            }
            public BuildContext(int i)
            {
                Height = i;
                Stack.Push(this);
            }
            public void Dispose()
            {
                if (Stack.Count>1)
                {
                    Stack.Pop();
                }
            }
        }
        public class Building
        {
            public  List<Wall> walls = new List<Wall>();
    
            public void Display() 
            {
                walls.ForEach(a => Console.WriteLine(a));
            }
        }
        public class Wall
        {
            public Point Start, End;
            public int Height;
            public Wall(Point start, Point end)
            {
                Start = start;
                End = end;
                Height = BuildContext.Current.Height;
            }
            public override string ToString()
            {
                return $"{nameof(Start)}:{Start},{nameof(End)}:{End},{nameof(Height)}:{Height},";
            }
        }
        public struct 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 Program
        {
            static void Main(string[] args)
            {
                Building building = new Building();
                using (new BuildContext(3000))
                {
                    building.walls.Add(new Wall(new Point(0, 0), new Point(0, 3000)));
                    building.walls.Add(new Wall(new Point(0, 0), new Point(4500, 0)));
                    using (new BuildContext(3500))
                    {
                        building.walls.Add(new Wall(new Point(0, 0), new Point(0, 3000)));
                        building.walls.Add(new Wall(new Point(0, 0), new Point(4000, 0)));
                    }
                    building.walls.Add(new Wall(new Point(3000, 0), new Point(0, 0)));
                    building.walls.Add(new Wall(new Point(0, 4500), new Point(0, 0)));
                    using (new BuildContext(5000))
                    {
                        building.walls.Add(new Wall(new Point(3000, 4500), new Point(4500, 3000))); 
                    }
                } 
                building.Display();
            }
        }
    }
  • 相关阅读:
    服务器文档下载zip格式
    关于精度,模运算和高精的问题//19/07/14
    Luogu P2010 回文日期 // 暴力
    树形DP水题集合 6/18
    普通背包水题集合 2019/6/17
    因为时间少
    重学树状数组6/14(P3368 【模板】树状数组 2)
    Luogu P1291 [SHOI2002]百事世界杯之旅 // 易错的期望
    Luogu P4316 绿豆蛙的归宿//期望
    树剖
  • 原文地址:https://www.cnblogs.com/Zingu/p/16263390.html
Copyright © 2020-2023  润新知