Abstract Data Types (TAD) doubt

0

Recently we were applying an example with a stack algorithm where the last element that is added is the first one that comes out.

Having done this, we were asked to remain encapsulated and, by the way we did it, the problem arose that when we used it by using an arraylist, it still responded to access by indexes. For example [1] (which was wrong, because I would only have to respond to the methods of the TAD). Then we were given a complete example of how the encapsulation should be done (which was a topic we had already seen), but I could not understand the part where the encapsulation is performed with private .

I understand that private is used to perform the encapsulation, but I asked myself why was not there a'get' or a'et 'to be used in those cases, right?

If anyone besides this can give me a definition of TAD or the way they are used, I would appreciate it.

static void Main(string[] args)
{

    Pila pila = new Pila();
    pila.push(1);
    pila.push("A");
    pila.push(2);
    Console.WriteLine(pila.pop());


    Console.ReadKey();
}
public class Pila
{
    private ArrayList elementos;
    public void push(object elemento)
    {
        elementos.Add(elemento);
    }
    public object pop()
    {
        object elemento = elementos[elementos.Count - 1];
        elementos.Remove(elemento);
        return elemento;
    }
    public Pila()
    {
        elementos = new ArrayList();
    }

}
    
asked by Shiki 29.05.2017 в 06:12
source

1 answer

2

The Encapsulation can be defined as:

A strategy that captures information and functions within a simple unit (called a class). Contains and hides information about the objects you use, such as data structures and code.

In my opinion, the key ideas of the encapsulation are:

  • Hide complexity.
  • Group and relate data and functions.
  • "Complicated" methods should be (where possible) private.
  • Instances of variables must be private.
  • Hide unnecessary data and functions for the end user (of the class).

In your example it is not necessary to expose any property (get / set) since the idea of a simple stack is to implement the put and take actions.

On the other hand this stack is implemented with an ArrayList but I could calmly change it to a List or a dictionary and the user would never know that the implementation changes. That is the magic of Encapsulation.

    
answered by 29.05.2017 / 16:14
source