2016-09-28 39 views
2

這些類的完美初始化設計模式是什麼?如何使用OOP在C#中初始化這些實體?

第一類稱爲「他買了」的名稱,ID和訂單列表的客戶端。

第二類稱爲DeliveryReceptionist,他的班級有他的名字,他的ID和他購買的訂單列表以及他所服務的人員列表。

第三類是他購買的產品,我有10個項目的列表和每個項目應該有一個名字和ID

第四類是由ITEM_ID的,每件商品的價格,數量的順序項目「,因爲每個訂單都有一種類型的項目」。

這很奇怪,不適用於現實生活,但它不是我說的。

所以,我已經試過了:

public class Information 
{ 
    public string Name { get; set; } 
    public int ID { get; set; } 
} 

public class Client : Information 
{ 
    List<theOrder> orders = new List<theOrder>(); 
} 

public class DelRec : Information 
{ 
    List<Client> listOfClients = new List<Client>(); 
    List<theOrder> orders = new List<theOrder>(); 
} 

public static class items 
{ 
    public static List<Information> item = new List<Information>(); 
} 

public class theOrder 
{ 
    private int itemID { get; set; } 
    private double price { get; set; } 
    private double number { get; set; } 
} 

好了嗎?或者我應該以更好的方式優化它?

+0

你已經有很棒的OOP風格。但是我想問一下你想創建哪個系統的一部分。我的意思是,這與訂單處理有關,因此訂單和客戶已經存儲,交付接待員需要通過此訂單並將其發送給客戶。 或者顧客去購物並選擇他想購買的產品? 系統的各個部分將以不同的方式實施。 –

回答

1

關於此代碼有幾件事。

所有屬性和類都應該是C#中的PascalCase。 theOrder應該是Order,因爲它的類型不是實例,如果你喜歡,你可以調用一個實例theOrder。 如果您將它們設置爲私有並且具有空的構造函數,則無法實例化Order類的屬性。另外你將如何使用獲得者?

您將需要構造函數來初始化您的屬性。 而且創建接口和抽象類也是很好的OOP。 首先,我的代碼將是這樣的:

public interface IPerson 
{ 
    string Name { get; } 
    int Id { get; } 
} 

public interface ISalesSubject : IPerson 
{ 
    IDictionary<int, IOrder> Orders { get; } 
} 

public abstract class SalesSubject: ISalesSubject 
{ 
    protected SalesSubject(string name, int id) 
    { 
     this.Name = name; 
     this.Id = id; 
     this.Orders = new Dictionary<int, IOrder>(); 
    } 

    protected SalesSubject(string name, int id, IDictionary<int, IOrder> orders) 
    { 
     this.Name = name; 
     this.Id = id; 
     this.Orders = orders; 
    } 

    public string Name { get; protected set; } 

    public int Id { get; protected set; } 

    public IDictionary<int, IOrder> Orders { get; protected set; } 

    public virtual void AddOrder(int itemId, IOrder order) 
    { 
     if (!this.Orders.ContainsKey(itemId)) 
     { 
      this.Orders.Add(itemId, order); 
     } 
    } 

    public virtual void RemoveOrder(int itemId, IOrder order) 
    { 
     this.Orders.Remove(itemId, order); 
    } 
} 

public class Client : SalesSubject 
{ 

    public Client(string name, int id) 
     : base(name, id) 
    { 
     this.Orders = new Dictionary<int, IOrder>(); 
    } 

    public Client(string name, int id, IDictionary<int, IOrder> orders) 
     : base(name, id, orders) 
    { 
     this.Orders = orders; 
    } 
} 

public class DeliveryReceptionist : SalesSubject 
{ 
    public DeliveryReceptionist(string name, int id) 
     : base(name, id) 
    { 
     this.Clients = new Dictionary<int, Client>(); 
    } 

    public DeliveryReceptionist(string name, int id, IDictionary<int, IOrder> orders) 
     : base(name, id, orders) 
    { 
     this.Clients = new Dictionary<int, Client>(); 
    } 

    public DeliveryReceptionist(string name, int id, IDictionary<int, IOrder> orders, IDictionary<int, Client> clients) 
     : base(name, id, orders) 
    { 
     this.Clients = clients; 
    } 

    public DeliveryReceptionist(string name, int id, IDictionary<int, Client> clients) 
     : base(name, id) 
    { 
     this.Clients = clients; 
    } 

    public IDictionary<int, Client> Clients { get; set; } 

    public void AddClient(int clientId, Client client) 
    { 
     if (!this.Clients.ContainsKey(clientId)) 
     { 
      this.Clients.Add(clientId, client); 
     } 
    } 

    public void RemoveClient(int clientId, Client client) 
    { 
     this.Clients.Remove(clientId, client); 
    } 
} 

public interface IOrder 
{ 
    int ItemID { get; set; } 
    double Price { get; set; } 
    double Number { get; set; } 
} 
public class Order : IOrder 
{ 
    public Order(int itemId, double price, double number) 
    { 
     this.ItemId = itemId; 
     this.Price = price; 
     this.Number = number; 
    } 

    public int ItemId { get; set; } 
    public double Price { get; set; } 
    public double Number { get; set; } 
} 

爲什麼你需要的物品class.Static類是反模式談論encapsualtion和良好的OOP.They時,可以從任何地方,其含量變化進行訪問如果是公共訪問修飾符。

+0

是的,我同意你的代碼應該在PascalCase和「theOrder」不需要「我添加了,而測試的東西,忘了刪除它,也錯誤地編輯代碼在這裏發佈之前澄清問題所以我也忘了把私人物品變成公共物品,所以我不能同意你在這些物品上的更多 關於物品類,它是必需的,我不知道該怎麼做,不得不添加一些項目,並顯示它們,我怎麼能做到這一點,與空繼承類?我真的不知道。 –

+0

最後,我不熟悉字典,所以你可以請一次顯示我怎麼能例如添加訂單或客戶?謝謝 –

+1

var orderId = 1; var price = 20.5; var number = 20; Order order = new Order(orderId,price,number); var clientName =「Omar」; var clientId = 100; var client = new Client(clientName,clientId); 客戶端。AddOrder(orderId,order); var delRecId = 1; var delRecName =「郵差」; var delRec = new DeliveryReceptionist(delRecName,delRecId); delRec.AddClient(clientId,client); delRec.RemoveClient(clientId); client.RemoveOrder(orderId); –