これは、開発者のニーズに基づいて展開および翻訳できるクラスとしてより適切に実装できるようです。以下は、いくつかのよく知られている定義済みの化学物質(プロパティとして)とクエリ可能なストア(via Add
およびGet
メソッド)を許可するために思いついたサンプルC#です。分子量や他の化学的性質を利用できるように非常に簡単に拡張することもできます。
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);