Home Web Front-end JS Tutorial Mastering SOLID Principles in React Native and MERN Stack

Mastering SOLID Principles in React Native and MERN Stack

Sep 03, 2024 pm 03:23 PM

Mastering SOLID Principles in React Native and MERN Stack
The SOLID principles, introduced by Robert C. Martin (Uncle Bob), form the foundation of good software design. These principles guide developers in creating systems that are maintainable, scalable, and easy to understand. In this blog, we'll dive deep into each of the SOLID principles and explore how they can be applied in the context of React Native and the MERN stack (MongoDB, Express.js, React, Node.js).


1. Single Responsibility Principle (SRP)

Definition: A class should have only one reason to change, meaning it should only have one job or responsibility.

Explanation:
The Single Responsibility Principle (SRP) ensures that a class or module is focused on one aspect of the software's functionality. When a class has more than one responsibility, changes related to one responsibility could unintentionally impact the other, leading to bugs and a higher cost of maintenance.

React Native Example:
Consider a UserProfile component in a React Native application. Initially, this component might be responsible for both rendering the user interface and handling API requests to update user data. This violates the SRP because the component is doing two different things: managing UI and business logic.

Violation:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

const UserProfile = ({ userId }) => {

  const [userData, setUserData] = useState(null);

 

  useEffect(() => {

    fetch(`/api/users/${userId}`)

      .then(response => response.json())

      .then(data => setUserData(data));

  }, [userId]);

 

  return (

    <View>

      <Text>{userData?.name}</Text>

      <Text>{userData?.email}</Text>

    </View>

  );

};

Copy after login
Copy after login

In this example, the UserProfile component is responsible for both fetching data and rendering the UI. If you need to change how data is fetched (e.g., by using a different API endpoint or introducing caching), you'll have to modify the component, which could lead to unintended side effects in the UI.

Refactor:
To adhere to SRP, separate the data-fetching logic from the UI rendering logic.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

// Custom hook for fetching user data

const useUserData = (userId) => {

  const [userData, setUserData] = useState(null);

 

  useEffect(() => {

    const fetchUserData = async () => {

      const response = await fetch(`/api/users/${userId}`);

      const data = await response.json();

      setUserData(data);

    };

    fetchUserData();

  }, [userId]);

 

  return userData;

};

 

// UserProfile component focuses only on rendering the UI

const UserProfile = ({ userId }) => {

  const userData = useUserData(userId);

 

  return (

    <View>

      <Text>{userData?.name}</Text>

      <Text>{userData?.email}</Text>

    </View>

  );

};

Copy after login

In this refactor, the useUserData hook handles the data fetching, allowing the UserProfile component to focus solely on rendering the UI. Now, if you need to modify the data-fetching logic, you can do so without affecting the UI code.

Node.js Example:
Imagine a UserController that handles both database queries and business logic in a Node.js application. This violates SRP because the controller has multiple responsibilities.

Violation:

1

2

3

4

5

6

7

8

9

10

11

class UserController {

  async getUserProfile(req, res) {

    const user = await db.query('SELECT * FROM users WHERE id = ?', [req.params.id]);

    res.json(user);

  }

 

  async updateUserProfile(req, res) {

    const result = await db.query('UPDATE users SET name = ? WHERE id = ?', [req.body.name, req.params.id]);

    res.json(result);

  }

}

Copy after login

Here, the UserController is responsible for both interacting with the database and handling HTTP requests. Any change in the database interaction logic would require modifications in the controller, increasing the risk of bugs.

Refactor:
Separate the database interaction logic into a repository class, allowing the controller to focus on handling HTTP requests.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

// UserRepository class handles data access logic

class UserRepository {

  async getUserById(id) {

    return db.query('SELECT * FROM users WHERE id = ?', [id]);

  }

 

  async updateUser(id, name) {

    return db.query('UPDATE users SET name = ? WHERE id = ?', [name, id]);

  }

}

 

// UserController focuses on business logic and HTTP request handling

class UserController {

  constructor(userRepository) {

    this.userRepository = userRepository;

  }

 

  async getUserProfile(req, res) {

    const user = await this.userRepository.getUserById(req.params.id);

    res.json(user);

  }

 

  async updateUserProfile(req, res) {

    const result = await this.userRepository.updateUser(req.params.id, req.body.name);

    res.json(result);

  }

}

Copy after login

Now, the UserController is focused on handling HTTP requests and business logic, while the UserRepository is responsible for database interactions. This separation makes the code easier to maintain and extend.


2. Open/Closed Principle (OCP)

Definition: Software entities should be open for extension but closed for modification.

Explanation:
The Open/Closed Principle (OCP) emphasizes that classes, modules, and functions should be easily extendable without modifying their existing code. This promotes the use of abstractions and interfaces, allowing developers to introduce new functionality with minimal risk of introducing bugs in existing code.

React Native Example:
Imagine a button component that initially has a fixed style. As the application grows, you need to extend the button with different styles for various use cases. If you keep modifying the existing component each time you need a new style, you'll eventually violate OCP.

Violation:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

const Button = ({ onPress, type, children }) => {

  let style = {};

 

  if (type === 'primary') {

    style = { backgroundColor: 'blue', color: 'white' };

  } else if (type === 'secondary') {

    style = { backgroundColor: 'gray', color: 'black' };

  }

 

  return (

    <TouchableOpacity onPress={onPress} style={style}>

      <Text>{children}</Text>

    </TouchableOpacity>

  );

};

Copy after login

In this example, the Button component has to be modified each time a new button type is added. This is not scalable and increases the risk of bugs.

Refactor:
Refactor the Button component to be open for extension by allowing styles to be passed in as props.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

const Button = ({ onPress, style, children }) => {

  const defaultStyle = {

    padding: 10,

    borderRadius: 5,

  };

 

  return (

    <TouchableOpacity onPress={onPress} style={[defaultStyle, style]}>

      <Text>{children}</Text>

    </TouchableOpacity>

  );

};

 

// Now, you can extend the button's style without modifying the component itself

<Button style={{ backgroundColor: 'blue', color: 'white' }} onPress={handlePress}>

  Primary Button

</Button>

 

<Button style={{ backgroundColor: 'gray', color: 'black' }} onPress={handlePress}>

  Secondary Button

</Button>

Copy after login

By refactoring, the Button component is closed for modification but open for extension, allowing new button styles to be added without altering the component’s internal logic.

Node.js Example:
Consider a payment processing system in a Node.js application that supports multiple payment methods. Initially, you might be tempted to handle each payment method within a single class.

Violation:

1

2

3

4

5

6

7

8

9

10

11

class PaymentProcessor {

  processPayment(amount, method) {

    if (method === 'paypal') {

      console.log(`Paid ${amount} using PayPal`);

    } else if (method === 'stripe') {

      console.log(`Paid ${amount} using Stripe`);

    } else if (method === 'creditcard') {

      console.log(`Paid ${amount} using Credit Card`);

    }

  }

}

Copy after login

In this example, adding a new payment method requires modifying the PaymentProcessor class, violating OCP.

Refactor:
Introduce a strategy pattern to encapsulate each payment method in its own class, making the PaymentProcessor open for extension but closed for modification.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

class PaymentProcessor {

  constructor(paymentMethod) {

    this.paymentMethod = paymentMethod;

  }

 

  processPayment(amount) {

    return this.paymentMethod.pay(amount);

  }

}

 

// Payment methods encapsulated in their own classes

class PayPalPayment {

  pay(amount) {

    console.log(`Paid ${amount} using PayPal`);

  }

}

 

class StripePayment {

  pay(amount) {

    console.log(`Paid ${amount} using Stripe`);

  }

}

 

class CreditCardPayment {

  pay(amount) {

    console.log(`Paid ${amount} using Credit Card`);

  }

}

 

// Usage

const paypalProcessor = new PaymentProcessor(new PayPalPayment());

paypalProcessor.processPayment(100);

 

const stripeProcessor = new PaymentProcessor(new StripePayment());

stripeProcessor.processPayment(200);

Copy after login

Now, to add a new payment method, you simply create a new class without modifying the existing PaymentProcessor. This adheres to OCP and makes the system more scalable and maintainable.


3. Liskov Substitution Principle (LSP)

Definition: Objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program.

Explanation:
The Liskov Substitution Principle (LSP) ensures that subclasses can stand in for their parent classes without causing errors or altering the

expected behavior. This principle helps maintain the integrity of a system's design and ensures that inheritance is used appropriately.

React Native Example:
Imagine a base Shape class with a method draw. You might have several subclasses like Circle and Square, each with its own implementation of the draw method. These subclasses should be able to replace Shape without causing issues.

Correct Implementation:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

class Shape {

  draw() {

    // Default drawing logic

  }

}

 

