Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>What you can do is let the TempControllers be responsible for persisting itself using a generic archiver.</p> <pre><code>class TempController { private Temperature _setPoint; public Temperature SetPoint { get; set;} public ImportFrom(Archive archive) { SetPoint = archive.Read("SetPoint"); } public ExportTo(Archive archive) { archive.Write("SetPoint", SetPoint); } } class AdvancedTempController { private Temperature _setPoint; private Rate _rateControl; public Temperature SetPoint { get; set;} public Rate RateControl { get; set;} public ImportFrom(Archive archive) { SetPoint = archive.Read("SetPoint"); RateControl = archive.ReadWithDefault("RateControl", Rate.Zero); } public ExportTo(Archive archive) { archive.Write("SetPoint", SetPoint); archive.Write("RateControl", RateControl); } } </code></pre> <p>By keeping it this way, the controllers do not care how the actual values are stored but you are still keeping the internals of the object well encapsulated.</p> <p>Now you can define an abstract Archive class that all archive classes can implement.</p> <pre><code>abstract class Archive { public abstract object Read(string key); public abstract object ReadWithDefault(string key, object defaultValue); public abstract void Write(string key); } </code></pre> <p>FormatA archiver can do it one way, and FormatB archive can do it another.</p> <pre><code>class FormatAArchive : Archive { public object Read(string key) { // read stuff } public object ReadWithDefault(string key, object defaultValue) { // if store contains key, read stuff // else return default value } public void Write(string key) { // write stuff } } class FormatBArchive : Archive { public object Read(string key) { // read stuff } public object ReadWithDefault(string key, object defaultValue) { // if store contains key, read stuff // else return default value } public void Write(string key) { // write stuff } } </code></pre> <p>You can add in another Controller type and pass it whatever formatter. You can also create another formatter and pass it to whichever controller.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload