其他分享
首页 > 其他分享> > 桥接模式+AutoFac

桥接模式+AutoFac

作者:互联网

桥:

interface IRenderer
    {
        void RenderCircle(float radius);
    }
    class VectorRenderder : IRenderer
    {
        public void RenderCircle(float radius)
        {
            Console.WriteLine($"Draw circle of radius {radius}");
        }
    }
    class RasterRenderer : IRenderer
    {
        public void RenderCircle(float radius)
        {
            Console.WriteLine($"Draw pixels for circle of radius {radius}");
        }
    }

     abstract class Shape
    {
        protected IRenderer renderer;

        protected Shape(IRenderer renderer)
        {
            this.renderer = renderer;
        }
        public abstract void Draw();
        public abstract void Resize(float size);
    }

    class Circle : Shape
    {
        private float raduis;

        public Circle(IRenderer renderer,float raduis):base(renderer)
        {
            this.raduis = raduis;
        }

        public override void Draw()
        {
            renderer.RenderCircle(raduis);
        }

        public override void Resize(float size)
        {
            raduis *= size;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            /**
            Circle c = new Circle(new RasterRenderer(), 2);
            c.Draw();
            c.Resize(2);
            c.Draw();
            */
            var cb = new ContainerBuilder();
            cb.RegisterType<VectorRenderder>().As<IRenderer>().SingleInstance();
            cb.Register((c, p) =>
            new Circle(c.Resolve<IRenderer>(),
            p.Positional<float>(0)
                ));
            using (var c=cb.Build())
            {
                var circle = c.Resolve<Circle>(new PositionalParameter(0, 5.0f));
                circle.Draw();
                circle.Resize(2);
                circle.Draw();

            } 
        }
    }

 

标签:AutoFac,桥接,void,float,Draw,模式,radius,renderer,public
来源: https://www.cnblogs.com/Zingu/p/16267600.html