POV
POV

Reputation: 12015

Is there sense of using interface to create class model?

Interface is:

export interface IAddEditGeneralDictionary {
      Code: string;
      StartDate?: Date | string;
      FinishDate?: Date | string;
      Name: string;
    }

Realization is:

export class AddEditGeneralDictionary implements IAddEditGeneralDictionary {
  constructor(public Code: string,
    public StartDate: Date | string,
    public FinishDate: Date | string,
    public Name: string){
}

I tried to make properties as private and use set/get, but interface does not allow me to do that.

Does it make sense to use interface to build model class?

Upvotes: 0

Views: 44

Answers (1)

seanplwong
seanplwong

Reputation: 1091

Interface is a shared boundary for different components/entity to communicate. It is supposed to be public so that users know what they are expecting, when they are invoking a concrete class from an interface.

Whatever private property or method they are implementation details. Just do it in the concrete class.

For example

interface Vehicle {
  start(): void;
}

class Car implements Vehicle {
  private engine;
  private wheels;
  public start(): void {}
}

class Jet implements Vehicle {
  private engine;
  private airframe;
  public start(): void {}
}

Implementation can take their freedom to define private properties but private properties are not very useful in interface.

Upvotes: 1

Related Questions