Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with "global" data structures in an object-oriented world

Tags:

oop

This is a question with many answers - I am interested in knowing what others consider to be "best practice".

Consider the following situation: you have an object-oriented program that contains one or more data structures that are needed by many different classes. How do you make these data structures accessible?

  1. You can explicitly pass references around, for example, in the constructors. This is the "proper" solution, but it means duplicating parameters and instance variables all over the program. This makes changes or additions to the global data difficult.

  2. You can put all of the data structures inside of a single object, and pass around references to this object. This can either be an object created just for this purpose, or it could be the "main" object of your program. This simplifies the problems of (1), but the data structures may or may not have anything to do with one another, and collecting them together in a single object is pretty arbitrary.

  3. You can make the data structures "static". This lets you reference them directly from other classes, without having to pass around references. This entirely avoids the disadvantages of (1), but is clearly not OO. This also means that there can only ever be a single instance of the program.

When there are a lot of data structures, all required by a lot of classes, I tend to use (2). This is a compromise between OO-purity and practicality. What do other folks do? (For what it's worth, I mostly come from the Java world, but this discussion is applicable to any OO language.)

like image 672
Brad Richards Avatar asked Oct 01 '08 08:10

Brad Richards


People also ask

What is an object-oriented data structure?

An object-oriented database (OOD) is a database system that can work with complex data objects — that is, objects that mirror those used in object-oriented programming languages. In object-oriented programming, everything is an object, and many objects are quite complex, having different properties and methods.

What is a global data structure?

Conceptually, global data structures have a defined storage mode consistent with those used by the serial ESSL library, except for real symmetric and complex Hermitian tridiagonal matrices.

What is the main approach used by OOP to solve problems?

The object-oriented problem solving approach, in general, can be devided into four steps. They are: (1) Identify the problem, (2) Identify the objects needed for the solution, (3) Identify messages to be sent to the objects, and (4) Create a sequence of messages to the objects that solve the problem.

Is Oops required for DSA?

No,OOP concepts are not required for competitive programming.


1 Answers

Global data isn't as bad as many OO purists claim!

After all, when implementing OO classes you've usually using an API to your OS. What the heck is this if it isn't a huge pile of global data and services!

If you use some global stuff in your program, you're merely extending this huge environment your class implementation can already see of the OS with a bit of data that is domain specific to your app.

Passing pointers/references everywhere is often taught in OO courses and books, academically it sounds nice. Pragmatically, it is often the thing to do, but it is misguided to follow this rule blindly and absolutely. For a decent sized program, you can end up with a pile of references being passed all over the place and it can result in completely unnecessary drudgery work.

Globally accessible services/data providers (abstracted away behind a nice interface obviously) are pretty much a must in a decent sized app.

like image 134
Scott Langham Avatar answered Nov 13 '22 18:11

Scott Langham