class Circle extends Shape {

  draw() {

    super.draw();

    // Circle-specific drawing logic

  }

}

 

class Square extends Shape {

  draw() {

    super.draw();

    // Square-specific drawing logic

  }

}

 

function renderShape(shape) {

  shape.draw();

}

 

// Both Circle and Square can replace Shape without issues

const circle = new Circle();

renderShape(circle);

 

const square = new Square();

renderShape(square);

Copy after login

In this example, both Circle and Square classes can replace the Shape class without causing any problems, adhering to LSP.

Node.js Example:
Consider a base Bird class with a method fly. You might have a subclass Sparrow that extends Bird and provides its own implementation of fly. However, if you introduce a subclass like Penguin that cannot fly, it violates LSP.

Violation:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

class Bird {

  fly() {

    console.log('Flying');

  }

}

 

class Sparrow extends Bird {

  fly() {

    super.fly();

    console.log('Sparrow flying');

  }

}

 

class Penguin extends Bird {

  fly() {

    throw new Error("Penguins can't fly");

  }

}

Copy after login

In this example, substituting a Penguin for a Bird will cause errors, violating LSP.

Refactor:
Instead of extending Bird, you can create a different hierarchy or use composition to avoid violating LSP.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

class Bird {

  layEggs() {

    console.log('Laying eggs');

  }

}

 

class FlyingBird extends Bird {

  fly() {

    console.log('Flying');

  }

}

 

class Penguin extends Bird {

  swim() {

    console.log('Swimming');

  }

}

 

// Now, Penguin does not extend Bird in a way that violates LSP

Copy after login

In this refactor, Penguin no longer extends Bird in a way that requires it to support flying, adhering to LSP.


4. Interface Segregation Principle (ISP)

Definition: A client should not be forced to implement interfaces it doesn't use.

Explanation:
The Interface Segregation Principle (ISP) suggests that instead of having large, monolithic interfaces, it's better to have smaller, more specific interfaces. This way, classes implementing the interfaces are only required to implement the methods they actually use, making the system more flexible and easier to maintain.

React Native Example:
Suppose you have a UserActions interface that includes methods for both regular users and admins. This forces regular users to implement admin-specific methods, which they don't need, violating ISP.

Violation:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

interface UserActions {

  viewProfile(): void;

  deleteUser(): void;

}

 

class RegularUser implements UserActions {

  viewProfile() {

    console.log('Viewing profile');

  }

 

  deleteUser() {

    throw new Error("Regular users can't delete users");

  }

}

Copy after login

In this example, the RegularUser class is forced to implement a method (deleteUser) it doesn't need, violating ISP.

Refactor:
Split the UserActions interface into more specific interfaces for regular users and admins.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

interface RegularUserActions {

  viewProfile(): void;

}

 

interface AdminUserActions extends RegularUserActions {

  deleteUser(): void;

}

 

class RegularUser implements RegularUserActions {

  viewProfile() {

    console.log('Viewing profile');

  }

}

 

class AdminUser implements AdminUserActions {

  viewProfile() {

    console.log('Viewing profile');

  }

 

  deleteUser() {

    console.log('User deleted');

  }

}

Copy after login

Now, RegularUser only implements the methods it needs, adhering to ISP. Admin users implement the AdminUserActions interface, which extends RegularUserActions, ensuring that they have access to both sets of methods.

Node.js Example:
Consider a logger interface that forces implementing methods for various log levels, even if they are not required.

Violation:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

class Logger {

  logError(message) {

    console.error(message);

  }

 

  logInfo(message) {

    console.log(message);

  }

 

  logDebug(message) {

    console.debug(message);

  }

}

 

class ErrorLogger extends Logger {

  logError(message) {

    console.error(message);

  }

 

  logInfo(message) {

    // Not needed, but must be implemented

  }

 

  logDebug(message) {

    // Not needed, but must be implemented

  }

}

Copy after login

In this example, ErrorLogger is forced to implement methods (logInfo, logDebug) it doesn't need, violating ISP.

Refactor:
Create smaller, more specific interfaces to allow classes to implement only what they need.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

class ErrorLogger {

  logError(message) {

    console.error(message);

  }

}

 

class InfoLogger {

  logInfo(message) {

    console.log(message);

  }

}

 

class DebugLogger {

  logDebug(message) {

    console.debug(message);

  }

}

Copy after login

Now, classes can implement only the logging methods they need, adhering to ISP and making the system more modular.


