OOP. Abstraction

      Comments Off on OOP. Abstraction

This topic contains 0 replies, has 1 voice, and was last updated by  Васильев Владимир Сергеевич 3 weeks ago.

  • Author
    Posts
  • #4077

    Abstraction is a model of a future data type, but without any specific implementation of its functionality in the form of algorithms. Defining data types through abstraction provides flexibility and extensibility of your code.

    Abstraction lists all the main characteristics of the future types. But each data type, which implements the abstraction, will need to define its own logic in each of its methods.
    In other words, Abstraction is a contract, signed by data types, if they want to implement it.

    Let’s assume that our application performs logging of some calculation process.

    In some cases, we need to perform logging to the SQL database, and sometimes – just to an XML file, or even just to a text file. Logging can be made in many different ways, and trying to predict all the options for every occasion, defining them directly within the main logic class through overloaded methods – is a bad idea.

    It makes more sense to define a separate data type, which will be responsible just for the functionality of logging. And each time we need a different algorithm of logging we create a class that implements the abstraction and provides the required logic in its methods. Thus all these classes will implement the same base interface – ILog

    // abstraction 
    interface ILog 
    {
           void LogData (double num); 
    }
    // implementation of abstraction
    class DBLogger: ILog 
    {
          public void LogData (double num) 
         {
            // Logging to database 
           // ... 
         } 
    }
    // class that performs the calculations 
    class Calculator 
    {
         ILog log; 
       public Calculator (ILog log) 
        {
          // set a particular type of logging 
           this.log = log; 
        } 
        public void Total (params double [] arr) 
        {
          double result = 0; 
          // calculate
          // ...
          // do logging 
          log.LogData (result); 
        } 
    }

    In this example, the interface ILog is an abstraction. Class BDLogger – is its implementation, which defines how the record is written into the Sql database.

    The Calculator class constructor allows to determine which type of logger will be used in this instance of the class. Due to the fact that the constructor of class Calculator takes a reference to ILog interface (abstraction) as a parameter, we can pass an object of any class that implements this abstraction.

    Now let’s see how it works:

    class Program 
    {
        static void Main () 
        {
          DBLogger logger = new DBLogger (); 
          Calculator calc = new Calculator (logger); 
          calc.Total (); 
        } 
    }

    If we need a different way of logging in future, for example, logging to an XML file, we shall write a new class implementing ILog interface. In its method Log Data() we shall encapsulate algorithm of writing log information to a file of XML format. We use the same option for the other similar cases.

    Thus we can say that constructing a new class according to a predetermined pattern of functionality is the main idea of Abstraction.

You must be logged in to reply to this topic.