2017-09-19 60 views
-4

我想知道是否有更好的方法來設計類似的數據結構。代碼數據結構清單替代解決方案列表C#

object --> list of objects --> list of lists

示例如下:

class Customer 
{ 
    public string Name {get; set;} 
    public string LastName {get; set;} 
} 

class Customers 
{ 
    public List<Customer> {get; set} 
} 

class MultipleLists_Customers 
{ 
    public List<Customers> {get; set;} 
} 
+2

很難判斷一個設計,不知道的要求。這個數據結構正在解決什麼問題? –

回答

0

設計是關於代表問題域,讓您的設計可能是好還是壞取決於是什麼問題....一些重命名事情,它可能是相當有效的像

class Customer 
{ 
public string Name {get;set;} 
public string LastName {get; set;} 
} 

class Vendor 
{ 
    public string Name {get; set;} 
    public List<Customer> Customers {get;set} 
} 

class Organization 
{ 
    public string Name {get; set;} 
    public List<Vendor> Vendors {get;set;} 
} 
0

正如基思注意到,重命名是一個好的開始。

如果你真正想要的是表示對象的名單列表的對象,就可以實現它沒有簡單地嵌套泛型創建新的數據結構:

public List<List<Customer>> CustomerLists; 

這就是說,它似乎你試圖建模一個訂購系統。像這樣的伎倆:

class Customer 
{ 
    public Customer(string firstName, string lastName) 
    { 
     FirstName = firstName; 
     LastName = lastName; 
    } 

    public string FirstName { get; } 
    public string LastName { get; } 
} 

class Vendor 
{ 
    public Vendor(string name, IEnumerable<Product> products) 
    { 
     Name = name; 
     Products = new HashSet<Product>(products); 
    } 

    public bool HasStock(Product product, double quantity) 
    { 
     // determine if product is currently in stock... 
    } 

    public string Name { get; } 
    public HashSet<Product> Products { get; } 
} 

class Product 
{ 
    // ... common product data. 
} 

class Order 
{ 
    public Order(
     DateTime createDate, Customer customer, 
     Vendor vendor, Product product, double quantity) 
    { 
     CreateDate = createDate; 
     Customer = customer; 
     Vendor = vendor; 
     Product = product; 
     Quantity = quantity; 
    } 

    public DateTime CreateDate { get; } 
    public Customer Customer { get; } 
    public Vendor Vendor { get; } 
    public Product Product { get; } 
    public double Quantity { get; } 
} 
0

最基本的方法是這樣的:

class Customer 
{ 
    public string Name {get; set;} 
    public string LastName {get; set;} 
} 

class MultipleLists_Customers 
{ 
    public List<Customer> Customers {get; set;} 
}