Why Arraylist instead of Stack in the implementation of the Memento standard?

Asked

Viewed 212 times

7

As you all know, the Memento pattern is the pattern that saves different states of objects and then retrieves them.

The intention is not to retrieve the "last" to enter and then remove it? This is a stack, right?

So why do the examples use ArrayList taking the index element (quantity - 1) and not using Stack straightforward?

There’s an explanation for this?

Example:

I’m using C#, but I can understand java, so this question is for both.

In C# I’m using List and to recover I’m using

TextoMemento estadoSalvo = estados.ElementAt(estados.Count -1);
        estados.RemoveAt(estados.Count - 1);

Why not Stack?

  • What examples? Without seeing what you’re talking about, you don’t know. If you don’t like one example, take one. If you don’t like any, leave them. This example has neither a list nor a stack (and it is canonical): http://www.dofactory.com/net/memento-design-pattern. This also does not http://blogs.microsoft.co.il/gilf/2008/08/01/memento-pattern/. Using a battery for specific need: http://www.codeproject.com/Articles/18025/Generic-Memento-Pattern-for-Undo-Redo-in-C

  • Check out these examples: http://en.wikipedia.org/wiki/Memento_pattern and https://brizeno.wordpress.com/category/padroes-de-projeto/mediator/ http://imasters.com.br/artigo/16994/java/padrao-de-projetos-com-memento/ I’m running from Brizeno’s website first. also: http://sourcemaking.com/design_patterns/Memento/java/1

2 answers

6


In Java, classes that inherit from Vector (including Stack) sane thread-safe, that is to say: they carry the overhead to prevent two threads access at the same time the same collection. The use of a ArrayList (that is not thread-safe) avoid that overhead, and should therefore be preferable to Stack unless this feature is important in its application.

Furthermore, if you prefer to use a semantically appropriate class, there is the Deque and its various implementations, which can also be used as if it were a stack:

Método do Stack | Método equivalente do Deque
----------------+----------------------------
push(e)         | addFirst(e)
pop()           | removeFirst()
peek()          | peekFirst()

(Naturally, this answer holds true for Java only. I don’t have enough C# experience to comment on the libraries of this language.)

  • Ah, so that’s it! Thank you very much! Thank you for the answer. Hugs.

3

Examples are just examples. Each one does as they see fit. If you want to know why and it is not explained where you read it, you should ask who made the example. It must have a reason.

mgibsonbr has already given a good reason for doing so with Java. In C# the implementation of the class Stack<T>, at least in its generic form (never used the legate form) is not competing. If need competition should use the ConcurrentStack<T>.

In fact this practical example in C# shows that the stack is used.

Browser other questions tagged

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