Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/admin.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Admin from './admin';
import User from './user';
import UserFactory from './user.factory';

export default class AdminType implements UserFactory {
create(pseudo: string): User {
return new Admin(pseudo);
}
}
15 changes: 5 additions & 10 deletions src/application.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Admin from './admin';
import UserFactoryProvider from './user.factory.provider';
import Article from './article';
import ArticleJsonFactory from './article.json.factory';
import ArticleRepository from './article.repository';
import Contributor from './contributor';
import Database from './database';
import { UserType } from './types';
import User from './user';
import UserFactory from './user.factory';

export default class Application {
run(): number {
Expand All @@ -16,14 +16,9 @@ export default class Application {
const userPseudo = 'pierre';
const userType: UserType = 'admin';

const user: User = (() => {
switch (userType as UserType) {
case 'admin':
return new Admin(userPseudo);
case 'contributor':
return new Contributor(userPseudo);
}
})();
const userFactory: UserFactory =
UserFactoryProvider.provideFromUserType(userType);
const user: User = userFactory.create(userPseudo);

const article = new Article(user, 'Mon premier article');

Expand Down
9 changes: 9 additions & 0 deletions src/contributor.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Contributor from './contributor';
import User from './user';
import UserFactory from './user.factory';

export default class ContributorType implements UserFactory {
create(pseudo: string): User {
return new Contributor(pseudo);
}
}
15 changes: 15 additions & 0 deletions src/user.factory.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import AdminType from './admin.type';
import ContributorType from './contributor.type';
import { UserType } from './types';
import UserFactory from './user.factory';

export default abstract class UserFactoryProvider {
static provideFromUserType(userType: UserType): UserFactory {
if (userType === 'admin') {
return new AdminType();
} else if (userType === 'contributor') {
return new ContributorType();
}
throw new Error(`"${userType} is not a valid user type"`);
}
}
5 changes: 5 additions & 0 deletions src/user.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import User from './user';

export default interface UserFactory {
create(pseudo: string): User;
}