# Data objects and Structures

Data Structure AlgorithmsAnalysis of AlgorithmsAlgorithms

## Basic concept

Data structures are defined as special classes implemented to hold data only, i.e. Pure models, e.g. Car, Kid, Animal, Event, Employee, Company, Customer ...etc. Those data are generally declared or considered as instance variables in other classes beginnings.

The methods of this class should not perform any real significant work, otherwise the data structure class is not a data structure anymore!

So mainly, the methods are getters and setters (i.e. accessors and mutators), generally because the instance variables are treated as private. There is alternative opinion: that Data structure variables should be public, and can be accessed directly from the instance of the class, but it is debatable that the private variables concept is better.

• In that context, the data structure class, reveals or exposes its data (variables) and have no meaningful (significant) methods or functions.
• A normal class (Called Object here), like MainActivity, ListAdapter, Calculator, Iterator, conceals their data, and reveals or exposes their methods that work on those data.

So, we have two approaches to solve the problem in hand, i.e. implement a data Structure in its purest form, while building another Object class to build operations on their data, OR, we can make the model classes as Object classes, that conceals their data while exposes their methods, like in the following example

public class Square {
public Point topLeft1;
public double side1;
}
public class Rectangle {
public Point topLeft1;
public double height1;
public double width1;
}
public class Circle {
public Point center1;
}
public class Geometry {
public final double PI = 3.141592653589793;
public double area(Object shape) throws NoSuchShapeException {
if (shape instanceof Square) {
Square s = (Square)shape;
return s.side1 x s.side1;
}else if (shape instanceof Rectangle) {
Rectangle r = (Rectangle)shape;
return r.height1 x r.width1;
}else if (shape instanceof Circle) {
Circle c = (Circle)shape;
throw new NoSuchShapeException(); }
}

In this solution the shapes are treated as data structures, while Geometry class is treated as Object.

Advantage − If we require adding more methods we will add them only in the Geometry class (this is the time this solution should be implemented).

Disadvantage − If we require to add more Data Structures (i.e. more shapes), we will have to change all your methods in the Geometry class.

## Difference between Data structures and Objects

• Objects expose behaviour and conceal data. This makes it simple to add new kinds of objects without changing existing behaviours. It also makes it difficult to add new behaviours to existing objects.
• Data structures reveal or expose data and have no significant behaviour. This makes it simple to add new behaviours to existing data structures but makes it hard to add new data structures to existing functions.
Published on 08-Jan-2020 11:06:18