GoF,纯体系结构,完美代码-真正的程序员手册。但是在前端世界中,这些书中的许多想法都不可用。至少很难找到与现实世界相似的地方。也许现代的前端提前了?也许“功能性”编程和React已经证明了它们优于OOP的优势?在本文中,我想举一个我试图根据经典书籍中描述的原理和方法实现的待办事项清单应用程序的示例。框架依赖
该框架是现代战线的基石。在hh.ru上,职位空缺是React vs Angular vs Vue开发人员。我使用了每个框架,并且很长一段时间我都不明白为什么我必须使用Vue三年才能将按钮从红色重新涂成紫色?为什么我需要知道如何继承原型,或者事件循环如何工作,以便将同一按钮从左上角移到右边?答案很简单-我们编写与库绑定的应用程序。为什么具有长期经验的公司与React合作?可以。(我同意,这些都是特例。在您的公司中,您准备好聘请没有使用框架经验的专家吗?)使用语言而不是语言进行编程。(McConnell)
框架是一种工具,而不是一种生活方式。(马丁)
对于前沿世界来说,这些论点充其量只是空洞的短语,而最坏的情况就是要证明事实相反。让我们看一下React的官方文档,并查看一个简单的待办事项列表应用程序的示例。来自React官方网站的待办事项清单示例class TodoApp extends React.Component {
constructor(props) {
super(props);
this.state = { items: [], text: '' };
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
render() {
return (
<div>
<h3> </h3>
<TodoList items={this.state.items} />
<form onSubmit={this.handleSubmit}>
<label htmlFor="new-todo">
?
</label>
<input
id="new-todo"
onChange={this.handleChange}
value={this.state.text}
/>
<button>
#{this.state.items.length + 1}
</button>
</form>
</div>
);
}
handleChange(e) {
this.setState({ text: e.target.value });
}
handleSubmit(e) {
e.preventDefault();
if (!this.state.text.length) {
return;
}
const newItem = {
text: this.state.text,
id: Date.now()
};
this.setState(state => ({
items: state.items.concat(newItem),
text: ''
}));
}
}
class TodoList extends React.Component {
render() {
return (
<ul>
{this.props.items.map(item => (
<li key={item.id}>{item.text}</li>
))}
</ul>
);
}
}
“使用道具和状态,您可以创建一个小的任务清单应用程序。在此示例中,状态用于跟踪元素的当前列表...“对于新手程序员(也就是我几年前),此短语会自动生成输出:“这是待办事项列表应用程序的理想示例”。但是谁将状态存储在组件中?为此,有一个状态管理库。Redux文档中待办事项清单的示例是,这就是应用程序变得更加清晰和容易的方式(否)。我们可以尝试在正确的方向上绘制依赖关系吗?独立决定
让我们看待待办事项列表不作为前端的问题,也就是说,忘了我们需要绘制HTML(“网络是一个细节”)。我们将无法用肉眼检查结果,因此我们将不得不编写测试(如Bob叔叔说的,“然后可以应用TDD”)。任务是什么?什么是待办事项清单?我们正在尝试写。待办事项import { Todo } from './Todo';
describe('Todo', () => {
let todo: Todo;
beforeEach(() => {
todo = new Todo('description');
});
it('+getItems() should returns Todo[]', () => {
expect(todo.getTitle()).toBe('description');
});
it('+isCompleted() should returns completion flag', () => {
expect(todo.isCompleted()).toBe(false);
});
it('+toggleCompletion() should invert completion flag', () => {
todo.toggleCompletion();
expect(todo.isCompleted()).toBe(true);
});
});
TodoList.spec.tsimport { TodoList } from './TodoList';
describe('TodoList', () => {
let todoList: TodoList;
beforeEach(() => {
todoList = new TodoList();
});
it('+getItems() should returns Todo[]', () => {
expect(todoList.getItems()).toEqual([]);
});
it('+add() should create item and add to collection', () => {
todoList.add('Write tests');
expect(todoList.getItems()).toHaveLength(1);
});
it('+add() should create item with the description', () => {
const description = 'Write tests';
todoList.add(description);
const [item] = todoList.getItems();
expect(item.getTitle()).toBe(description);
});
it('+getCompletedItems() should not returns uncompleted Todo[]', () => {
const description = 'Write tests';
todoList.add(description);
expect(todoList.getCompletedItems()).toEqual([]);
});
it('+getCompletedItems() should returns completed Todo[]', () => {
const description = 'Write tests';
todoList.add(description);
const [item] = todoList.getItems();
item.toggleCompletion();
expect(todoList.getCompletedItems()).toEqual([item]);
});
it('+getUncompletedItems() should returns uncompleted Todo[]', () => {
const description = 'Write tests';
todoList.add(description);
const [item] = todoList.getItems();
expect(todoList.getUncompletedItems()).toEqual([item]);
});
it('+getUncompletedItems() should not returns completed Todo[]', () => {
const description = 'Write tests';
todoList.add(description);
const [item] = todoList.getItems();
item.toggleCompletion();
expect(todoList.getUncompletedItems()).toEqual([]);
});
});
export class Todo {
private completed: boolean = false;
constructor(private description: string) {}
getTitle(): string {
return this.description;
}
isCompleted(): boolean {
return this.completed;
}
toggleCompletion(): void {
this.completed = !this.completed;
}
}
import { Todo } from './Todo';
export class TodoList {
private items: Todo[] = [];
getItems(): Todo[] {
return this.items;
}
getCompletedItems(): Todo[] {
return this.items.filter((todo) => todo.isCompleted());
}
getUncompletedItems(): Todo[] {
return this.items.filter((todo) => !todo.isCompleted());
}
add(description: string): void {
this.items.push(new Todo(description));
}
}
我们得到两个带有信息接口的简单类。这就是全部?测试通过。现在拿起React。import React from 'react';
import { TodoList } from './core/TodoList';
export class App extends React.Component {
todoList: TodoList = this.createTodoList();
render(): any {
return (
<React.Fragment>
<header>
<h1>Todo List App</h1>
</header>
<main>
<TodoListCmp todoList={this.todoList}></TodoListCmp>
<AddTodoCmp todoList={this.todoList}></AddTodoCmp>
</main>
</React.Fragment>
);
}
private createTodoList(): TodoList {
const todoList = new TodoList();
todoList.add('Initial created Todo');
return todoList;
}
}
export const TodoListCmp: React.FC<{ todoList: TodoList }> = ({ todoList }) => {
return (
<div>
<h2>What to do?</h2>
<ul>
{todoList.getItems().map((todo) => (
<li key={todo.getTitle()}>{todo.getTitle()}</li>
))}
</ul>
</div>
);
};
export const AddTodoCmp: React.FC<{ todoList: TodoList }> = ({ todoList }) => {
return <button onClick={() => todoList.add(`Todo ${todoList.getItems().length}`)}>Add</button>;
};
并确保...添加项目无效。嗯...现在很清楚为什么所有内容都需要写成状态了 -这样React组件在了解了更改后便重新绘制了。但这是违反所有可能的原理并将逻辑放入视图组件的原因吗?有点耐心和勇气。要解决此问题,可以在无限循环或 Observer模式中调用forceUpdate()是完善的。我喜欢RxJs库,但是我不会连接它,而只是复制其任务所需的API。可观察的规格import { Observable, Subject } from './Observable';
describe('Observable', () => {
let subject: Subject<any>;
let observable: Observable<any>;
beforeEach(() => {
subject = new Subject();
observable = subject.asObservable();
});
it('should call callback on next value', async () => {
const spy = jasmine.createSpy();
observable.subscribe(spy);
subject.next({});
await delay();
expect(spy).toHaveBeenCalled();
});
it('should not call callback on next value if unsubscribed', async () => {
const spy = jasmine.createSpy();
const subscription = observable.subscribe(spy);
subscription.unsubscribe();
subject.next({});
await delay();
expect(spy).not.toHaveBeenCalled();
});
it('should send to callback subject.next value', async () => {
const spy = jasmine.createSpy();
observable.subscribe(spy);
const sendingValue = {};
subject.next(sendingValue);
await delay();
expect(spy.calls.first().args[0]).toBe(sendingValue);
});
});
function delay(timeoutInMs?: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, timeoutInMs));
}
可观察的export interface Observable<T = unknown> {
subscribe(onNext: (value: T) => void): Subscription;
}
export interface Subscription {
unsubscribe(): void;
}
export class Subject<T = unknown> implements Observable<T> {
protected callbackSet: Set<(value: T) => void> = new Set();
asObservable(): Observable<T> {
return this;
}
subscribe(onNext: (value: T) => void): Subscription {
this.callbackSet.add(onNext);
return { unsubscribe: () => this.callbackSet.delete(onNext) };
}
next(value: T): void {
Promise.resolve().then(() => this.callbackSet.forEach((onNext) => onNext(value)));
}
}
我认为,没有什么太复杂了。添加一个测试(更改通知-这是逻辑)。 it('+TodoList.prototype.add() should emit changes', async () => {
const spy = jasmine.createSpy();
todoList.changes.subscribe(spy);
todoList.add('description');
await delay();
expect(spy).toHaveBeenCalled();
});
让我们考虑一下,但是Todo元素的更改会影响TodoList的状态吗?影响-getCompletedItems / getUncompletedItems方法必须返回一组不同的元素。也许值得将toggleCompletion移至TodoList类?这是个坏主意-采用这种方法,我们必须为与新Todo元素有关的每个功能都添加TodoList(稍后再返回)。但是,如何再次观察者了解变化?为简化起见,让Todo元素本身通过回调传达更改。该程序的完整版本如下所示。import React from 'react';
import { Observable, Subject } from 'src/utils/Observable';
import { generateId } from 'src/utils/generateId';
export class Todo {
private completed: boolean = false;
id: string = generateId();
constructor(private description: string, private onCompletionToggle?: (todo: Todo) => void) {}
getTitle(): string {
return this.description;
}
isCompleted(): boolean {
return this.completed;
}
toggleCompletion(): void {
this.completed = !this.completed;
this.onCompletionToggle?.(this);
}
}
export class TodoList {
private items: Todo[] = [];
private changesSubject = new Subject();
readonly changes: Observable = this.changesSubject.asObservable();
getItems(): Todo[] {
return this.items;
}
getCompletedItems(): Todo[] {
return this.items.filter((todo) => todo.isCompleted());
}
getUncompletedItems(): Todo[] {
return this.items.filter((todo) => !todo.isCompleted());
}
add(description: string): void {
this.items.push(new Todo(description, () => this.changesSubject.next({})));
this.changesSubject.next({});
}
}
export class App extends React.Component {
todoList: TodoList = this.createTodoList();
render(): any {
return (
<React.Fragment>
<header>
<h1>Todo List App</h1>
</header>
<main>
<TodoListCmp todoList={this.todoList}></TodoListCmp>
<AddTodoCmp todoList={this.todoList}></AddTodoCmp>
</main>
</React.Fragment>
);
}
componentDidMount(): void {
this.todoList.changes.subscribe(() => this.forceUpdate());
}
private createTodoList(): TodoList {
const todoList = new TodoList();
todoList.add('Initial created Todo');
return todoList;
}
}
export const TodoListCmp: React.FC<{ todoList: TodoList }> = ({ todoList }) => {
return (
<div>
<h2>What to do?</h2>
<ul>
{todoList.getUncompletedItems().map((todo) => (
<TodoCmp key={todo.id} todo={todo}></TodoCmp>
))}
{todoList.getCompletedItems().map((todo) => (
<TodoCmp key={todo.id} todo={todo}></TodoCmp>
))}
</ul>
</div>
);
};
export const TodoCmp: React.FC<{ todo: Todo }> = ({ todo }) => (
<li
style={{ textDecoration: todo.isCompleted() ? 'line-through' : '' }}
onClick={() => todo.toggleCompletion()}
>
{todo.getTitle()}
</li>
);
export const AddTodoCmp: React.FC<{ todoList: TodoList }> = ({ todoList }) => {
return <button onClick={() => todoList.add(`Todo ${todoList.getItems().length}`)}>Add</button>;
};
这似乎是独立于框架的待办事项列表应用程序的外观。唯一的限制是PL。您可以为控制台实现显示,也可以使用Angular。也许该应用程序的当前版本不够复杂,无法确保一种独立的方法正在起作用并证明其优势。因此,我们将发挥我们的想象力,以模拟或多或少合理的待办事项清单开发方案。来自客户的编辑
大多数项目的主要噩梦是不断变化的需求。您知道无法避免进行编辑,并且这是正常现象。但是您如何为将来的变化做准备?特殊的待办事项元素
OOP的主要功能之一是通过引入新类型来解决问题的能力。也许这是最强大的OOP技术,可以单手提取复杂而繁琐的程序。例如,我不知道Todo元素需要什么。可能需要更改其名称,可能需要添加其他属性,也可以通过直接访问SpaceX服务器来更改此元素...但是我确定需求将会改变,并且我将需要不同类型的Todo。export class EditableTodo extends Todo {
changeTitle(title: string): void {
this.title = title;
this.onChange?.(this);
}
}
似乎要显示一种特殊类型,我们还需要更改视图组件。在实践中,我遇到(并撰写了)一些组件,其中有上百万种不同的条件将div块从长颈鹿变成了机关枪。为避免此问题,您可以创建带有巨大开关案例列表的临时组件。或应用Visitor模式和双重调度,然后让Todo元素自己决定要绘制哪种类型的组件。export class Todo {
id: string = '';
constructor(
protected title: string,
private completed: boolean = false,
protected onChange?: (todo: Todo) => void,
) {}
getTitle(): string {
return this.title;
}
isCompleted(): boolean {
return this.completed;
}
toggleCompletion(): void {
this.completed = !this.completed;
this.onChange?.(this);
}
render(renderer: TodoRenderer): any {
return renderer.renderSimpleTodo(this);
}
}
export class EditableTodo extends Todo {
changeTitle(title: string): void {
this.title = title;
this.onChange?.(this);
}
render(renderer: TodoRenderer): any {
return renderer.renderEditableTodo(this);
}
}
export class TodoRenderer {
renderSimpleTodo(todo: Todo): any {
return <SimpleTodoCmp todo={todo}></SimpleTodoCmp>;
}
renderFixedTodo(todo: Todo): any {
return <FixedTodoCmp todo={todo}></FixedTodoCmp>;
}
renderEditableTodo(todo: EditableTodo): any {
return <EditableTodoCmp todo={todo}></EditableTodoCmp>;
}
}
当一种类型的项目具有不同的视图时,双重调度选项特别有用。您可以通过将不同的TodoRenderers替换为render方法来更改它们。现在我们准备好了。对“特殊” Todo元素的新需求的担忧已经消失。我认为开发人员自己可以采取主动,并提供一些要求引入新类型的功能,现在可以通过编写新代码和对现有代码进行最小更改来添加这些功能。将数据保存到服务器
哪种应用程序不与服务器交互?当然,您需要能够通过HTTP保存我们的列表-另一个新要求。我们正在努力解决问题。AppTodoList.spec.tsimport { delay } from 'src/utils/delay';
import { TodoType } from '../core/TodoFactory';
import { AppTodoList } from './AppTodoList';
describe('AppTodoList', () => {
let todoList: AppTodoList;
beforeEach(() => {
todoList = new AppTodoList({
getItems: async () => [{ type: TodoType.Simple, title: 'Loaded todo', completed: false }],
save: () => delay(),
});
});
it('+resolve() should load saved todo items', async () => {
await todoList.resolve();
expect(todoList.getItems().length).toBeGreaterThan(0);
});
it('+resolve() should emit changes', async () => {
const spy = jasmine.createSpy();
todoList.changes.subscribe(spy);
await todoList.resolve();
await delay(1);
expect(spy).toHaveBeenCalled();
});
it('+add() should emit changes', async () => {
const spy = jasmine.createSpy();
todoList.changes.subscribe(spy);
todoList.add({ title: '' });
await delay(1);
expect(spy).toHaveBeenCalled();
});
it('+add() should emit changes after resolve()', async () => {
const spy = jasmine.createSpy();
todoList.changes.subscribe(spy);
await todoList.resolve();
todoList.add({ title: '' });
await delay(1);
expect(spy).toHaveBeenCalledTimes(2);
});
it('+todo.onChange() should emit changes', async () => {
await todoList.resolve();
await delay(1);
const spy = jasmine.createSpy();
todoList.changes.subscribe(spy);
const [todo] = todoList.getItems();
todo.toggleCompletion();
await delay(1);
expect(spy).toHaveBeenCalled();
});
it('+add() should call TodoListApi.save', async () => {
const spy = jasmine.createSpy();
todoList = new AppTodoList({ getItems: async () => [], save: async () => spy() });
todoList.add({ title: '' });
await delay();
expect(spy).toHaveBeenCalled();
});
it('+todo.onChange() should call TodoListApi.save', async () => {
const spy = jasmine.createSpy();
todoList = new AppTodoList({ getItems: async () => [{ title: '' }], save: async () => spy() });
await todoList.resolve();
const [todo] = todoList.getItems();
todo.toggleCompletion();
await delay();
expect(spy).toHaveBeenCalled();
});
it('+add() should save todoList state on success and rollback to it on error', async () => {
const api = { getItems: async () => [], save: () => Promise.resolve() };
todoList = new AppTodoList(api);
todoList.add({ title: '1' });
await delay(1);
const savedData = JSON.stringify(todoList.getItems());
api.save = () => Promise.reject('Mock saving failed');
todoList.add({ title: '2' });
expect(todoList.getItems()).toHaveLength(2);
await delay(1);
expect(JSON.stringify(todoList.getItems())).toBe(savedData);
});
});
export interface TodoListApi {
getItems(): Promise<TodoParams[]>;
save(todoParamsList: TodoParams[]): Promise<void>;
}
export class AppTodoList implements TodoList {
private todoFactory = new TodoFactory();
private changesSubject = new Subject();
changes: Observable = this.changesSubject.asObservable();
private state: TodoList = new TodoListImp();
private subscription: Subscription = this.state.changes.subscribe(() => this.onStateChanges());
private synchronizedTodoParamsList: TodoParams[] = [];
constructor(private api: TodoListApi) {}
async resolve(): Promise<void> {
const todoParamsList = await this.api.getItems();
this.updateState(todoParamsList);
}
private updateState(todoParamsList: TodoParams[]): void {
const todoList = new TodoListImp(todoParamsList);
this.state = todoList;
this.subscription.unsubscribe();
this.subscription = todoList.changes.subscribe(() => this.onStateChanges());
this.synchronizedTodoParamsList = todoParamsList;
this.changesSubject.next({});
}
private async onStateChanges(): Promise<void> {
this.changesSubject.next({});
try {
const params = this.state.getItems().map((todo) => this.todoFactory.serializeTodo(todo));
await this.api.save(params);
this.synchronizedTodoParamsList = params;
} catch {
this.updateState(this.synchronizedTodoParamsList);
}
}
destroy(): void {
this.subscription.unsubscribe();
}
getItems(): Todo[] {
return this.state.getItems();
}
getCompletedItems(): Todo[] {
return this.state.getCompletedItems();
}
getUncompletedItems(): Todo[] {
return this.state.getUncompletedItems();
}
add(todoParams: TodoParams): void {
this.state.add(todoParams);
}
}
我们不知道应用程序的行为。等待保存成功并显示更改?允许显示更改,如果出现错误,是否回滚到同步状态?还是完全忽略保存错误?客户很可能也不知道这一点。因此,对要求的更改是不可避免的,但它们仅会影响负责保护的一类。并在接下来的编辑中。变更记录
“我们需要取消/重做动作的能力” ...似乎一波新编辑使我们感到惊讶。但是在任何情况下,您都不能牺牲测试。现在完全不清楚哪种继承层次结构更适合,以及继承通常是否适合。因此,如果仅补充脏类(将考虑实现细节),那么就不会有不好的事情发生,而要牺牲唯一责任的原则。TodoListHistory.spec.tsimport { TodoParams } from 'src/core/TodoFactory';
import { delay } from 'src/utils/delay';
import { TodoListHistory } from './TodoListHistory';
describe('TodoListHistory', () => {
let history: TodoListHistory;
beforeEach(() => {
history = new TodoListHistory();
});
it('+getState() should returns TodoParams[]', () => {
expect(history.getState()).toEqual([]);
});
it('+setState() should rewrite current state', () => {
const newState = [{ title: '' }] as TodoParams[];
history.setState(newState);
expect(history.getState()).toBe(newState);
});
it('+hasPrev() should returns false on init', () => {
expect(history.hasPrev()).toBe(false);
});
it('+hasPrev() should returns true after setState()', () => {
history.setState([]);
expect(history.hasPrev()).toBe(true);
});
it('+switchToPrev() should switch on prev state', () => {
const prevState = [{ title: '' }] as TodoParams[];
history.setState(prevState);
history.setState([]);
history.switchToPrev();
expect(history.getState()).toBe(prevState);
});
it('+hasPrev() should returns false after switch to first', () => {
history.setState([]);
history.switchToPrev();
expect(history.hasPrev()).toBe(false);
});
it('+hasNext() should returns false on init', () => {
expect(history.hasNext()).toBe(false);
});
it('+hasNext() should returns true after switchToPrev()', () => {
history.setState([]);
history.switchToPrev();
expect(history.hasNext()).toBe(true);
});
it('+switchToNext() should switch on next state', () => {
const prevState = [{ title: '' }] as TodoParams[];
history.setState([]);
history.setState(prevState);
history.switchToPrev();
history.switchToNext();
expect(history.getState()).toBe(prevState);
});
it('+hasNext() should returns false after switchToNext()', () => {
history.setState([]);
history.switchToPrev();
history.switchToNext();
expect(history.hasNext()).toBe(false);
});
it('+hasNext() should returns false after setState()', () => {
history.setState([]);
history.switchToPrev();
history.setState([]);
expect(history.hasNext()).toBe(false);
});
it('+switchToPrev() should switch on prev state after setState()', () => {
const prevState = [{ title: '' }] as TodoParams[];
history.setState(prevState);
history.setState([]);
history.switchToPrev();
history.setState([]);
history.switchToPrev();
expect(history.getState()).toBe(prevState);
});
it('+setState() should not emit changes', async () => {
const spy = jasmine.createSpy();
history.changes.subscribe(spy);
history.setState([]);
await delay();
expect(spy).not.toHaveBeenCalled();
});
it('+switchToPrev() should emit changes', async () => {
history.setState([]);
await delay();
const spy = jasmine.createSpy();
history.changes.subscribe(spy);
history.switchToPrev();
await delay();
expect(spy).toHaveBeenCalled();
});
it('+switchToPrev() should emit changes', async () => {
history.setState([]);
history.switchToPrev();
await delay();
const spy = jasmine.createSpy();
history.changes.subscribe(spy);
history.switchToNext();
await delay();
expect(spy).toHaveBeenCalled();
});
it('+reset() should reset history and apply initial state', async () => {
history.setState([]);
expect(history.hasPrev()).toBe(true);
const initState = [{ title: '' }] as TodoParams[];
history.reset(initState);
expect(history.hasPrev()).toBe(false);
expect(history.getState()).toBe(initState);
});
});
AppTodoList.spec.tsimport { delay } from 'src/utils/delay';
import { TodoType } from '../core/TodoFactory';
import { AppTodoList } from './AppTodoList';
describe('AppTodoList', () => {
let todoList: AppTodoList;
beforeEach(() => {
todoList = new AppTodoList({
getItems: async () => [{ type: TodoType.Simple, title: 'Loaded todo', completed: false }],
save: () => delay(),
});
});
it('+resolve() should load saved todo items', async () => {
await todoList.resolve();
expect(todoList.getItems().length).toBeGreaterThan(0);
});
it('+resolve() should emit changes', async () => {
const spy = jasmine.createSpy();
todoList.changes.subscribe(spy);
await todoList.resolve();
await delay();
expect(spy).toHaveBeenCalled();
});
it('+add() should emit changes', async () => {
const spy = jasmine.createSpy();
todoList.changes.subscribe(spy);
todoList.add({ title: '' });
await delay();
expect(spy).toHaveBeenCalled();
});
it('+add() should emit changes after resolve()', async () => {
const spy = jasmine.createSpy();
todoList.changes.subscribe(spy);
await todoList.resolve();
todoList.add({ title: '' });
await delay();
expect(spy).toHaveBeenCalledTimes(2);
});
it('+todo.onChange() should emit changes', async () => {
await todoList.resolve();
await delay();
const spy = jasmine.createSpy();
todoList.changes.subscribe(spy);
const [todo] = todoList.getItems();
todo.toggleCompletion();
await delay();
expect(spy).toHaveBeenCalled();
});
it('+add() should call TodoListApi.save', async () => {
const spy = jasmine.createSpy();
todoList = new AppTodoList({ getItems: async () => [], save: async () => spy() });
todoList.add({ title: '' });
await delay();
expect(spy).toHaveBeenCalled();
});
it('+todo.onChange() should call TodoListApi.save', async () => {
const spy = jasmine.createSpy();
todoList = new AppTodoList({ getItems: async () => [{ title: '' }], save: async () => spy() });
await todoList.resolve();
const [todo] = todoList.getItems();
todo.toggleCompletion();
await delay();
expect(spy).toHaveBeenCalled();
});
it('+add() should ignore error on save', async () => {
const api = { getItems: async () => [], save: () => Promise.resolve() };
todoList = new AppTodoList(api);
todoList.add({ title: '1' });
await delay();
api.save = jasmine.createSpy().and.returnValue(Promise.reject('Mock saving failed'));
todoList.add({ title: '2' });
expect(todoList.getItems()).toHaveLength(2);
await delay();
expect(api.save).toHaveBeenCalled();
expect(todoList.getItems()).toHaveLength(2);
});
it('+resolve() should provide current todoList state to history', async () => {
expect(todoList.getItems()).toHaveLength(0);
expect(todoList.getHistory().getState()).toHaveLength(0);
await todoList.resolve();
await delay();
expect(todoList.getItems()).toHaveLength(1);
expect(todoList.getHistory().getState()).toHaveLength(1);
});
it('+add() should provide current todoList state to history', async () => {
expect(todoList.getItems()).toHaveLength(0);
expect(todoList.getHistory().getState()).toHaveLength(0);
todoList.add({ title: '' });
await delay();
expect(todoList.getItems()).toHaveLength(1);
expect(todoList.getHistory().getState()).toHaveLength(1);
});
it('+history.switchToPrev() should change todoList state on prev', async () => {
todoList.add({ title: '' });
await delay();
expect(todoList.getItems()).toHaveLength(1);
expect(todoList.getHistory().getState()).toHaveLength(1);
todoList.getHistory().switchToPrev();
await delay();
expect(todoList.getHistory().getState()).toHaveLength(0);
expect(todoList.getItems()).toHaveLength(0);
});
it('+history.switchToPrev() should change todoList state on prev after resolve()', async () => {
await todoList.resolve();
todoList.add({ title: '' });
await delay();
expect(todoList.getItems()).toHaveLength(2);
expect(todoList.getHistory().getState()).toHaveLength(2);
todoList.getHistory().switchToPrev();
await delay();
expect(todoList.getHistory().getState()).toHaveLength(1);
expect(todoList.getItems()).toHaveLength(1);
});
it('+add() should emit changes after history.switchToPrev()', async () => {
todoList.add({ title: '' });
todoList.getHistory().switchToPrev();
await delay();
const spy = jasmine.createSpy();
todoList.changes.subscribe(spy);
todoList.add({ title: '' });
await delay();
expect(spy).toHaveBeenCalled();
});
});
import { TodoParams } from 'src/core/TodoFactory';
import { Observable, Subject } from 'src/utils/Observable';
export class TodoListHistory {
private changesSubject = new Subject();
private history: TodoParams[][] = [this.state];
changes: Observable = this.changesSubject.asObservable();
constructor(private state: TodoParams[] = []) {}
reset(state: TodoParams[]): void {
this.state = state;
this.history = [this.state];
}
getState(): TodoParams[] {
return this.state;
}
setState(state: TodoParams[]): void {
this.deleteHistoryAfterCurrentState();
this.state = state;
this.history.push(state);
}
private nextState(state: TodoParams[]): void {
this.state = state;
this.changesSubject.next({});
}
private deleteHistoryAfterCurrentState(): void {
this.history = this.history.slice(0, this.getCurrentStateIndex() + 1);
}
hasPrev(): boolean {
return this.getCurrentStateIndex() > 0;
}
hasNext(): boolean {
return this.getCurrentStateIndex() < this.history.length - 1;
}
switchToPrev(): void {
const prevStateIndex = Math.max(this.getCurrentStateIndex() - 1, 0);
this.nextState(this.history[prevStateIndex]);
}
switchToNext(): void {
const nextStateIndex = Math.min(this.getCurrentStateIndex() + 1, this.history.length - 1);
this.nextState(this.history[nextStateIndex]);
}
private getCurrentStateIndex(): number {
return this.history.indexOf(this.state);
}
}
import { Observable, Subject, Subscription } from 'src/utils/Observable';
import { Todo } from '../core/Todo';
import { TodoFactory, TodoParams } from '../core/TodoFactory';
import { TodoList, TodoListImp } from '../core/TodoList';
import { TodoListApi } from './TodoListApi';
import { HistoryControl, TodoListHistory } from './TodoListHistory';
export class AppTodoList implements TodoList {
private readonly todoFactory = new TodoFactory();
private readonly history: TodoListHistory = new TodoListHistory();
private changesSubject = new Subject();
readonly changes: Observable = this.changesSubject.asObservable();
private state: TodoList = new TodoListImp();
private stateSubscription: Subscription = this.state.changes.subscribe(() =>
this.onStateChanges(),
);
private historySubscription = this.history.changes.subscribe(() => this.onHistoryChanges());
constructor(private api: TodoListApi) {}
private onStateChanges(): void {
const params = this.state.getItems().map((todo) => this.todoFactory.serializeTodo(todo));
this.history.setState(params);
this.api.save(params).catch(() => {});
this.changesSubject.next({});
}
private onHistoryChanges(): void {
const params = this.history.getState();
this.updateStateTodoList(params);
this.api.save(params).catch(() => {});
}
private updateStateTodoList(todoParamsList: TodoParams[]): void {
const todoList = new TodoListImp(todoParamsList);
this.state = todoList;
this.stateSubscription.unsubscribe();
this.stateSubscription = this.state.changes.subscribe(() => this.onStateChanges());
this.changesSubject.next({});
}
async resolve(): Promise<void> {
const todoParamsList = await this.api.getItems();
this.history.reset(todoParamsList);
this.updateStateTodoList(todoParamsList);
}
destroy(): void {
this.stateSubscription.unsubscribe();
this.historySubscription.unsubscribe();
}
getHistory(): HistoryControl<TodoParams[]> {
return this.history;
}
getItems(): Todo[] {
return this.state.getItems();
}
getCompletedItems(): Todo[] {
return this.state.getCompletedItems();
}
getUncompletedItems(): Todo[] {
return this.state.getUncompletedItems();
}
add(todoParams: TodoParams): void {
this.state.add(todoParams);
}
}
由于更改历史记录几乎相同,因此我们将待办事项列表历史记录的管理分为基本类。import { Todo } from 'src/core/Todo';
import { Observable, Subject, Subscription } from 'src/utils/Observable';
import { TodoFactory, TodoParams } from '../core/TodoFactory';
import { TodoList, TodoListImp } from '../core/TodoList';
import { HistoryControl, HistoryState } from './HistoryState';
export class HistoricalTodoList implements TodoList, HistoryControl {
protected readonly todoFactory = new TodoFactory();
protected readonly history = new HistoryState<TodoParams[]>([]);
private changesSubject: Subject = new Subject();
readonly changes: Observable = this.changesSubject.asObservable();
private state: TodoList = new TodoListImp();
private stateSubscription: Subscription = this.state.changes.subscribe(() =>
this.onStateChanged(this.getSerializedState()),
);
constructor() {}
protected onStateChanged(params: TodoParams[]): void {
this.history.addState(params);
this.changesSubject.next({});
}
protected onHistorySwitched(): void {
this.updateState(this.history.getState());
}
protected updateState(todoParamsList: TodoParams[]): void {
this.state = new TodoListImp(todoParamsList);
this.updateStateSubscription();
this.changesSubject.next({});
}
private updateStateSubscription(): void {
this.stateSubscription.unsubscribe();
this.stateSubscription = this.state.changes.subscribe(() =>
this.onStateChanged(this.getSerializedState()),
);
}
private getSerializedState(): TodoParams[] {
return this.state.getItems().map((todo) => this.todoFactory.serializeTodo(todo));
}
destroy(): void {
this.stateSubscription.unsubscribe();
}
getItems(): Todo[] {
return this.state.getItems();
}
getCompletedItems(): Todo[] {
return this.state.getCompletedItems();
}
getUncompletedItems(): Todo[] {
return this.state.getUncompletedItems();
}
add(todoParams: TodoParams): void {
this.state.add(todoParams);
}
canUndo(): boolean {
return this.history.hasPrev();
}
canRedo(): boolean {
return this.history.hasNext();
}
undo(): void {
this.history.switchToPrev();
this.onHistorySwitched();
}
redo(): void {
this.history.switchToNext();
this.onHistorySwitched();
}
}
import { TodoParams } from 'src/core/TodoFactory';
import { HistoricalTodoList } from './HistoricalTodoList';
import { TodoListApi } from './TodoListApi';
export class ResolvableTodoList extends HistoricalTodoList {
constructor(private api: TodoListApi) {
super();
}
async resolve(): Promise<void> {
const todoParamsList = await this.api.getItems();
this.history.reset(todoParamsList);
this.updateState(todoParamsList);
}
protected onStateChanged(params: TodoParams[]): void {
super.onStateChanged(params);
this.api.save(params).catch(() => this.undo());
}
protected onHistorySwitched(): void {
super.onHistorySwitched();
this.api.save(this.history.getState()).catch(() => {});
}
}
保护问题的解决方案本身就来了。现在,我们不必担心客户从长远来看会选择哪种保护策略。您可以为他提供所有3种选择,以扩展基类。摘要
从我们的待办事项清单看来,我们已经有了Google Keep的小型原型。链接到存储库以启动应用程序或查看提交的历史记录。此示例与大多数前端应用程序有何根本不同?我们不依赖库,因此,从未与React一起工作过的人可以理解此应用程序。我们的决策仅旨在获得结果,而不会分散框架的细节,因此代码或多或少反映了正在解决的问题。我们设法使添加新的Todo元素类型变得容易,并且我们准备更改保护策略。我们遇到了什么困难?我们解决了使用Observer模式更新视图而不参考框架的问题。事实证明,仍然需要使用此模式来解决主要问题(即使我们不需要绘制HTML)。因此,我们不会因为放弃框架中内置的变更检测系统的“服务”而产生成本。我想强调的是,编写测试并不困难。使用信息界面测试简单的独立对象是一种乐趣。代码的复杂度仅取决于任务本身和我的技能(或曲率)。负责此工作的开发人员水平如何?Junior React Developer可以编写这样的解决方案吗?“编程更像是一种技巧”,因此,如果没有使用OOP和模式的实践,我认为这将很困难。但是您和您的公司决定您投资什么。您是在实践OOP还是理解下一个框架的复杂性?我再一次变得确信经验丰富的程序员的文学作品具有相关性,并向人们展示了如何在前端充分使用经典著作的建议。谢谢阅读!