依赖倒置原则(Dependence Inversion Principle,DIP):
- 代码设计要依赖于抽象接口,不要依赖于具体实现。
- 通过面向抽象的编程方式来降低类之间的耦合,便于扩展。
反例示例:
代码语言:javascript复制class LuckyDrawController {
prizeRandom(users: User[], count: number): User[] {
...
}
prizeWeight(users: User[], count: number): User[] {
...
}
}
UML类图:
代码实现:
代码语言:javascript复制```typescript
class User {
name: string; // 用户名
level: number; // 会员等级
active: number; // 活跃值
constructor(name: string, level: number, active: number) {
this.name = name;
this.level = level;
this.active = active;
}
}
抽奖接口:传入所有参加抽奖的用户,返回得奖的用户
代码语言:javascript复制interface ILuckyDraw {
prize(users: User[], count: number): User[];
}
全随机抽奖类
代码语言:javascript复制class RandomLuckyDraw implements ILuckyDraw {
prize(users: User[], count: number): User[] {
// 洗牌
const shuffleUsers = _.shuffle(users);
// 随机抽取三位用户
return _.sampleSize(shuffleUsers, count);
}
}
按权重抽象
代码语言:javascript复制class WeightLuckyDraw implements ILuckyDraw {
prize(users: User[], count: number): User[] {
// 排序
const sortUsers = _.orderBy(users, ["level", "active"], ["desc", "desc"]);
// 抽取前三位
return _.take(sortUsers, count);
}
}
运行示例:
代码语言:javascript复制import { RandomLuckyDraw, WeightLuckyDraw, User } from "./DIP";
// 生成随机用户
let users = [];
for (let i = 0; i < 10; i ) {
const user = new User(
Mock.Random.cname(),
Mock.Random.integer(0, 10),
Mock.Random.integer(50, 100)
);
users.push(user);
}
// 随机抽奖
const randomLuckyDraw = new RandomLuckyDraw();
const winners1 = randomLuckyDraw.prize(users, 3);
console.log(winners1);
// 权重抽奖
const weightLuckyDraw = new WeightLuckyDraw();
const winners2 = weightLuckyDraw.prize(users, 3);
console.log(winners2);
【在线代码】:依赖倒置原则演示