Is the connection inside an object viewed as an anti-pattern?

Suppose you have a class that is often (or even exclusively) used as part of a linked list. Is the anti-pattern to put information about the links inside the object?

For instance:

   public class Item
   {
       private Item prev; 
       private Item next;
       ...
   }

The often cited recommendation is simply to use a common container class (for example, java.util.LinkedListin Java), but this creates the overhead of having a separate node object (and the related object cannot easily refer to its siblings).

Thoughts?

+5
source share
8 answers

This is not necessarily an anti-pattern. Anti-patterns must have negative side effects in order to earn the right to “anti”.

For example, the structure Nodein the structure Treeshould need to associate it with the inside cache Node. Everything else will violate the much more important concept of encapsulating and localizing data and code based on the responsibility of the object.

, Customer, "" , , Customer : Customer Customer . , ; , , -.

+2

. , (, , , , ).

- " " . , , . , , , , ... , .

+2

-, , . , / , . YAGNI.

SRP, .

+2

- , . , "" "" , - , . , .

, , . , .

, , node - ? , , ( ), .

+2

, XML DOM java (- ) , , , , . , , , , , .

+2

Java , , Item . , , Item - , # , . , , , , , .

, , , .

+1

.

, , ( , , ).

+1

, / , . , : , , "", .

, , , ( , , ), "" .

Only when the project is large, and the linked list is an important part of the puzzle, should you spend time on it as flawlessly as possible. As a rule, if several source files need this linked list, then do it right, do not cut corners.

In general, know when and how to spend your energy. There is a very thin line to go between writing the right and reliable code and over-engineering.

+1
source

All Articles