Unity 背包道具搜索(2)

上一篇: http://www.cnblogs.com/plateFace/p/6490577.html

上次编写代码只是把逻辑编写出来, 对于里面的代码还存在一下问题

1. 搜索功能没有解耦

2. 添加新的搜索,只能修改源代码

3. 消除大量switch

搜索器:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SearchCollection<T>
{
    private Dictionary<int, ItemFun<T>> mExpressionDic = new Dictionary<int, ItemFun<T>>();


    public void Init()
    {
        mExpressionDic.Clear();
    }

    public void AddFilter(int id, ItemFun<T> fun)
    {
        mExpressionDic.Add(id, fun);
    }

    public List<T> Get(Dictionary<int, object> condition, IEnumerable<T> itemCollection)
    {
        List<T> tempList = new List<T>();
        ItemFun<T> func = null;
        bool isNotThrough = false;
        foreach (var item in itemCollection)
        {
            isNotThrough = true;
            foreach (var cond in condition)
            {
                func = mExpressionDic[cond.Key];

                if (func.IsSuit(item, cond.Value) == false)
                {
                    isNotThrough = false;
                    break;
                }
            }
            if (isNotThrough)
            {
                tempList.Add(item);
            }
        }
        return tempList;
    }
}
public abstract class ItemFun<T>
{
    public abstract bool IsSuit(T item, object args);
}

转载于:https://www.cnblogs.com/plateFace/p/8608538.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值