0

所以我有这个映射两个酒店目录的任务;两者都是 csv 文件。我创建了两个类,基于它们的职责: 1. CatalogManager:处理目录的 I/O 操作。2. CatalogMapper:处理两个目录的映射任务。

定义如下:

public static class CatalogManager
{
   public static List<Hotel> GetHotels(string filePath) { }
   public static void SaveHotels (List<Hotel> hotels, string filePath) { }
   public static void SaveMappedHotels (List<MappedHotel> hotels, string filePath) { }
   public static List<string> GetHotelChains(string filePath) { }
}

public static class CatalogMapper
{
   public static List<MappedHotel> MapCatalogs (List<Hotel> masterCatalog, List<Hotel> targetCatalog) { }

   public static FetchAddressGeoCodes (Hotel.Address address)
   { // fetch address's geocode using Google Maps API }

   public static string GetRelevantHotelChain (string hotelName)
   {
      List<string> chains = CatalogManager.GetChains();
      // find and return the chain corresponding to hotelName. 
   }
}

典型的映射操作可能类似于:

List<Hotel> masterCatalog = CatalogManager.GetHotels(masterFilePath);
List<Hotel> targetCatalog = CatalogManager.GetHotels(targetFilePath);
List<MappedHotel> mappedHotels = CatalogMapper.MapHotels(masterCatalog, targetCatalog);
CatalogManager.SaveMappedHotels(mappedHotels, mappedCatalogFilePath);

如代码所示,这两个类都是静态的。尽管我发现它们是正确的并且可以正常工作,但我仍然觉得这种设计在 OOP 方面存在问题。这两个类都只是静态的,这很好吗?我发现不需要实例化它们。此外,这个设计还有哪些其他缺陷?我确信存在缺陷。解决方案是什么?

4

2 回答 2

1

另一种合理的方法是CatalogManager使用文件名初始化一个非静态类。这将允许您从内存中部分使用文件并在需要时读取或写入。

于 2011-10-10T11:24:24.183 回答
1

不要害怕免费功能!

我觉得CatalogMapper映射 a很可疑Hotel.Address。要正确分解/封装,要么

  • CatalogMapper应该在非酒店特定的Address,

  • 或者,如果Hotel.Address是某种特殊的地理代码,那么Hotel.Address应该能够将自己映射到没有CatalogMapper.


通过评论的澄清,我想提出以下建议。我不懂 C#,所以我会把它写成 C++。希望能翻译

struct Hotel {
    const Address & address () const;

    // I typedef EVERTYTHING :-)    
    typedef std :: list <std :: string> StringList;
    typedef std :: pair <Hotel, Hotel> Pair;
    typedef std :: list <Hotel> Container; // TODO implicit sharing?
    typedef std :: list <Pair> Mapping;

    // NB will be implemented in terms of std::istream operations below,
    // or whatever the C# equivalent is.
    // These could arguably live elsewhere.
    static Container load (const std :: string &);
    static Mapping load_mapping (const std :: string &);
    static void save (const std :: string &, const Container &);
    static void save_mapping (const std :: string &, const Mapping &);

    // No need for a "Manager" class for this.
    static StringList load_chain (const string & file_name);

private:
    static Hotel load (std :: istream &);
    void save (std :: ostream &) const;
};

// Global namespace, OK because of overloading. If there is some corresponding
// generic library function which merges pair-of-list into list-of-pair
// then we should be specialising/overloading that.
Hotel :: Mapping merge (const Hotel :: Container &, const Hotel :: Container &);

struct GeoCode {
   GeoCode (const Address &);
};

每当我看到一个名为“Manager”的类时,如果它不是创建、拥有和控制对其他对象的访问的对象,那么它就是我们处于名词王国的警告警报。对象可以自己管理。如果该逻辑超出了单个类的范围,则您应该只为该逻辑创建一个单独的类。

于 2011-10-10T13:25:57.293 回答