SOLID: Interface SegregationPrinciple — With Real Life Example

export default interface IUser {
email: string;
password: string;
firstName: string;
lastName: string;
pointOfOrder: number;
hourOfWork: number;

salary: number;
discount: number;
}
import IUser from "./IUser";

export default class User implements IUser {
readonly email: string;
readonly password: string;
readonly firstName: string;
readonly lastName: string;
readonly pointOfOrder: number;
readonly hourOfWork: number;

constructor(
firstName: string,
lastName: string,
email: string,
password: string,
pointOfOrder: number,
hourOfWork: number
) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.password = password;
this.pointOfOrder = pointOfOrder;
this.hourOfWork = hourOfWork;
}

get salary(): number {
return this.hourOfWork * 10 * 26;
}

get discount(): number {
return this.pointOfOrder === 0 ? 0 : this.pointOfOrder / 100;
}
}
import IUser from "./user/IUser";
import User from "./user/User";

(() => {
const sami: IUser = new User(
"sami",
"ibrahimbaş",
"info@ssibrahimbas.com",
"1234",
0,
40
);
const zuck: IUser = new User(
"mark",
"zuckerberg",
"info@meta.com",
"1234",
34567,
0
);
})();
export default interface IUser {
email: string;
password: string;
firstName: string;
lastName: string;
}
import IUser from "../abstract/IUser";

export default class User implements IUser {
readonly email: string;
readonly password: string;
readonly firstName: string;
readonly lastName: string;

constructor(
firstName: string,
lastName: string,
email: string,
password: string
) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.password = password;
}
}
import IUser from "./IUser";

export default interface IEmployee extends IUser {
hourOfWork: number;
salary: number;
}
import User from "./User";
import IEmployee from "../abstract/IEmployee";

export default class Employee extends User implements IEmployee {
readonly hourOfWork: number;
constructor(
firstName: string,
lastName: string,
email: string,
password: string,
hourOfWork: number
) {
super(firstName, lastName, email, password);
this.hourOfWork = hourOfWork;
}

get salary(): number {
return this.hourOfWork * 23 * 26;
}
}
import IUser from "./IUser";

export default interface ICustomer extends IUser {
pointOfOrder: number;
discount: number;
}
import User from "./User";
import ICustomer from "../abstract/ICustomer";

export default class Customer extends User implements ICustomer {
readonly pointOfOrder: number;
constructor(
firstName: string,
lastName: string,
email: string,
password: string,
pointOfOrder: number
) {
super(firstName, lastName, email, password);
this.pointOfOrder = pointOfOrder;
}

get discount(): number {
return this.pointOfOrder === 0 ? 0 : this.pointOfOrder / 100;
}
}
import Employee from "./user/concrete/Employee";
import Customer from "./user/concrete/Customer";
import ICustomer from "./user/abstract/ICustomer";
import IEmployee from "./user/abstract/IEmployee";

(() => {
const sami: IEmployee = new Employee(
"sami",
"ibrahimbaş",
"info@ssibrahimbas.com",
"1234",
10
);
const zuck: ICustomer = new Customer(
"mark",
"zuckerberg",
"info@meta.com",
"1234",
345
);

console.log("salary -> ", sami.salary);
console.log("discount -> ", zuck.discount);
})();

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sami Salih İbrahimbaş

Sami Salih İbrahimbaş

Hello, I am Sami Salih İbrahimbaş. I am a Mid Level Backend Stack Developer living and working in Turkey.