Internal class with private fields exposed outside using interface

Let’s consider the following situation – there is an internal class Load in our library or plugin. We want to expose our private fields _Q1 and _Q2 as readonly fields using ILoad interface, but we still want to be able to modify the internal content of the fields from the class level. Please note that declaring our private fields using IForce interface makes our fields readonly also from the class level.

As you can see below we can cast our objects to an interfaces using public properties which exposes our objects as readonly fields outside the class.

C# code

    internal class Load : ILoad
    {
        private Force _Q1;
        private Force _Q2;

        public IForce GetQ1
        {
            get
            {
                return (IForce)_Q1;
            }
        }

        public IForce GetQ2
        {
            get
            {
                return (IForce)_Q2;
            }
        }

        public Load()
        {
            _Q1 = new Force();
            _Q2 = new Force();
        }

        public IAddQ1Condition WithQ1
        {
            get
            {
                return new AddQ1(_Q1);
            }
        }

        internal class AddQ1 : IAddQ1Condition
        {
            private Force _q1;

            public AddQ1(Force q1)
            {
                _q1 = q1;
            }

            public IAddQ1Condition Value(double value)
            {
                _q1.Value = value;
                return this;
            }
        }
    }

    public interface ILoad
    {
        IForce GetQ1
        {
            get;
        }

        IForce GetQ2
        {
            get;
        }

        IAddQ1Condition WithQ1 { get; }

        IAddQ2Condition WithQ2 { get; }
    }

    public interface IForce
    {
        double GetValue
        {
            get;
        }

        double GetPosition
        {
            get;
        }

        double GetApplicationLevel
        {
            get;
        }
    }

C# result

Method overriding in C# – part 3

Let’s analyze just one more example of method overriding. In this case we have got three classes. TopClass is derived from IntermediateClass. IntermediateClass is derived from BaseClass. What if we are in the TopClass level and we want to perform some operations at the lower level and then go back, to the top level?

Well, look at the SomeJob method:

C# code

 class Program
    {
        static void Main(string[] args)
        {
            BaseClass bc = new BaseClass();
            bc.InvokeMethod();

            IntermediateClass ic = new IntermediateClass();
            ic.InvokeMethod();

            TopClass tc = new TopClass();
            tc.SomeJob();

            Console.ReadLine();
        }
    }

    public class BaseClass
    {
        public virtual void InvokeMethod()
        {
            Console.WriteLine("This is the base method");
        }
    }

    public class IntermediateClass : BaseClass
    {
        public override void InvokeMethod()
        {
            Console.WriteLine("This is the intermediate method");
        }

        public void IntermediateMethod()
        {
            // let's do another job here
            Console.WriteLine("Some job from Intermediate class level");
            InvokeMethod();
        }
    }

    public class TopClass : IntermediateClass
    {
        public override void InvokeMethod()
        {
            Console.WriteLine("This is the top method");
        }

        public void SomeJob()
        {
            // let's do some job here
            Console.WriteLine("Some job from Top class level");
            IntermediateMethod();
        }
    }

C# result
This is the base method
This is the intermediate method
Some job from Top class level
Some job from Intermediate class level
This is the top method