Is the code-interface principle used for entity classes?

I am trying to execute a code interface for a project. Should I first create an interface and then implement this interface for entity classes? I think that this can lead to the fact that the first approach of the interface is too far, and objects should be ignored. This is what I mean ...

public interface Address {
  public String getStreet();
  public void setStreet(String street);
}

@Entity
public class AddressImpl implements Address {
  private String street;

  public String getStreet(){
    return this.street;
  }
  public void setStreet(String street){
    this.street = street;
  }
}

@Entity
public class OfficeImpl /* implements Office */ {
  private Address location;

  public Address getLocation(){
    return this.location;
  }
  public void setLocation(Address location){
    this.location = location;
  }
}

public class Driver {
  public static void main(String[] args) {
    Office work = new OfficeImpl();
    Address workAddress = new AddressImpl();
    workAddress.setStreet("Main St.");
    work.setLocation(workAddress);
  }
}
+2
source share
5 answers

I think that creating interfaces for objects is probably not necessary.

The goal of creating interfaces (or at least one of the goals) is to make it easier to replace one specific implementation in favor of another. This is definitely good for your DAO, Business Logic, etc.

, !

+4

, , , , , , , .

, , , "" , . , , , , .

+3

Entities , Entities!

public interface IEntity
{
    int EntityId { get; set; }
    bool FindById(int id);
    bool Create(object [] values);
    bool Delete(int id);
    //etc.
}

#, . " ".

+3

, , , , .

, , , . , :

interface IFlaggable {
  bool IsFlagged ...
  string Reason ...
}

class ForumPost implements IFlaggable { }

class PrivateMessage implements IFlaggable { }

, !

+1

I do not do interfaces for storing beans data at all, that is, I do not create interfaces for classes with primitive type values ​​and getters / seters for them. In fact, I never faced the moment when I needed interfaces for everything that I usually use for them (polymorphism and mockery, basically), so I did not worry about that.

I think I should note that most of the time when I use databeans, I also reflect the values ​​of the same objects with custom classes that work as follows:

Reflector r = new Reflector(new DataBean( [ values given through constructor ] ));
long someNumber = r.get("method", Long.class);
0
source

Source: https://habr.com/ru/post/1733956/


All Articles