5. Dependency Inversion Principle (DIP)

Definition: High-level modules should not depend on low-level modules. Both should depend on abstractions.

Explanation:
The Dependency Inversion Principle (DIP) emphasizes that high-level modules (business logic) should not be directly dependent on low-level modules (e.g., database access, external services). Instead, both should depend on abstractions, such as interfaces. This makes the system more flexible and easier to modify or extend.

React Native Example:
In a React Native application, you might have a UserProfile component that directly fetches data from an API service. This creates a tight coupling between the component and the specific API implementation, violating DIP.

Violation:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

const UserProfile = ({ userId }) => {

  const [userData, setUserData] = useState(null);

 

  useEffect(() => {

    fetch(`/api/users/${userId}`)

      .then(response => response.json())

      .then(data => setUserData(data));

  }, [userId]);

 

  return (

    <View>

      <Text>{userData?.name}</Text>

      <Text>{userData?.email}</Text>

    </View>

  );

};

Copy after login
Copy after login

In this example, the UserProfile component is tightly coupled with a specific API implementation. If the API changes, the component must be modified, violating DIP.

Refactor:
Introduce an abstraction layer (such as a service) that handles data fetching. The UserProfile component will depend on this abstraction, not the concrete implementation.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

// Define an abstraction (interface)

const useUserData = (userId, apiService) => {

  const [userData, setUserData] = useState(null);

 

  useEffect(() => {

    apiService.getUserById(userId).then(setUserData);

  }, [userId]);

 

  return userData;

};

 

// UserProfile depends on an abstraction, not a specific API implementation

const UserProfile = ({ userId, apiService }) => {

  const userData = useUserData(userId, apiService);

 

  return (

    <View>

      <Text>{userData?.name}</Text>

      <Text>{userData?.email}</Text>

    </View>

  );

};

Copy after login

Now, UserProfile can work with any service that conforms to the apiService interface, adhering to DIP and making the code more flexible.

Node.js Example:
In a Node.js application, you might have a service that directly uses a specific database implementation. This creates a tight coupling between the service and the database, violating DIP.

Violation:

1

2

3

4

5

class UserService {

  getUserById(id) {

    return db.query('SELECT * FROM users WHERE id = ?', [id]);

  }

}

Copy after login

In this example, UserService is tightly coupled with the specific database implementation (db.query). If you want to switch databases, you must modify UserService, violating DIP.

Refactor:
Introduce an abstraction (interface) for database access, and have UserService depend on this abstraction instead of the concrete implementation.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

// Define an abstraction (interface)

class UserRepository {

  constructor(database) {

    this.database = database;

  }

 

  getUserById(id) {

    return this.database.findById(id);

  }

}

 

// Now, UserService depends on an abstraction, not a specific database implementation

class UserService {

  constructor(userRepository) {

    this.userRepository = userRepository;

  }

 

  async getUserById(id) {

    return this.userRepository.getUserById(id);

  }

}

 

// You can easily switch database implementations without modifying UserService

const mongoDatabase = new MongoDatabase();

const userRepository = new UserRepository(mongoDatabase);

const userService = new UserService(userRepository);

Copy after login

By depending on an abstraction (UserRepository), UserService is no longer tied to a specific database implementation. This adheres to DIP, making the system more flexible and easier to maintain.


Conclusion

The SOLID principles are powerful guidelines that help developers create more maintainable, scalable, and robust software systems. By applying these principles in your React

Native and MERN stack projects, you can write cleaner code that's easier to understand, extend, and modify.

Understanding and implementing SOLID principles might require a bit of effort initially, but the long-term benefits—such as reduced technical debt, easier code maintenance, and more flexible systems—are well worth it. Start applying these principles in your projects today, and you'll soon see the difference they can make!

The above is the detailed content of Mastering SOLID Principles in React Native and MERN Stack. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1246
24
The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript Engines: Comparing Implementations JavaScript Engines: Comparing Implementations Apr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

JavaScript: Exploring the Versatility of a Web Language JavaScript: Exploring the Versatility of a Web Language Apr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration) How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration) Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration) Building a Multi-Tenant SaaS Application with Next.js (Backend Integration) Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

From C/C   to JavaScript: How It All Works From C/C to JavaScript: How It All Works Apr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript and the Web: Core Functionality and Use Cases JavaScript and the Web: Core Functionality and Use Cases Apr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

See all articles