design-patterns

What is the difference between Two-way Adapter and Pluggable Adapter Pattern in C#?

点点圈 提交于 2021-02-07 03:44:41
问题 Both Two-way Adapter and Pluggable Adapter can access both the classes and also change the behavior of the method which is required to be changed. The following is my code: Two-Way Adapter public interface IAircraft { bool Airborne { get; } void TakeOff(); int Height { get; } } // Target public sealed class Aircraft : IAircraft { int height; bool airborne; public Aircraft() { height = 0; airborne = false; } public void TakeOff() { Console.WriteLine("Aircraft engine takeoff"); airborne = true;

R: How to select files in directory which satisfy conditions both on the beginning and end of name?

不打扰是莪最后的温柔 提交于 2021-02-07 02:33:37
问题 I need to select files which start with "M" and end with " .csv". I can easily select files which start with "M" : list.files(pattern="^M"), or files which end with "csv": list.files(pattern = " .csv"). But how to select files which satisfy both conditions at the same time? 回答1: You could try glob2rx lf <- list.files("path_to_directory/", pattern=glob2rx("M*.csv")) which translates to: glob2rx("M*.csv") [1] "^M.*\\.csv$" 回答2: The pattern argument takes a regular expression: list.files(pattern

R: How to select files in directory which satisfy conditions both on the beginning and end of name?

心已入冬 提交于 2021-02-07 02:30:52
问题 I need to select files which start with "M" and end with " .csv". I can easily select files which start with "M" : list.files(pattern="^M"), or files which end with "csv": list.files(pattern = " .csv"). But how to select files which satisfy both conditions at the same time? 回答1: You could try glob2rx lf <- list.files("path_to_directory/", pattern=glob2rx("M*.csv")) which translates to: glob2rx("M*.csv") [1] "^M.*\\.csv$" 回答2: The pattern argument takes a regular expression: list.files(pattern

Decoration with several interface

泄露秘密 提交于 2021-02-06 11:54:35
问题 I am always struggling with decoration + interfaces. Say I have the following 'behavior' interfaces : interface IFlyable { void Fly();} interface ISwimmable { void Swim();} A main interface interface IMainComponent { void DoSomethingA(); void DoSomethingB();} A decorator on the main interace public class Decorator : IMainComponent { private readonly IMainComponent decorated; [..] public virtual void DoSomethingA() { decorated.DoSomethingA(); } public virtual void DoSomethingB() { decorated

Observer pattern or Callback?

限于喜欢 提交于 2021-02-05 20:20:14
问题 I have to do a design of a DownloadManager , but my main question is related to the notifications that a Download can send to the DownloadManager like onUpdate() to update a progress bar, onError() , onFinish() , etc. Somehow the DownloadManager has to receive this notifications from its Download s. I've thought 2 possible ways: Observer pattern Callbacks Observer pattern Basically there are 1 Observable and N Observers. In my case the DownloadManager has te be an Observer and the Downloads

Observer pattern or Callback?

对着背影说爱祢 提交于 2021-02-05 20:11:22
问题 I have to do a design of a DownloadManager , but my main question is related to the notifications that a Download can send to the DownloadManager like onUpdate() to update a progress bar, onError() , onFinish() , etc. Somehow the DownloadManager has to receive this notifications from its Download s. I've thought 2 possible ways: Observer pattern Callbacks Observer pattern Basically there are 1 Observable and N Observers. In my case the DownloadManager has te be an Observer and the Downloads

How to Structure a C# WinForms Model-View-Presenter (Passive View) Program?

荒凉一梦 提交于 2021-02-05 13:54:18
问题 I am designing a GUI that has the following basic idea (similarly modeled after Visual Studio's basic look-and-feel): File navigation Control selector (for selecting what to display in the Editor component) Editor Logger (errors, warnings, confirmation, etc.) For now, I will be using a TreeView for file navigation, a ListView for selecting controls to be displayed in the Editor and a RichTextBox for the Logger. The Editor will have 2 types of editing modes depending on what is selected in the

Static variable vs singleton

爷,独闯天下 提交于 2021-02-05 09:31:28
问题 I'm making Java REST application. I wonder how should I implement my services - should I use static service variables for whole application or make services as singletons like it was made in Spring MVC. Is there any difference between singleton object and initializing object only once during application? 回答1: should I use static service variables for whole application or make services as singletons It depends. You have to ask yourself two questions to come up with an answer: Where is that

How to create List of Action Delegates when different number and types of parameters are needed

别等时光非礼了梦想. 提交于 2021-02-05 06:41:45
问题 We have a set of about two dozen classes that inherit from a base class which has an abstract Validate method. Of course each class has different validation needs but the rules are needed in different combinations between all of them so, as you can imagine, this resulted in a lot of code duplication, as an example: Class A needs rules 1, 3, 6, and 9 Class B needs rules 3, 4, and 8 Class C needs rules 1, 8, and 9 .... you get the picture. So I was thinking of doing a simple refactoring and

Implementing the Template Method pattern in C#

我是研究僧i 提交于 2021-02-04 15:31:34
问题 The template method pattern provides that the abstract base class has a not overridable method: this method implements the common algorithm and should not overridden in the subclasses. In Java the template method is declared final within the abstract base class, in C# the sealed keyword has a similar meaning, but a not overridden method can not be declared sealed . public abstract class Base { protected abstract AlgorithmStep1(); protected abstract AlgorithmStep2(); public sealed void