Null State Pattern

Null State Pattern

Unpacking the Null State Pattern

Definition:

The Null State pattern is a behavioral design pattern that provides an object with an appropriate default state when its state is undefined or null. It helps avoid messy if-else checks for null states, offering a cleaner and more maintainable code structure.

Use Case: Navigating User States

In the realm of user authentication, the Null State pattern can be a game-changer. Consider scenarios where you need to manage both logged-in and anonymous users seamlessly. Let's dive into a practical example:

Scenario: A Seamless User Experience

Meet our PHP code snippet:

// make the interface
<?php

interface UserInterface
{
    public function getUsername();
    public function getEmail();
}

// make classes that implement it
class User implements UserInterface
{
    private string $username;
    private string $email;

    public function __construct($username, $email) {
        $this->username = $username;
        $this->email = $email;
    }

    public function getUsername(): string
    {
        return $this->username . "\n";
    }

    public function getEmail(): string
    {
        return $this->email;
    }

}
class NullUser implements UserInterface
{

    public function getUsername(): string
    {
        return "Guest \n";
    }

    public function getEmail(): string
    {
        return "guest@example.com \n";
    }
}

// Usage

function getCurrentUser(bool $isLoggedIn): UserInterface
{
    if ($isLoggedIn) {
        return new User("john", "doe@gmail");
    }

    return new NullUser();
}

$loggedInUser = getCurrentUser(true);
echo $loggedInUser->getUsername();  // Works for logged-in users

$anonymousUser = getCurrentUser(false);
echo $anonymousUser->getUsername();  // Works for anonymous users too
        

Easy, right? The Null State pattern ensures a smooth user experience without the hassle of tangled if-else checks.

Here, we have a function getCurrentUser that dynamically provides either a logged-in user or a null user based on the $isLoggedIn parameter. The Null State pattern simplifies the user retrieval process, ensuring a consistent user experience.

Implementation: Check Out the Code

Head over to the GitHub repository for the full implementation and explore other design patterns.

Dive Deeper: Watch the Tutorial

For a more in-depth understanding, check out the accompanying YouTube video tutorial: YouTube - Null State Pattern in PHP. It breaks down the concept, providing practical insights and real-world applications.

To view or add a comment, sign in

More articles by Adam Ijachi

  • State Pattern

    Are you tired of dealing with monolithic code that becomes a tangled mess as your project grows? Design patterns are…

  • Iterator Pattern

    The Iterator pattern is a design pattern that allows you to traverse a collection of objects without exposing its…

  • Composite Design Pattern

    A composite design pattern is a structural design pattern that allows you to compose objects into tree structures and…

  • Template Method Design

    Hi everyone! In this blog post, I'm going to talk about the template method pattern, a very useful design pattern in…

  • Proxy Design Pattern

    Hey there, welcome to my blog! Today I'm going to talk about the proxy pattern, what it is, when to use it, and how it…

  • Adapter Design Pattern

    Hey everyone, welcome to my blog! Today I want to talk about one of my favorite design patterns: the Adapter pattern…

  • Command Design Pattern

    The command pattern is a behavioral design pattern that allows you to encapsulate a request as an object and execute it…

  • Singleton Design Pattern

    The singleton pattern is a design pattern that ensures that only one instance of a class exists in the application. It…

  • Factory Design Pattern

    Hi there, welcome to my blog! Today I will talk about one of the most useful design patterns in software development:…

    2 Comments
  • Decorator Design Pattern

    Hi everyone, Today, I want to talk about the decorator design pattern, what it is, when to use it, and what are its…

Insights from the community

Others also viewed

Explore topics