SShortSingh.
Back to feed

State Pattern Explained: How Objects Can Change Behavior Based on Internal State

0
·1 views

The State Pattern, defined by the Gang of Four, is a behavioral design pattern that allows an object to alter its behavior when its internal state changes, making it appear to change its class. A common real-world example is an e-commerce order system, where operations like pay, ship, cancel, and refund depend on the order's current status. Without the pattern, managing multiple states and operations leads to complex cascading conditionals — seven states and six operations can produce up to 42 conditions to maintain. The pattern solves this by separating each state into its own class implementing a shared interface, so each state handles only its own valid transitions and throws exceptions for invalid ones. Adding a new state, such as an anti-fraud hold, requires only creating a new class without modifying existing states or the core Order class, naturally respecting the Open/Closed Principle.

Read the full story at DEV Community

This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)

Log in to join the discussion and vote.

Log in

Related stories

0
ProgrammingDEV Community ·

12 Practical Tips to Boost React App Performance Without Complexity

React applications can slow down as they grow due to unnecessary renders, large lists, excessive JavaScript, and too many API requests. Developers can improve performance by keeping state close to the components that need it, avoiding redundant state that can instead be derived during render, and virtualizing large lists using libraries like react-window. Lazy loading with React's built-in lazy and Suspense allows code to be split so users only download what they need upfront. Tools like useTransition, useDeferredValue, and input debouncing help defer non-urgent updates, while strategies such as response caching, request deduplication, and canceling stale requests reduce unnecessary network load.

0
ProgrammingDEV Community ·

How Node.js and C++ Native Addons Enable AI Agents to Control Desktop Apps

Web-based automation agents have long been limited to browser environments, unable to interact with native desktop applications or OS-level interfaces. A technical deep dive published on DEV Community outlines how developers can break this limitation by bridging Node.js's V8 JavaScript engine with C++ native addons. Using Node-API (node-addon-api), these addons compile into dynamic shared libraries that run within the same process memory as the Node.js app, eliminating costly inter-process communication overhead. This architecture allows autonomous agents to capture screenshots, calculate screen coordinates, and simulate mouse and keyboard input at the OS level. The approach targets enterprise workflows and general-purpose AI agents that require control beyond what browser-based tools like the Chrome DevTools Protocol can provide.

0
ProgrammingDEV Community ·

Supply chain attack hits keyv and cacheable npm packages, stealing cloud credentials

A supply chain attack compromised the npm packages keyv and cacheable, along with several related libraries, after the Jaredwray maintainer account was taken over by a threat actor. At least ten packages were published containing a malicious preinstall hook that downloads a Bun runtime and runs an obfuscated payload to steal cloud, CI, GitHub, and Kubernetes credentials. The stolen credentials were then used to republish additional trojanized npm packages, with evidence suggesting at least one account outside the keyv and cacheable ecosystems was also affected. The compromised packages collectively receive tens of millions of weekly downloads and are often installed indirectly — for example, through the dependency chain eslint → file-entry-cache → flat-cache → keyv. Security researchers flagged affected packages within minutes of publication, and an ongoing list of impacted artifacts is being maintained by Socket.dev.

0
ProgrammingDEV Community ·

Amazon Hits $3 Trillion as AWS Posts 37% Growth But Struggles to Meet Demand

Amazon has joined a rare group of companies by surpassing a $3 trillion market valuation. Its cloud division, AWS, recorded $42.2 billion in quarterly revenue, marking 37% year-over-year growth — its fastest rate in over four years. Despite Amazon planning capital expenditure of around $220 billion for 2026, CEO Andy Jassy acknowledged that AWS will still lack sufficient capacity to meet customer demand through 2025 and into 2026. The shortfall highlights a broader shift in the AI industry, where the key bottlenecks are no longer algorithms or talent but physical infrastructure such as chips, power, and data centers. Analysts and developers are increasingly focused on compute efficiency and workload optimization as cloud capacity becomes a scarce and strategically critical resource.

State Pattern Explained: How Objects Can Change Behavior Based on Internal State · ShortSingh