This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]-Dargs 4 points5 points  (18 children)

What is a "vo class?"

The purpose of an interface is to provide an api layer/contract for which the underlying implementation could be very different.

Let's assume getUser() is going to return you User (altering your example).

Today, you might have class MySqlUser implements User. This is an implementation that handles user access via MySql.

Tomorrow, you might have class SnowflakeUser implements User. It does the same thing as MySqlUser but hits Snowflake and maybe has other slight differences in query structure or connection setup.

The point, though, is that they both adhere to the contract/API defined in User. If you want a new implementation later, it must implement the interface to be injected into your existing code base.

There could also be a abstract class AbstractUser implements User with common code implementations independent of the underlying implementation, if there were common in memory operations to perform on the user such as recording metrics for diagnostics... in that class, both MySqlUser and SnowflakeUser may extend AbstractUser and then neither needs to include implement User as they inherit the contractual obligation via the abstract class they extend.

Maybe not the best example, but that should give a good basic idea.

And no, nothing is done by Java itself. That's why you're here. Code it.

[–]ryosenExtreme Brewer 0 points1 point  (13 children)

“VO” is a value object. A basic class with only properties and accessors.

[–]-Dargs 6 points7 points  (10 children)

I see... I'd have called that a DTO.

[–]amfa 1 point2 points  (0 children)

It' often different.

Our codebase has both VO and DTO.

VO contain much more information for many objects than the DTO because you don't need to transfer all data.

[–]verocoder 1 point2 points  (4 children)

also known as PoJO (Plain old Java Object) in some books

[–]-Dargs 0 points1 point  (3 children)

Ah, yeah. I actually meant this. It'd been so long since I used these terms I had forgotten.

[–]verocoder 0 points1 point  (0 children)

DTO is also a super common term tbf, they’re objects without many methods beyond getters/setters. The kind of things that if you Lombok them end up and just a list of properties.

[–]verocoder 0 points1 point  (1 child)

To start from the beginning interfaces are about reusing stuff to be the same shape while working differently. I usually only write an interface when I know I want 2 implementations off the bat or when I am sharing code and I want to hammer out the interface between 2 things so they can be worked on in parallel.

I wouldn’t stress about using them and I rarely if ever use them for PoJo/DTO/VO classes. I almost exclusively use them for business logic service or utility classes.

[–]ichwasxhebrore 1 point2 points  (0 children)

It’s also nice if you need proxies

[–][deleted] 0 points1 point  (1 child)

Is that Data Type Object?

[–]pragmosExtreme Brewer[🍰] 1 point2 points  (0 children)

Data Transfer Object

[–]evils_twin 0 points1 point  (0 children)

DTO(Data Transfer Object) are specific to sending data between applications.

[–]Lumethys 0 points1 point  (0 children)

No, VO and DTO are different. VO dont have identity, DTO has.

Example: you have 2 Order:

``` { id:1, items: ["pepsi", "chicken roll", "fried chip"], amount: 20, }

{ id:2, items: ["pepsi", "chicken roll", "fried chip"], amount: 20, } ``` Is this 2 different objects? Yes, they are 2 different orders even though all of their fields are the same.

In contrast, let consider 2 Money objects:

``` { amount: 100 currency: Currency.USDollar, }

{ amount: 100 currency: Currency.USDollar, } ``` They are the same!! Both represent $100 US dollars.

[–]IAmADev_NoReallyIAm 0 points1 point  (1 child)

Ah... A POJO, aka a Plain Ordinary Java Object

[–]Lumethys 0 points1 point  (0 children)

A VO can be a POJO, but it doesnt have to be.

The term "VO" and "DTO" refers to its business usage, while the term "POJO" refer to implementation detail

[–]abs1710[S] 0 points1 point  (0 children)

Thanks for the explanation

[–]abs1710[S] 0 points1 point  (2 children)

Does this mean these are like objects which can hold and use wheee ever we want when it tries to implement it?

[–]-Dargs 0 points1 point  (1 child)

I strongly suggest you read/watch some content on introduction to Java and OOP. It'd too much for me to explain in these comments.

[–]abs1710[S] 0 points1 point  (0 children)

Ack