似乎可以将其更好地实现为可以根据开发人员的需求进行扩展和翻译的类。以下是一些我想出的示例C#,它允许使用一些众所周知的定义化学品(作为属性),然后允许查询存储(via Add
和Get
方法)。您还可以轻松扩展以提供您的摩尔质量和其他化学性质。
public interface IChemical
{
string AtomicFormula
{
get;
}
string ChemicalName
{
get;
}
string AbbreviatedChemicalName
{
get;
}
}
public sealed class Chemical : IChemical
{
private static readonly IChemical h2so4 = new Chemical("H2SO4", "sulfuric acid", "sulf. acid");
private static readonly IChemical hcl = new Chemical("HCl", "hydrochloric acid", "hydro. acid");
private static readonly IDictionary<string, IChemical> chemicalsByAtomicFormula = new Dictionary<string, IChemical>();
private static readonly IDictionary<string, IChemical> chemicalsByChemicalName = new Dictionary<string, IChemical>();
private static readonly IDictionary<string, IChemical> chemicalsByAbbreviatedChemicalName = new Dictionary<string, IChemical>();
private readonly string atomicFormula;
private readonly string chemicalName;
private readonly string abbreviatedChemicalName;
static Chemical()
{
chemicalsByAtomicFormula.Add(h2so4.AtomicFormula, h2so4);
chemicalsByChemicalName.Add(h2so4.ChemicalName, h2so4);
chemicalsByAbbreviatedChemicalName.Add(h2so4.AbbreviatedChemicalName, h2so4);
chemicalsByAtomicFormula.Add(hcl.AtomicFormula, hcl);
chemicalsByChemicalName.Add(hcl.ChemicalName, hcl);
chemicalsByAbbreviatedChemicalName.Add(hcl.AbbreviatedChemicalName, hcl);
}
public Chemical(string atomicFormula, string chemicalName, string abbreviatedChemicalName)
{
if (string.IsNullOrWhiteSpace(atomicFormula))
{
throw new ArgumentException("Atomic formula may not be null or whitespace.", "atomicFormula");
}
if (string.IsNullOrWhiteSpace(chemicalName))
{
throw new ArgumentException("Chemical name may not be null or whitespace.", "chemicalName");
}
if (string.IsNullOrWhiteSpace(abbreviatedChemicalName))
{
throw new ArgumentException("Abbreviated chemical name may not be null or whitespace.", "abbreviatedChemicalName");
}
this.atomicFormula = atomicFormula;
this.chemicalName = chemicalName;
this.abbreviatedChemicalName = abbreviatedChemicalName;
}
public static IChemical H2SO4
{
get
{
return h2so4;
}
}
public static IChemical HCl
{
get
{
return hcl;
}
}
public string AtomicFormula
{
get
{
return this.atomicFormula;
}
}
public string ChemicalName
{
get
{
return this.chemicalName;
}
}
public string AbbreviatedChemicalName
{
get
{
return this.abbreviatedChemicalName;
}
}
public static void AddChemical(IChemical chemical)
{
if (chemical == null)
{
throw new ArgumentNullException("chemical", "chemical may not be null");
}
if (chemicalsByAtomicFormula.ContainsKey(chemical.AtomicFormula))
{
return;
}
chemicalsByAtomicFormula.Add(chemical.AtomicFormula, chemical);
if (chemicalsByChemicalName.ContainsKey(chemical.ChemicalName))
{
return;
}
chemicalsByChemicalName.Add(chemical.ChemicalName, chemical);
if (chemicalsByAbbreviatedChemicalName.ContainsKey(chemical.AbbreviatedChemicalName))
{
return;
}
chemicalsByAbbreviatedChemicalName.Add(chemical.AbbreviatedChemicalName, chemical);
}
public static IChemical GetChemicalByAtomicFormula(string atomicFormula)
{
if (string.IsNullOrWhiteSpace(atomicFormula))
{
throw new ArgumentException("Atomic formula may not be null or whitespace.", "atomicFormula");
}
IChemical chemical;
return chemicalsByAtomicFormula.TryGetValue(atomicFormula, out chemical) ? chemical : null;
}
public static IChemical GetChemicalByChemicalName(string chemicalName)
{
if (string.IsNullOrWhiteSpace(chemicalName))
{
throw new ArgumentException("Chemical name may not be null or whitespace.", "chemicalName");
}
IChemical chemical;
return chemicalsByChemicalName.TryGetValue(chemicalName, out chemical) ? chemical : null;
}
public static IChemical GetChemicalByAbbreviatedChemicalName(string abbreviatedChemicalName)
{
if (string.IsNullOrWhiteSpace(abbreviatedChemicalName))
{
throw new ArgumentException("Abbreviated chemical name may not be null or whitespace.", "abbreviatedChemicalName");
}
IChemical chemical;
return chemicalsByAbbreviatedChemicalName.TryGetValue(abbreviatedChemicalName, out chemical) ? chemical : null;
}
}
您可以添加新的化学物质,例如:
Chemical.AddChemical(new Chemical("Na2SiF6", "sodium hexafluorosilicate", "sod. hex.flu.sil."));
并获得其他位:
Console.WriteLine(Chemical.GetChemicalByChemicalName("sulfuric acid").AtomicFormula);