English 中文(简体)
哪些设计模式用于订购和过滤数据?
原标题:Which design pattern for ordering and filtering data?

我需要在用户通过下降选择的标准之后过滤和订购数据条目。 可选择的是“西端”、“最原始条目第一”、“最低价格第一”等。

我只能为选择和转换/转盘设定一个表,以便我检索数据,但我能以易于推广的方式这样做。

What design pattern would fit the situation best?

最佳回答

每个人都提到了战略模式。 我只是想我简单地予以执行。 无需使其更加复杂。

public enum SortMethod
{
    Newest,
    Oldest,
    LowestPrice,
}

public class Foo
{
    public DateTime Date {get;set;}
    public decimal Price {get;set;}
}


...
var strategyMap = new Dictionary<SortMethod, Func<IEnumerable<Foo>, IEnumerable<Foo>>>
                  {
                      { SortMethod.Newest, x => x.OrderBy(y => y.Date) },
                      { SortMethod.Oldest, x => x.OrderByDescending(y => y.Date) },
                      { SortMethod.LowestPrice, x => x.OrderBy(y => y.Price) }
                  };

...
var unsorted = new List<Foo>
               {
                   new Foo { Date = new DateTime(2012, 1, 3), Price = 10m },
                   new Foo { Date = new DateTime(2012, 1, 1), Price = 30m },
                   new Foo { Date = new DateTime(2012, 1, 2), Price = 20m }
               };

var sorted = strategyMap[SortMethod.LowestPrice](unsorted);
问题回答

我并非总是赞成把正确的模式命名为适合我的想法,但我的初步想法是,为什么不能为每一种选择提供一个简单的类别,不能执行国际协调委员会(T),然后将这些项目装上你们的倒数选择。 除接口方法外,你很可能只需要一个名称财产。

public class NameSorter: IComparer<WhateverObj>
{
public String DisplayName;    

public int Compare(WhateverObj x, WhateverObj y)
{

}
}

如评论意见所述,这种声音如。 你承认这一点,因为这一点在互联网框架内已经非常突出。

这里的一个例子是,我喜欢使用IComparer,或者在3.5中使用LINQ推广方法。 您仍然需要在基准类别中添加一种工厂式的方法,该基准类别可比较它应当使用的方法,或作为数据的一部分储存在下级清单中。

static void Main(string[] args)
{

    List<User> users = new List<User>();
    users.Add(new User() { Name = "Larry", Age = 35 });
    users.Add(new User() { Name = "Bob", Age = 25 });
    users.Add(new User() { Name = "Brian", Age = 30 });

    NameComparer sorter = new NameComparer();
    IEnumerable<User> sortedUsers = sorter.Sort(users);

    NameComparer35 sorter35 = new NameComparer35();
    IEnumerable<User> sortedUsers35 = sorter35.Sort(users);
}

public abstract class MyComparer<T> : IComparer<T> where T: User
{
    public abstract int Compare(T x, T y);

    public IEnumerable<T> Sort(IEnumerable<T> items)
    {
        items.ToList().Sort(this);
        return items;
    }
}

public abstract class MyComparer35<T> where T : User
{
    public abstract IEnumerable<T> Sort(IEnumerable<T> items);
}

public class NameComparer35 : MyComparer35<User>
{
    public override IEnumerable<User> Sort(IEnumerable<User> items)
    {
        return items.OrderBy(u => u.Name);
    }
}

public class NameComparer : MyComparer<User>
{
    public override int Compare(User x, User y)
    {
        return x.Name.CompareTo(y.Name);
    }
}

public class AgeComparer : MyComparer<User>
{
    public override int Compare(User x, User y)
    {
        return x.Age.CompareTo(y.Age);
    }
}

public class User
{
    public string Name { get; set; }
    public int Age { get; set; }

    public override string ToString()
    {
        return string.Format("{0} {1}", Name, Age);
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签