Theoretical doubt - Interface, single responsibility

7

Presentation:

I created a class fotografia.cs that should be responsible for:

  • Calculate the angle of view of the lens;
  • Lens zoom (in mm)
  • Receive the cut factor (value multiplied by the zoom of the lens shows the actual lens value)
  • Camera sensor dimensions, etc.

I thought about creating the interface, more for academic purposes than practical.

Interface

namespace fotografia
{
    public enum _CamerasFabricante { Canon=0, Nikon = 1, Sony = 2 };
    public interface IFotografia
    {
        int CameraFabricante(_CamerasFabricante _value);
        double FatordeCorte { get; set; }
        int ObjetivaMM { get; set; }

        double SensorHmm { get; set; }
        double SensorVmm { get; set; }
        double CalculoAnguloVisaoH();
        double CalculoAnguloVisaoV();
    }
}

I created the class for this interface:

using System;

namespace fotografia
{
    internal class fotografia : IFotografia
    {
        public double FatordeCorte { get; set; }

        public int ObjetivaMM { get; set; }

        public int CameraFabricante(_CamerasFabricante _value)
        {
            return (int)_value;
        }

        private double _MMFinalObjetiva()
        {
            return (Convert.ToDouble(ObjetivaMM) * FatordeCorte);
        }

        public double SensorHmm
        {
            get;
            set;
        }

        private double myVar;

        public double SensorVmm
        {
            get { return myVar; }
            set { myVar = value; }
        }

        public double CalculoAnguloVisaoH()
        {
            double fov = SensorHmm / (2 * _MMFinalObjetiva());

            double arctan = 2 * Math.Atan(fov)  * 180.0 / Math.PI;
            return arctan;
        }

        private double CalculoAnguloVisaoV()
        {
            double fov = SensorVmm / (2 * _MMFinalObjetiva());

            double arctan = 2 * Math.Atan(fov) * 180.0 / Math.PI;
            return arctan;
        }
    }
}

Inquiries:

  • Is this class doing more things than it should? because each method actually does only 1 single thing, however the class photography does everything in relation to photography. About single responsibility is this correct?
  • Implemented Interface and class correctly?
  • I'm calling it like this in the program

    IFotografia ft = new fotografia();
                ft.CameraFabricante(_CamerasFabricante.Canon);
                ft.ObjetivaMM = 50;
                ft.SensorHmm = 22.3;
                ft.FatordeCorte = 1.6;
                var t = ft.CalculoAnguloVisaoH();
    
        
    asked by anonymous 24.03.2014 / 14:52

    2 answers

    6

    The principle of sole responsibility is part of the SOLID principles created by Robert C. Martin as a design guide to follow in your code but not as an obligation.

    Retrieved from the documentation of the principles:

      

    In the context of the Principle of Single Responsibility we define responsibility as being   "the reason for change". If you think of one or more reasons to change a class, then this class has more of a responsibility. This is hard to see sometimes.   We are accustomed to thinking about responsibilities in groups. For example consider the following Modem interface:

    public interface Modem
    {
    
        public void Dial(string pno);
        public void Hangup();
        public void Send(char c);
        public char Recv();
    }
    
      

    Most of us agree that this interface seems perfectly reasonable.   The four functions it declares are certainly functions belonging to a modem.

         

    Of whatever have been there are two responsibilities shown here. The first is connection management. The second is data communication. The dial and hangup functions manage the modem connection, while the send and recv functions communicate data.

         

    Should these two responsibilities be separate?   This will depend on how the application will change.

    Here has a more complete description of the principles.

    Soon to respond to this principle you should keep in mind how your application will change. If you will modify these things:

  • Calculate the angle of view of the lens;
  • Lens zoom (in mm)
  • Receive the cut factor (value multiplied by the zoom of the lens shows the actual lens value)
  • Camera sensor dimensions, etc.
  • So your class has many responsibilities, if you do not modify it, it's fully compatible with this principle.

        
    24.03.2014 / 16:29
    3

    That's right, but you should also create the methods in the interface, not just the properties.

    public enum _CamerasFabricante { Canon=0, Nikon = 1, Sony = 2 };
    public interface IFotografia
    {
        int CameraFabricante(_CamerasFabricante _value);
        double FatordeCorte { get; set; }
        int ObjetivaMM { get; set; }
    
        double SensorHmm { get; set; }
        double SensorVmm { get; set; }
    
        double CalculoAnguloVisaoH()
        double CalculoAnguloVisaoV()
    }
    
        
    24.03.2014 / 14:59