Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Factory Pattern | Creational Patterns
Quizzes & Challenges
Quizzes
Challenges
/
JavaScript Design Patterns

bookFactory Pattern

The Factory pattern is a creational design pattern that provides a way to create objects without specifying the exact class or type of object that will be created. This pattern is particularly helpful when you want to delegate the responsibility of instantiating objects to a separate component, often called a "factory." You should consider using the Factory pattern when your code needs to create objects that share a common interface but may have different internal implementations, or when the process of creating an object is complex or involves conditional logic.

1234567891011121314151617181920212223242526272829303132333435363738394041
class AdminUser { constructor(name) { this.name = name; this.role = "admin"; } } class GuestUser { constructor(name) { this.name = name; this.role = "guest"; } } class RegularUser { constructor(name) { this.name = name; this.role = "regular"; } } class UserFactory { static createUser(type, name) { switch (type) { case "admin": return new AdminUser(name); case "guest": return new GuestUser(name); default: return new RegularUser(name); } } } const admin = UserFactory.createUser("admin", "Alice"); const guest = UserFactory.createUser("guest", "Bob"); const regular = UserFactory.createUser("regular", "Charlie"); console.log(admin.role); // "admin" console.log(guest.role); // "guest" console.log(regular.role); // "regular"
copy

Using the Factory pattern improves code flexibility by centralizing the logic for object creation. This means you can introduce new types of objects or modify the creation process without changing the code that depends on these objects. Instead of scattering instantiation logic throughout your codebase, the Factory pattern lets you encapsulate it in a single location. This approach also supports the open/closed principle, making your code easier to extend and maintain.

1. The Factory pattern is useful when:

2. Which of the following is a benefit of the Factory pattern?

question mark

The Factory pattern is useful when:

Select the correct answer

question mark

Which of the following is a benefit of the Factory pattern?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you explain how the Factory pattern supports the open/closed principle?

What are some real-world scenarios where the Factory pattern is especially useful?

Can you show how to add a new user type to this example?

Awesome!

Completion rate improved to 7.14

bookFactory Pattern

Scorri per mostrare il menu

The Factory pattern is a creational design pattern that provides a way to create objects without specifying the exact class or type of object that will be created. This pattern is particularly helpful when you want to delegate the responsibility of instantiating objects to a separate component, often called a "factory." You should consider using the Factory pattern when your code needs to create objects that share a common interface but may have different internal implementations, or when the process of creating an object is complex or involves conditional logic.

1234567891011121314151617181920212223242526272829303132333435363738394041
class AdminUser { constructor(name) { this.name = name; this.role = "admin"; } } class GuestUser { constructor(name) { this.name = name; this.role = "guest"; } } class RegularUser { constructor(name) { this.name = name; this.role = "regular"; } } class UserFactory { static createUser(type, name) { switch (type) { case "admin": return new AdminUser(name); case "guest": return new GuestUser(name); default: return new RegularUser(name); } } } const admin = UserFactory.createUser("admin", "Alice"); const guest = UserFactory.createUser("guest", "Bob"); const regular = UserFactory.createUser("regular", "Charlie"); console.log(admin.role); // "admin" console.log(guest.role); // "guest" console.log(regular.role); // "regular"
copy

Using the Factory pattern improves code flexibility by centralizing the logic for object creation. This means you can introduce new types of objects or modify the creation process without changing the code that depends on these objects. Instead of scattering instantiation logic throughout your codebase, the Factory pattern lets you encapsulate it in a single location. This approach also supports the open/closed principle, making your code easier to extend and maintain.

1. The Factory pattern is useful when:

2. Which of the following is a benefit of the Factory pattern?

question mark

The Factory pattern is useful when:

Select the correct answer

question mark

Which of the following is a benefit of the Factory pattern?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 2
some-alt