-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
142vip.cn
committed
Nov 28, 2023
1 parent
2cf3a53
commit e6a7995
Showing
201 changed files
with
13,699 additions
and
626 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* MVC模式 | ||
* - ts版本 | ||
*/ | ||
namespace MVCPattern { | ||
|
||
// 模型(Model) | ||
interface Todo { | ||
id: number; | ||
title: string; | ||
completed: boolean; | ||
} | ||
|
||
class TodoModel { | ||
private todos: Todo[] = [] | ||
|
||
addTodo(todo: Todo) { | ||
this.todos.push(todo) | ||
} | ||
|
||
getTodos() { | ||
return this.todos | ||
} | ||
|
||
updateTodoStatus(id: number, completed: boolean) { | ||
const todo = this.todos.find((todo) => todo.id === id) | ||
if (todo) { | ||
todo.completed = completed | ||
} | ||
} | ||
} | ||
|
||
// 视图(View) | ||
class TodoView { | ||
render(todos: Todo[]) { | ||
console.log('Todo List:') | ||
todos.forEach((todo) => { | ||
console.log(`[${todo.completed ? 'x' : ' '}] ${todo.title}`) | ||
}) | ||
} | ||
} | ||
|
||
// 控制器(Controller) | ||
class TodoController { | ||
private model: TodoModel | ||
private view: TodoView | ||
|
||
constructor(model: TodoModel, view: TodoView) { | ||
this.model = model | ||
this.view = view | ||
} | ||
|
||
addTodoToModel(todo: Todo) { | ||
this.model.addTodo(todo) | ||
} | ||
|
||
updateTodoStatusInModel(id: number, completed: boolean) { | ||
this.model.updateTodoStatus(id, completed) | ||
} | ||
|
||
updateView() { | ||
const todos = this.model.getTodos() | ||
this.view.render(todos) | ||
} | ||
} | ||
|
||
// 使用示例 | ||
const model = new TodoModel() | ||
const view = new TodoView() | ||
const controller = new TodoController(model, view) | ||
|
||
// 添加新任务 | ||
const todo1: Todo = { id: 1, title: 'Learn TypeScript', completed: false } | ||
const todo2: Todo = { id: 2, title: 'Write Code', completed: true } | ||
|
||
controller.addTodoToModel(todo1) | ||
controller.addTodoToModel(todo2) | ||
|
||
// 更新任务状态 | ||
controller.updateTodoStatusInModel(1, true) | ||
|
||
// 更新视图 | ||
controller.updateView() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* 合成复用原则Demo | ||
*/ | ||
namespace CrpDemo { | ||
interface Logger { | ||
log(message: string): void; | ||
} | ||
|
||
class ConsoleLogger implements Logger { | ||
log(message: string): void { | ||
console.log(`[ConsoleLogger] ${message}`) | ||
} | ||
} | ||
|
||
class FileLogger implements Logger { | ||
log(message: string): void { | ||
console.log(`[FileLogger] ${message}`) | ||
// 将日志写入文件的具体实现 | ||
} | ||
} | ||
|
||
class User { | ||
private logger: Logger | ||
|
||
constructor(logger: Logger) { | ||
this.logger = logger | ||
} | ||
|
||
save(): void { | ||
// 执行保存用户的逻辑 | ||
this.logger.log('User saved.') | ||
} | ||
} | ||
|
||
// 使用示例 | ||
const consoleLogger = new ConsoleLogger() | ||
const userWithConsoleLogger = new User(consoleLogger) | ||
userWithConsoleLogger.save() | ||
|
||
const fileLogger = new FileLogger() | ||
const userWithFileLogger = new User(fileLogger) | ||
userWithFileLogger.save() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* 依赖倒置原则Demo | ||
*/ | ||
namespace DipDemo { | ||
interface IMessageSender { | ||
sendMessage(message: string): void; | ||
} | ||
|
||
class EmailSender implements IMessageSender { | ||
sendMessage(message: string): void { | ||
console.log(`Sending email: ${message}`) | ||
} | ||
} | ||
|
||
class SMSMessageSender implements IMessageSender { | ||
sendMessage(message: string): void { | ||
console.log(`Sending SMS: ${message}`) | ||
} | ||
} | ||
|
||
class NotificationService { | ||
private messageSender: IMessageSender | ||
|
||
constructor(messageSender: IMessageSender) { | ||
this.messageSender = messageSender | ||
} | ||
|
||
sendNotification(message: string): void { | ||
this.messageSender.sendMessage(message) | ||
} | ||
} | ||
|
||
// 使用示例 | ||
const emailSender = new EmailSender() | ||
const smsSender = new SMSMessageSender() | ||
|
||
const emailNotification = new NotificationService(emailSender) | ||
const smsNotification = new NotificationService(smsSender) | ||
|
||
emailNotification.sendNotification('Hello, email notification.') | ||
smsNotification.sendNotification('Hello, SMS notification.') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* 接口隔离原则Demo | ||
*/ | ||
namespace IspDemo { | ||
interface Animal { | ||
eat(): void; | ||
} | ||
|
||
interface Flyable { | ||
fly(): void; | ||
} | ||
|
||
class Bird implements Animal, Flyable { | ||
public eat(): void { | ||
console.log('Bird is eating...') | ||
} | ||
|
||
public fly(): void { | ||
console.log('Bird is flying...') | ||
} | ||
} | ||
|
||
class Dog implements Animal { | ||
public eat(): void { | ||
console.log('Dog is eating...') | ||
} | ||
} | ||
|
||
// 使用示例 | ||
const bird = new Bird() | ||
bird.eat() | ||
bird.fly() | ||
|
||
const dog = new Dog() | ||
dog.eat() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* 迪米特法则Demo | ||
* 又叫最小知道原则 | ||
*/ | ||
namespace LodDemo { | ||
class Person { | ||
private readonly name: string | ||
|
||
constructor(name: string) { | ||
this.name = name | ||
} | ||
|
||
getName(): string { | ||
return this.name | ||
} | ||
} | ||
|
||
class Team { | ||
private readonly members: Person[] | ||
|
||
constructor() { | ||
this.members = [] | ||
} | ||
|
||
addMember(member: Person): void { | ||
this.members.push(member) | ||
} | ||
|
||
printTeamMembers(): void { | ||
for (const member of this.members) { | ||
console.log(member.getName()) | ||
} | ||
} | ||
} | ||
|
||
// 使用示例 | ||
const john = new Person('John') | ||
const jane = new Person('Jane') | ||
const team = new Team() | ||
|
||
team.addMember(john) | ||
team.addMember(jane) | ||
|
||
team.printTeamMembers() | ||
} |
Oops, something went wrong.