使用单一职责原则重构程序 - SOLID-SRP
Re-factorize a program using single responsibility principle - SOLID- SRP
我正在学习 SOLID,我正在尝试理解单一职责原则。
classWalkingData存储了一个"date"和一个"walked distance"。 class也读取存储的数据。
public class WalkingData
{
public DateTime Date { get; set; }
public int WalkedDistance { get; set; }
private string _filePath = @"c:\Data\Json.txt";
//Read Data from Json File
public List<WalkingData> GetAll()
{
//If file does not exist returns an empty list
if (!File.Exists(_filePath)) return new List<WalkingData>();
string jsonData;
//Read the existing Json file
using (StreamReader readtext = new StreamReader(_filePath))
{
jsonData = readtext.ReadToEnd();
}
//Deserialize the Json and returs a list of WalkingData
return JsonConvert.DeserializeObject<List<WalkingData>>(jsonData);
}
//save an instance of WalkingData in Json file
public void Save()
{
List<WalkingData> lstExistingWalkingData = new List<WalkingData>();
//if existing data, load it into lstExistingWalkingData
if (File.Exists(_filePath))
lstExistingWalkingData = GetAll();
//Add the current instace into lstExistingWalkingData
lstExistingWalkingData.Add(this);
//Serialize lstExistingWalkingData
string output = JsonConvert.SerializeObject(lstExistingWalkingData);
//Save the Json file
using (StreamWriter w = new StreamWriter(_filePath))
{
w.WriteLine(output);
}
}
}
在我应用单一责任原则后,我有了新代码,我想确认我是否以合理的方式应用了该原则:
//This class is located on a library called BOL and has a reference to DAL library
public class WalkingData
{
public DateTime Date { get; set; }
public int WalkedDistance { get; set; }
}
//This class is located on a library called BOL and has a reference to DAL library
public class WalkingDataManager
{
WalkingDataRepository walkingDataRepository = new WalkingDataRepository();
public List<WalkingData> GetAll()
{
return walkingDataRepository.GetAll();
}
public void Save(WalkingData walkingData)
{
walkingDataRepository.Save(walkingData);
}
}
//this class is located in library Called DAL
internal class WalkingDataRepository
{
private string _filePath = @"c:\Data\Json.txt";
//Read Data from Json File
internal List<WalkingData> GetAll()
{
//If file does not exist returns an empty list
if (!File.Exists(_filePath)) return new List<WalkingData>();
string jsonData;
//Read the existing Json file
using (StreamReader readtext = new StreamReader(_filePath))
{
jsonData = readtext.ReadToEnd();
}
//Deserialize the Json and returs a list of WalkingData
return JsonConvert.DeserializeObject<List<WalkingData>>(jsonData);
}
//save an instance of WalkingData in Json file
internal void Save(WalkingData walkingData)
{
List<WalkingData> lstExistingWalkingData = new List<WalkingData>();
//if existing data, load it into lstExistingWalkingData
if (File.Exists(_filePath))
lstExistingWalkingData = GetAll();
//Add the current instace into lstExistingWalkingData
lstExistingWalkingData.Add(walkingData);
//Serialize lstExistingWalkingData
string output = JsonConvert.SerializeObject(lstExistingWalkingData);
//Save the Json file
using (StreamWriter w = new StreamWriter(_filePath))
{
w.WriteLine(output);
}
}
}
您分离了保存数据的职责(WalkingDataRepository
),并将其封装在WalkingDataManager
中。重构工作完全符合 SRP 所说的。简而言之,SRP 不仅要分解功能,还要封装它们!我也wrote an article了。
困扰我的是 anemic WalkingData
模型。这是一个反模式。您应该让 WalkingData
负责维护自己的数据,而不是创建另一个 class WalkingDataManager
来完成这项工作。
我正在学习 SOLID,我正在尝试理解单一职责原则。
classWalkingData存储了一个"date"和一个"walked distance"。 class也读取存储的数据。
public class WalkingData
{
public DateTime Date { get; set; }
public int WalkedDistance { get; set; }
private string _filePath = @"c:\Data\Json.txt";
//Read Data from Json File
public List<WalkingData> GetAll()
{
//If file does not exist returns an empty list
if (!File.Exists(_filePath)) return new List<WalkingData>();
string jsonData;
//Read the existing Json file
using (StreamReader readtext = new StreamReader(_filePath))
{
jsonData = readtext.ReadToEnd();
}
//Deserialize the Json and returs a list of WalkingData
return JsonConvert.DeserializeObject<List<WalkingData>>(jsonData);
}
//save an instance of WalkingData in Json file
public void Save()
{
List<WalkingData> lstExistingWalkingData = new List<WalkingData>();
//if existing data, load it into lstExistingWalkingData
if (File.Exists(_filePath))
lstExistingWalkingData = GetAll();
//Add the current instace into lstExistingWalkingData
lstExistingWalkingData.Add(this);
//Serialize lstExistingWalkingData
string output = JsonConvert.SerializeObject(lstExistingWalkingData);
//Save the Json file
using (StreamWriter w = new StreamWriter(_filePath))
{
w.WriteLine(output);
}
}
}
在我应用单一责任原则后,我有了新代码,我想确认我是否以合理的方式应用了该原则:
//This class is located on a library called BOL and has a reference to DAL library
public class WalkingData
{
public DateTime Date { get; set; }
public int WalkedDistance { get; set; }
}
//This class is located on a library called BOL and has a reference to DAL library
public class WalkingDataManager
{
WalkingDataRepository walkingDataRepository = new WalkingDataRepository();
public List<WalkingData> GetAll()
{
return walkingDataRepository.GetAll();
}
public void Save(WalkingData walkingData)
{
walkingDataRepository.Save(walkingData);
}
}
//this class is located in library Called DAL
internal class WalkingDataRepository
{
private string _filePath = @"c:\Data\Json.txt";
//Read Data from Json File
internal List<WalkingData> GetAll()
{
//If file does not exist returns an empty list
if (!File.Exists(_filePath)) return new List<WalkingData>();
string jsonData;
//Read the existing Json file
using (StreamReader readtext = new StreamReader(_filePath))
{
jsonData = readtext.ReadToEnd();
}
//Deserialize the Json and returs a list of WalkingData
return JsonConvert.DeserializeObject<List<WalkingData>>(jsonData);
}
//save an instance of WalkingData in Json file
internal void Save(WalkingData walkingData)
{
List<WalkingData> lstExistingWalkingData = new List<WalkingData>();
//if existing data, load it into lstExistingWalkingData
if (File.Exists(_filePath))
lstExistingWalkingData = GetAll();
//Add the current instace into lstExistingWalkingData
lstExistingWalkingData.Add(walkingData);
//Serialize lstExistingWalkingData
string output = JsonConvert.SerializeObject(lstExistingWalkingData);
//Save the Json file
using (StreamWriter w = new StreamWriter(_filePath))
{
w.WriteLine(output);
}
}
}
您分离了保存数据的职责(WalkingDataRepository
),并将其封装在WalkingDataManager
中。重构工作完全符合 SRP 所说的。简而言之,SRP 不仅要分解功能,还要封装它们!我也wrote an article了。
困扰我的是 anemic WalkingData
模型。这是一个反模式。您应该让 WalkingData
负责维护自己的数据,而不是创建另一个 class WalkingDataManager
来完成这项工作。