Theoretical doubt - Interface, unique responsibility

Asked

Viewed 360 times

7

Presentation:

I created a photo.Cs class that should be responsible for:

  • Calculate the angle of view of the lens;
  • Receive lens zoom (in mm)
  • Receive the cut factor (value multiplied by the lens zoom shows the actual lens value)
  • Dimensions of the camera sensor, 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();
    }
}

so 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;
        }
    }
}

Doubts:

  1. Is this class doing more than it should? because each method really does only 1 thing, but the photography class does everything in relation to photography. On the sole responsibility this correct?
  2. I implemented the Interface and the class correctly?

I’m calling it that on the show

IFotografia ft = new fotografia();
            ft.CameraFabricante(_CamerasFabricante.Canon);
            ft.ObjetivaMM = 50;
            ft.SensorHmm = 22.3;
            ft.FatordeCorte = 1.6;
            var t = ft.CalculoAnguloVisaoH();
  • Relevant: http://qualityisspeed.blogspot.com.br/2014/08/why-i-dont-teach-solid.html

2 answers

6


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

Removed from the documentation of the principles:

In the context of the Principle of Sole 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 than one responsibility. That’s hard to watch sometimes. We are used to thinking of group responsibilities. 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 any were there are two responsibilities shown here. The first is connection management. The second is data communication. Dial and hangup functions manage modem connection, while send and recv functions communicate data.

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

Here has a more complete description of the principles.

Soon to answer this principle you should keep in mind how your application will change. If you’re gonna change these things:

  1. Calculate the angle of view of the lens;
  2. Receive lens zoom (in mm)
  3. Receive the cut factor (value multiplied by the lens zoom shows the actual lens value)
  4. Dimensions of the camera sensor, etc.

So your class has many responsibilities, if not modify it is fully compatible with this principle.

  • I understand and it becomes even more difficult to decide, on the one hand this software is something new, non-existent, so it will definitely have thousands of changes, because in the future it will be open source..(There is no such solution for prototyping in C#, I don’t think even in the Arduino) But on the other hand creating a class for each function of that class seems like a lot! until pq "Receive lens zoom" is just a get;set; "receive cut factor", tb. Calculate real objective, basically is a value * the other, I think it does not compensate...anyway. .difícil.. I think I’ll leave everything together for now..

  • 1

    It hasn’t changed correctly yet? So its interface is principle compliant. Wait for the new requirements to re-evaluate. From the little I understand of the domain of the problem ( Photograph ) I believe that these settings are recorded during the photo and do not change later. Then it would be another class, perhaps photography, create these values and insert it into the photograph. After that these values never change.

  • ....Then it would be of another class, who class Machinfotografica, ....

3

This is correct, but you should also create the methods in the interface, not only 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()
}
  • Tchicotti, really had already put, I forgot to update in the post...thanks. But Sensorhmm, Sensorvmm are properties that I will only post values, I must put get/set?

  • but at some point you will recover these values or not?

  • In fact only internal functions will recover.

  • 1

    then you need to allow get/set

  • and sole responsibility, my class does not do many things? or does it mean that each property should make 1 single goal?

  • 1

    Each property already has its own responsibility... an interface like yours, aims to aggregate or centralize responsibilities in a class. then it is correct, mainly by the fact of aggregating what is related to the execution of the camera... now if it also contained for example something related to the registration of users for example, that left the context of it, it would be wrong

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.