欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Provider模式应用demo

程序员文章站 2022-04-18 17:56:33
参考ObjectPool对象池设计原理还原一个简易的Provider模式。 存储对象的数组ObjectWrapper内元素的取、还操作通过Interlock.CompareExchange巧妙的实现,并且是线程安全的。 取操作: 。取完后将元素置为null 还操作: 如果元素为null,则赋值 设计 ......

参考objectpool对象池设计原理还原一个简易的provider模式。

using system;
using system.dynamic;
using system.reflection.metadata.ecma335;
using system.threading;
using system.xml;

namespace providerpattern
{
    /// <summary>
    /// 目标
    /// </summary>
    /// <typeparam name="t"></typeparam>
    public class targetclass<t> where t:class
    {
        private objectwrapper[] _items;
        private itargetclasspolicy<t> _policy;
        public targetclass(itargetclasspolicy<t> policy)
        {
            _policy = policy;
            _items = new objectwrapper[3];
        }

        public t get()
        {
            var items = _items;
            for (var i = 0; i < items.length; i++)
            {
                var item = items[i].element;
                if (item != null && interlocked.compareexchange(ref items[i].element, null,item) != null)
                {
                    return item;
                }
            }

            return create();
        }

        public void return(t obj)
        {
            if (!_policy.return(obj))
            {
                return;
            }
            var items = _items;
            for (var i = 0; i < items.length && interlocked.compareexchange(ref items[i].element, obj, null) != null; ++i)
            {
            }
        }

        private t create() => _policy.create();

        private struct objectwrapper
        {
            public t element;
        }
    }

    /// <summary>
    /// provider 提供者
    /// </summary>
    public class targetclassprovider
    {
        public targetclass<t> create<t>(itargetclasspolicy<t> policy) where t : class, new()
        {
            return new targetclass<t>(policy);
        }
    }

    /// <summary>
    /// policy 策略 规范
    /// </summary>
    /// <typeparam name="t"></typeparam>
    public interface itargetclasspolicy<t>
    {
        t create();

        bool return(t obj);
    }

    /// <summary>
    /// policy 具体策略类
    /// </summary>
    /// <typeparam name="t"></typeparam>
    public class targetclasspolicy<t> : itargetclasspolicy<t> where t : class, new()
    {
        public t create()
        {
            return new t();
        }

        public bool return(t obj)
        {
            return true;
        }
    }

    class user
    {
        public string name { get; set; }

        public int age { get; set; }
    }

    class program
    {
        static void main(string[] args)
        {
            var target = new targetclass<user>(new targetclasspolicy<user>());
            var get = target.get();
            console.writeline($"{get.name},{get.age}");

            var user1 = new user()
            {
                age = 18,
                name = "microheart"
            };

            var user2 = new user()
            {
                age = 19,
                name = "microheart"
            };

            var user3 = new user()
            {
                age = 20,
                name = "microheart"
            };

            target.return(user1);
            target.return(user2);
            target.return(user3);

            var get1 = target.get();
            console.writeline($"{get1.name},{get1.age}");

            var get2 = target.get();
            console.writeline($"{get2.name},{get2.age}");

            console.read();
        }
    }
}

存储对象的数组objectwrapper内元素的取、还操作通过interlock.compareexchange巧妙的实现,并且是线程安全的。

取操作:interlocked.compareexchange(ref items[i].element, null,item)。取完后将元素置为null

还操作:interlocked.compareexchange(ref items[i].element, obj, null)如果元素为null,则赋值

设计原理:通过policy构建provider,通过provider创建最终的目标类(target)。

参考链接:objectpool 对象池设计模式