Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Factory Pattern | Creational Patterns
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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 2

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Awesome!

Completion rate improved to 7.14

bookFactory Pattern

Stryg for at vise menuen

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

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 2
some-alt