Questions tagged [state]
The State pattern is used to represent the internal state of an object and to encapsulate varying behavior for the same object based on its state. This can be a cleaner way for an object to change its behavior at runtime without resorting to large monolithic conditional statements and thus improve maintainability.
71
questions
27
votes
3
answers
2k
views
A Failable<T> that allows safe returning of exceptions
This was inspired by a conversation in chat, that started with the discussion of C#7.0 tuples and out parameter declarations, which led to the idea that there is no 'good1' way to return an error ...
16
votes
0
answers
3k
views
Realtime concurrent Haskell MIDI buffer
Problem Background
MIDI is a serial representation of control signals to a sound generator. Typically, a noteOn message initiates the attack phase of a sound at a given pitch. The note will ...
15
votes
1
answer
35k
views
State pattern + C++ template
This finds its origin in the following reflection. In their book from 1995, the so-called gang of four (GoF) described the state pattern. What they were actually telling us in their description, is ...
14
votes
2
answers
472
views
Handling game states for an online RPG game
I'm writing an ORPG. The code below is client-side.
I have a main thread looping in Game::Run:
...
11
votes
2
answers
8k
views
Developing a better state pattern in C
I have created a state diagram to show the different transitions and states. I could not find many examples of the state pattern in C, so I have taken an example from a Java state pattern and tried to ...
8
votes
3
answers
474
views
Handling shared state among a lot of elements in Angular
I am working on a project in Angular where I have a number of similar data objects. When you click on anyone of them it's state and amount of data shown will change. All of the objects start in the ...
8
votes
1
answer
613
views
HangPerson in Haskell
I created a HangPerson game in Haskell and I must admit it feels very imperative. How can I make it more "functional?" Are there more elegant ways of breaking up the task? The ...
8
votes
1
answer
577
views
Haskell Tic-Tac-Toe (with automation and GUI)
There are already many Tic Tac Toe posts. But as far as I can tell, none of the ones in Haskell are complete with a GUI
Here is my implementation with Gloss. Gist Link for convenience
...
8
votes
1
answer
817
views
Elegant output separators
Today I found myself reinventing output separators for the zillionth time.
I have converged on an approach like the following that seem pretty clean:
Live On Coliru
...
7
votes
4
answers
374
views
Memory usage in Reversi board state
I'm coding a Reversi game, with an artificial intelligence using the MinMax as the search algorithm. My concern is that (most) search algorithms needs to store a lot of instances of "states", in my ...
7
votes
2
answers
541
views
Evaluating Parser State
The "parser state" of a module in rubberduck can be one of several values:
...
7
votes
2
answers
17k
views
Console application menu
Sometimes I need to write a simple Java console applications and menu look:
...
7
votes
1
answer
389
views
A beginner's brainfuck interpreter in Haskell
I am rather new to haskell, and could use some feedback on my code and the decisions I had to make. In my previous project, I made a JSON parser, but relied heavily on guidance from a university ...
6
votes
4
answers
2k
views
Section 2: Classes - Design a StackOverflow Post
Design a class called Post. This class models a StackOverflow post. It should have properties
for title, description and the date/time it was created. We should be able to up-vote or down-vote
a ...
6
votes
1
answer
2k
views
Date range generator
An app has a feature that's much like any calendar application (like the Outlook calendar for example).
Consequently, I need to do a lot of date logic. I wrote a class with date calculation methods ...
6
votes
1
answer
1k
views
A variant of state pattern + C++ template FSM without state creation/destruction penalty
This is a variant of the solution posted here, without state creation/destruction penalty.
Note that my comment in the original question about the simplification provided by C++11's inherited ...
6
votes
1
answer
311
views
Usage of state machine to model lifetime of an IP address
I need to model an IP address and thought using a state machine (the state_machine gem) would be a good idea.
An IP has the following characteristics:
It is assigned to a server.
It can be in state ...
6
votes
1
answer
737
views
State Monad in TypeScript
Here's my implementation of State Monad in TypeScript, based on a canonical Haskell implementation.
I would like it to get code reviewed.
...
6
votes
0
answers
58
views
React state management design pattern new approach thoughts
Hey people on the internet!
I do not know where to share my new npm package so I am asking you do you know of a good place to share my package?
Also I would really appreciate if you take a look on it ...
5
votes
2
answers
91
views
Static variable representing program state accessed with multiple functions
I have this method that performs certain functions based on the state that the algorithm is currently at:
...
5
votes
1
answer
529
views
Vending Machine implementing State Pattern
I was asked to implement a Vending Machine in a recent interview coding challenge. My attempt at a solution is based on the state pattern.
I would like to get some code reviews on this bearing in mind ...
5
votes
1
answer
371
views
Untouchable Numbers
I wrote this program in response to a CodeGolf challenge that required generating this sequence of "untouchable" numbers. This sequence can be found on OEIS as A005114.
My initial implementation was ...
5
votes
1
answer
201
views
Finite State Automaton implementation
I've been studying compiler construction theory. Right now, I'm studying Finite State Automaton and I've tried to create my own implementation. I'm not sure if my implementation is right.
...
5
votes
1
answer
953
views
Monkey testing a SmartCard library
My definition of monkey testing is basically playing with a program in as if I was a monkey (press every button, unplug things, go in the wrong order..etc etc)
So I made a rather simple SmartCard ...
5
votes
1
answer
87
views
Logon state management
I'm in the process of writing a simple text based MUD. First up is making sure that it's possible to create new players / logon with existing players. Since everything is text based, there are ...
5
votes
1
answer
2k
views
Model of an mp3 player with state and prototype design patterns
After reading about design patterns in general, I decided to try and incorporate them in an actual program. I chose a State machine for my first exercise, or, more concretely, an MP3 player. The ...
4
votes
4
answers
2k
views
State Pattern for traffic lights
I tried to implement a traffic light system using state pattern, please comment on the OOP/design pattern use.
...
4
votes
2
answers
218
views
Using Booleans to Manage State
I'm in the middle of writing my own little bullet shooter game much in the style of Touhou. Everything works fine so far but I'm disliking certain aspects of my code. Take this function for showing ...
4
votes
2
answers
401
views
Persisting Variables Over A State Loss
In an application that I built recently, I needed to persist several variables, without loosing their state, for duration that the application was in use. After some research, I found this post on ...
4
votes
1
answer
2k
views
(C++14) Handling state in C-style function pointer callbacks
I'm having some issues coming up with a sensible solution for using shared state in a C-style function pointer. I am using GLFW to handle my OpenGL context and have created a thin wrapper around all ...
4
votes
1
answer
210
views
Encapsulated state in clojure
While going through SICP and trying to implement the code in clojure, I've found that while I can get the code in chapter 3 to work, it seems to go against Clojure idioms, but I can't quite imagine ...
4
votes
1
answer
349
views
Huge terminal game (in development)
I'm developing a big (for me, as a beginner) terminal program "Brainfuck - The Game", in which you have to complete Brainfuck challenges. I would like your review on existing code (and project ...
4
votes
1
answer
171
views
Simple attribute parser for HTML
I am studying Java and trying to write an HTML parser, which should parse tag names and attributes. I wrote a class (code below) using the State pattern.
This is necessary for my training project, ...
3
votes
3
answers
138
views
Cooking steak with State Pattern and without IFs
I've been playing around a bit with a case of State Pattern but including as well ranges in order to get into a specific state.
So the definition is as simple as this:
There is a Steak that has a ...
3
votes
1
answer
1k
views
Simple Javascript state management class
So I needed very basic state management and notification for a small game-like thing I'm building. I decided to implement something like a finite state machine (but not quite, it doesn't transition ...
3
votes
1
answer
265
views
Is my design still a state design pattern, or some abomination?
I was trying to review state design pattern, and tried to create flexible implementation of it so it could reuse it in future.
So I came out with this:
...
3
votes
1
answer
565
views
Using C++11 move semantics to implement state pattern
I'm implementing a C++ layer on top of the sockets api of the OS (i.e. man 7 socket)1. Tcp sockets go through various states. Using the RAII principle leads to distinguishing the states: ...
3
votes
1
answer
354
views
Unit test for a React click-counting component
Take this basic component using state:
...
3
votes
1
answer
117
views
Haskell - Caching third party API token across requests
The following code is designed to cache a (UTCTime, Text) value that keeps the Text token around until it expires, preventing ...
3
votes
1
answer
113
views
ApplicationSettings Class for disabling/restoring Application State
This previous iteration of this question can be found here
A utility class to handle the state of the Application object.
Storing the application's initial state (...
3
votes
0
answers
399
views
ThreeJS State Object
state object - a state or scene from the game. When the start function is called, the state starts rendering, shutting down any other state using the renderer, and calling the specified functions (...
3
votes
0
answers
311
views
jQuery Plugin - HTML5 History State
I've built this small jQuery plugin to work with HTML5 History State functions. It's my first jQuery plugin, so I'm not sure if it's up to the best practices, or what could be done better. Keeping it ...
2
votes
2
answers
492
views
Scalable design of a system with multiple state machines, where valid state transitions depend on the state of other machines
This is a pretty huge question, however, I would appreciate it if you could just review the design and not my implementations of it. The Implementation and test sections could be ignored, they are ...
2
votes
2
answers
472
views
getDerivedStateFromProps [closed]
Starting from React 16.3 there are some new lifecycle methods introduced. One of them is the static getDerivedStateFromProps(props,state) method. This method ...
2
votes
2
answers
83
views
N rotations of an array, either left or right, and then printing the result
Disclaimer, I'm new to OOP, but have experience in haskell and javascript.
Essentially, the challenge was to create a program which would shift an array a given amount of times to the left and then ...
2
votes
2
answers
478
views
Counting occurrences of Char8s in a file
To learn some Data.Map and Control.Monad.State, I have written the following code, which should count the occurrences of ...
2
votes
1
answer
71
views
Factory for state classes with varying numbers of reference data members
Consider the following code:
...
2
votes
1
answer
135
views
Simpler state manament alternative to Redux, MobX, Context API, etc
I got frustrated with the jargon and created an alternative state management solution. Its up at https://github.com/smakazmi/react-soliit. It goes something like this
...
2
votes
1
answer
2k
views
Reading and Writing object from files
I have a server which has a permanent state which has to be saved on stable storage. It is composed by three fields:
List<LogEntry> log
...
2
votes
1
answer
201
views
Vocabulary trainer passing around too much IO/State
I've written a vocabulary trainer which does a lot of passing state around, as I have an ACID-database and a temporary state.
In my experience with Haskell I've learnt that overusing ...