【发布时间】:2015-11-06 00:35:04
【问题描述】:
我注意到与我倾向于在线阅读的代码相比,我的课程往往更大/更长。下面的代码旨在作为示例,但我更感兴趣的是思考和解决问题的方式。
正如您将看到的那样,该类处理许多角色,我很想学习如何将其重构为其他类和示例,如果可能的话,提供实用的解决方案。关于如何解决这个问题的书籍/指南的链接会很棒。
所以我有一个带有以下声明的后端类。我正在使用 C#,但我认为我的问题涉及其他语言。
public static class Backend {
//These classes are the equivalent classes of a cloud database tables
//Using them to map the tables to objects in my app
public class User{}
public class Place{}
public class SubPriority{}
public class Question{}
public class Parent{}
public class Response{}
public class SubParent{}
//initialize the local and cloud databases
public static async void init();
//Add place to local database
public static string AddPlace(string name, string buildingType)
//Retrieve places from local database
public static List<Place> RetrievePlaces()
//Delete a place from local database
public static bool DeletePlace(string placeID)
public static string AddSubPriority(String name)
public static List<SubPriority> RetrieveSubPriorities()
public static bool DeleteSubPriority(string placeID, int ID)
//Sync local db with cloud
public static async Task<bool> SyncWithCloud()
//Download SubParents from the cloud
public static async Task<List<SubParent>> DownloadSubParents()
//Retrieve SubParents from local
public static List<SubParent> RetrieveSubParents(int parentid)
...
...
...
//Similar methods for parents and questions
//Handling login
public static async Task<bool> Login(string userName, string pass)
static async Task<bool> LoginOnline(string userName, string pass)
static bool LoginOffline(string userName, string pass)
//Check for internet connectivity
static async Task<bool> isConnectedToInternet()
static bool InternetAvailable()
}
【问题讨论】:
-
这似乎是 this Programmer Stack Exchange article 的副本。我个人可能会考虑将您的班级分成几个班级。
标签: language-agnostic refactoring