Found a total of 10000 related content
Simple Factory
Article Introduction:What is Simple Factory?
Simple factory is not design pattern. It simply decouples object creation from client code. In other words, Simple factory encapsulates object instantiation by moving instantiation logic to a separate class.
Simple fa
2024-11-24
comment 0
499
Understanding the Factory and Factory Method Design Patterns
Article Introduction:What is a Factory class? A factory class is a class that creates one or more objects of different classes.
The Factory pattern is arguably the most used design pattern in Software engineering. In this article, I will be providing an in-depth explana
2024-11-05
comment 0
512
How to Apply the Factory Pattern in PHP for Flexible Object Creation?
Article Introduction:This article explains PHP's Factory Pattern, a creational design pattern for object creation. It details how to create flexible object instantiation using factory classes and methods, highlighting benefits like loose coupling, improved code organiza
2025-03-10
comment 0
525
Describe the Factory pattern and give an example of its use in Go.
Article Introduction:The article discusses the Factory pattern in software design, focusing on its implementation in Go. It covers the pattern's benefits like encapsulation and flexibility, and how it enhances maintainability in Go applications.
2025-03-31
comment 0
843
Factory design pattern in Java example
Article Introduction:The factory pattern is to encapsulate object creation logic through a factory class, so that the caller does not need to care about the specific implementation class. 1. Define the unified behavior specification of interface Shape; 2. Create Circle and Rectangle implementation classes; 3. Write ShapeFactory factory class to return different instances according to parameters; 4. Use the factory class to obtain objects and call methods. This mode is suitable for scenarios where object creation is complex, the type is often changed, or the principle of opening and closing is required. It can effectively decouple the caller and specific classes and reduce maintenance costs.
2025-07-13
comment 0
699
Polymorphism Types vs Design pattern
Article Introduction:How to use polymorphism and design patterns in combination? The combination of polymorphism and design patterns can enhance the flexibility and maintainability of the code by: 1. The policy pattern uses polymorphism to define the algorithm family, making it interchangeable at runtime; 2. The template method pattern delays the implementation of algorithm steps in the subclass through polymorphism; 3. The visitor pattern uses polymorphism in the form of double-scheduling, allowing new operations to be added in the class hierarchy.
2025-06-21
comment 0
236
PHP Master | The Null Object Pattern - Polymorphism in Domain Models
Article Introduction:Core points
The empty object pattern is a design pattern that uses polymorphism to reduce conditional code, making the code more concise and easy to maintain. It provides a non-functional object that can replace the real object, thus eliminating the need for null value checks.
Empty object mode can be used in conjunction with other design modes, such as factory mode creating and returning empty objects, or policy mode changing the behavior of an object at runtime.
The potential disadvantage of the empty object pattern is that it may lead to the creation of unnecessary objects and increase memory usage. It may also make the code more complicated because additional classes and interfaces are required.
To implement the empty object pattern, you need to create an empty object class that implements the same interface as the real object. This empty object provides a default implementation for all methods in the interface, allowing it to replace the real object. This makes
2025-02-25
comment 0
632
How to implement factory model in Python?
Article Introduction:Implementing factory pattern in Python can create different types of objects by creating a unified interface. The specific steps are as follows: 1. Define a basic class and multiple inheritance classes, such as Vehicle, Car, Plane and Train. 2. Create a factory class VehicleFactory and use the create_vehicle method to return the corresponding object instance according to the type parameter. 3. Instantiate the object through the factory class, such as my_car=factory.create_vehicle("car","Tesla"). This pattern improves the scalability and maintainability of the code, but it needs to be paid attention to its complexity
2025-05-16
comment 0
1110
Python: Refactoring to Patterns
Article Introduction:Photo by Patric Ho
This concise guide maps Python code smells to their corresponding design pattern solutions.
class CodeSmellSolutions:
DUPLICATED_CODE = [
"form_template_method",
"introduce_polymorphic_creation_wi
2025-01-16
comment 0
1090
Design Pattern - JavaScript Challenges
Article Introduction:You can find all the code in this post at repo Github.
Design Pattern related challenges
Event Emitter
class EventEmitter {
constructor() {
this._events = Object.create(null);
this._key = 0;
}
on(eventNam
2024-11-03
comment 0
649
How to implement a factory method in Python?
Article Introduction:To implement the factory method pattern, first create a base class containing abstract factory methods, then define a subclass to override the method to return a specific product instance, and finally call the factory method to obtain the object through the base class reference. 1. Create the basic Creator class and declare factory_method; 2. Create ConcreteCreator1 and ConcreteCreator2 to return ConcreteProduct1 and ConcreteProduct2 respectively; 3. Use a unified interface to handle the creation of different types of objects, so that the code is neat and easy to expand.
2025-07-14
comment 0
377
What is the Factory pattern?
Article Introduction:Factory mode is used to encapsulate object creation logic, making the code more flexible, easy to maintain, and loosely coupled. The core answer is: by centrally managing object creation logic, hiding implementation details, and supporting the creation of multiple related objects. The specific description is as follows: the factory mode handes object creation to a special factory class or method for processing, avoiding the use of newClass() directly; it is suitable for scenarios where multiple types of related objects are created, creation logic may change, and implementation details need to be hidden; for example, in the payment processor, Stripe, PayPal and other instances are created through factories; its implementation includes the object returned by the factory class based on input parameters, and all objects realize a common interface; common variants include simple factories, factory methods and abstract factories, which are suitable for different complexities.
2025-06-24
comment 0
430
PHP Master | Logging with PSR-3 to Improve Reusability
Article Introduction:Core points
PSR-3, a common log object interface, allows developers to write reusable code without relying on any specific log implementation, thereby improving compatibility between different log libraries in PHP.
The PSR-3 interface provides eight methods to handle messages of different severity levels, and a common log() method that can receive any severity levels. Its design is to solve the problem of log implementation incompatibility.
Although PSR-3 has many benefits, some log libraries do not support it natively. However, developers can create PSR-3 compliant adapters by leveraging the adapter pattern and extending the AbstractLogger class provided in the Psr/Log library.
Many major PHP projects
2025-02-24
comment 0
1252
How Do I Choose the Right Design Pattern for My PHP Project?
Article Introduction:This article guides PHP developers in selecting appropriate design patterns. It emphasizes analyzing project requirements, identifying recurring issues (e.g., tight coupling, code duplication), and researching suitable patterns (Singleton, Factory,
2025-03-10
comment 0
590
C factory pattern example
Article Introduction:Factory mode is a creative design mode used to uniformly manage the creation of objects. It hides the instantiation details of specific classes through factory classes to improve scalability and maintenance; its core structure includes abstract product classes, specific product classes and factory classes; for example, in C, you can inherit the Shape base class and return the Circle or Rectangle instance according to parameters by ShapeFactory; use factory mode to decouple object creation and use, facilitate expansion and unified management creation logic; practical applications such as ParserFactory can create corresponding parsers based on file format; it is recommended to use smart pointers to handle error types during implementation, and consider using the automatic registration mechanism of mapping tables to optimize multi-type management.
2025-07-06
comment 0
431
Understanding the JavaScript Template Method Pattern
Article Introduction:The template method pattern is a behavioral design pattern that defines an algorithm framework in the base class and delays specific steps to the subclass implementation. It enhances code reuse and structural clarity by encapsulating fixed processes and allowing local customization. 1. The core idea is to define the algorithm skeleton and delay some steps to subclasses; 2. Define template methods and steps through the parent class in JavaScript, and subclasses inherit and rewrite specific methods to implement; 3. Applicable scenarios include multiple subclasses sharing logic but different details, keeping the algorithm structure stable and requiring flexible changes; 4. When using it, pay attention to avoid excessive dependence on inheritance, ensuring that the step naming is clear, and non-abstract steps can be directly implemented in the parent class, and beware of the runtime risks brought by JS lacking interface support.
2025-07-19
comment 0
995