design-patterns

What is a good design pattern to avoid having to use a global variable at the top your main form?

最后都变了- 提交于 2020-08-22 06:22:12
问题 Every time I talk to experienced programmers, they talk about having global variables being a bad practice because of debugging or security exploits. I have a simple List of strings I want to load from a a textfile and access across different methods in my form. Before, I would simply initialize said variable at the top, inside of my form class and use it across methods. I always try to reduce that practice when I can and only initialize those variables when I really need them. Is it a bad

single-element enum type singletone with lazy loading capability

。_饼干妹妹 提交于 2020-08-22 05:47:11
问题 I read many forums and posts about different style to implement single-tone pattern in java and seems "Enum are the best way to implement singletone pattern in java"!! I wonder how can i use Java Enum to implement SingleTone pattern in java with lazy-loading capability. since Enums are just classes. The first time a class is used, it gets loaded by the JVM and all of its static initialization is done. the enum members are static , so they're all going to be initialized. does anyone know how

Python: Try three times a function until all failed

断了今生、忘了曾经 提交于 2020-08-20 07:18:15
问题 I am writing in Python 2.7 and encounter the following situation. I would like to try calling a function three times. If all three times raise errors, I will raise the last error I get. If any one of the calls succeed, I will quit trying and continue immediately. Here is what I have right now: output = None error = None for _e in range(3): error = None try: print 'trial %d!' % (_e + 1) output = trial_function() except Exception as e: error = e if error is None: break if error is not None:

Polling thread in Blazor

做~自己de王妃 提交于 2020-08-09 07:04:07
问题 I am building a Blazor application with a REST API and Web interface. I will also have a monitoring part of the application that will poll data each second from a lot of different data sources. I have created a long running thread in a separate class that simply polls the data I want and it seems to be working fine. The application template I am using is a Blazor ASP.NET Server application. Simply like this: m_pollThread = new Thread(new ThreadStart(PollThread)) { IsBackground = true }; m

Mediator/EventAggregator differences

戏子无情 提交于 2020-08-03 03:48:10
问题 Also, when i need to communicate between loosely coupled objects (for example, MVVM 's ViewModel's), different books and blogs about best programming practices suggest use Mediator/EventAggregator patterns. My question is about differences/relations between those patterns. Can anyone describe them for me? 回答1: The two are mostly used for the same reason; reducing coupling between classes. I have even seen implementations of both, doing exactly the same thing, namely passing information as a

Is it possible to make an object expose the interface of an type parameter?

故事扮演 提交于 2020-07-29 06:06:30
问题 In C#, is it possible to write something like this: public class MyClass<T> : T where T : class, new() { } I know that the above implementation does not compile, but what I am actually trying to achive is implementing some kind of generic wrapper to an unknown type, so that an client can call the wrapper just as he would call the type, provided by the parameter T , instead of calling it using something like wrapper.Instance.SomeMember() . Thanks in advance! 回答1: This isn't possible. In my

What is the difference between a state machine and the implementation of the state pattern?

亡梦爱人 提交于 2020-07-28 06:02:05
问题 I wonder if a state machine is just the state pattern at work or if there is actually a difference between those two? I found this article with the bold title "the state design pattern vs state machine" but at the end of the day he only says that the state pattern makes state machines obsolete but then doesn't describe what exactly is a state machine compared to the implementation of the state pattern . 回答1: The way I describe this difference to my colleagues is that state patterns are a more

What is the difference between a state machine and the implementation of the state pattern?

久未见 提交于 2020-07-28 06:01:50
问题 I wonder if a state machine is just the state pattern at work or if there is actually a difference between those two? I found this article with the bold title "the state design pattern vs state machine" but at the end of the day he only says that the state pattern makes state machines obsolete but then doesn't describe what exactly is a state machine compared to the implementation of the state pattern . 回答1: The way I describe this difference to my colleagues is that state patterns are a more