Strategy Pattern replacing if/else or case statments - strategy-pattern

Why does it seem to me that using Strategy is just putting off the if/else to the Factory?
Using Strategy, doesn't a Factory need to figure out which concrete class to instantiate, and doesn't it do so by if/else?
Is another option to use a Map/List somehow, and have the keys be a name of the class to instantiate, and maybe have the class using the Factory pass in a name?

Map\List is an implemetation of Factory pattern.
Using Strategy patter is better than if\else because it's creates les coupled code.
With Factory+Startegy you can extend algorithms of processing without touch of client code, and have more ways to configure code dynamicaly (withot recompile).

Related

AutoFixture AutoDataAttribute Customization Beyond Derived Attribute

I am using the AutoDataAttribute class within AutoFixture.Xunit2 in a lot of projects. The recommended approach to add your own customizations seems to be a derived attribute like the following (note I am using FakeItEasy):
public class AutoFakeItEasyDataAttribute : AutoDataAttribute
{
public AutoFakeItEasyDataAttribute()
: base(() => new Fixture().Customize(new DomainCustomization()))
{
}
}
In an effort to reduce code copying/pasting, I wanted to abstract this derived attribute to a package we could consume in our projects. However, despite attempts utilizing dependency injection with this library and running into CLR issues with the DataAttribute not able to take anything beyond basic "primitives", I have ran into the proverbial "brick-wall". Obviously constructor injection doesn't seem to work here nor property injection to my knowledge (although unlikely that matters as the property isn't allocated until after the constructor call anyway).
The bottom line, I am looking for a way to include this derived attribute into a package but in a way where the domains can be customized for each individual project's needs?
I don't think what you're trying to achieve is possible due to how attributes work in C#. As you mentioned yourself you cannot pass into the attributes but a small set of primitive values, and in xUnit 2 data attributes don't have access to the test class instance, so you can not inject instances via reflection.
You could theoretically inject the IFixture instance into the test class using the library you mentioned (which I think is a horrible practice, that promotes sloppier tests), but then you'd have to give up the decorator notation of AutoFixture and use the declarative notation, to create your test data.

How to give up on switch case in favor of polymorphism React?

My React application implements two business cases. They are similar in many ways, but differs in, for example, executable methods during the process:
Case A: doHardWork()
Case B: doSimpleWork()
The results of this methods are processed equally and previous methods can be the same too.
Every time than I need to use one of these methods I am forced to use switch/case construction to define what method I will use exactly.
For solving this situation I want to use something like polymorphism, but don't see that it is possible, because methods doHardWork and doSimpleWork refer on MOBX store variables and another methods. So I can't incapsulate the logic of specific business case to the specific class.
Please help to avoid switch/case. The project become overweighted because of it and hard to expand.

Deprecation of TableRegistry::get()

I'd like to ask what are your thought on deprecation of the TableRegistry::get() static call in CakePHP 3.6?
In my opinion it was not a good idea.
First of all, using LocatorAwareTrait is wrong on many levels. Most important, using traits in such way can break the Single Responsibility and Separation of Concerns principles. In addition some developers don't want to use traits as all because they thing that it breaks the object oriented design pattern. They prefer delegation.
I prefer to use delegation as well with combination of flyweight/singleton approach. I know that the delegation is encapsulated by the LocatorAwareTrait but the only problem is that it exposes the (get/set)TableLocator methods that can be used incorrectly.
In other words if i have following facade:
class Fruits {
use \Cake\ORM\Locator\LocatorAwareTrait;
public function getApples() { ... }
public function getOranges() { ... }
...
}
$fruits = new Fruits();
I don't want to be able to call $fruits->getTableLocator()->get('table') outside of the scope of Fruits.
The other thing you need to consider when you make such changes is the adaptation of the framework. Doing TableRegistry::getTableLocator()->get('table') every time i need to access the model is not the best thing if i have multiple modules in my application that move beyond simple layered architecture.
Having flyweight/singleton class like TableRegistry with property get to access desired model just makes the development more straight forward and life easier.
Ideally, i would just like to call TR::get('table'), although that breaks the Cake's coding standards. (I've created that wrapper for myself anyways to make my app bullet proof from any similar changes)
What are your thoughts?

Kotlin Nested Object Classes

Ok so i'v been starting to learn kotlin for a week now, and i love the language:p
Besides the great utility of extension function, i feel like they lack a proper way of creating namespaces like java utility classes (xxxUtil).
I have recently starting to use this aproach, which im not sure is the right one, and i would like some feedback from Kotlin experienced users.
Is this a valid and proper thing todo:
object RealmDb {
private val realmInstance by lazy{ Realm.getInstance(MainApplication.instance) }
private fun wrapInTransaction(code:() -> Unit){
realmInstance.beginTransaction();
code.invoke()
realmInstance.commitTransaction();
}
object NormaNote{
fun create(...) {...}
fun update(...) {...}
}
}
So, whenever i want to update some NormalNote value to a Realm Database, i do the following:
RealmDb.NormaNote.create(title.text.toString(), note.text.toString())
Is this a common thing to do? Are there better approaches? As i understood, this is singleton nesting, i don't think there's any problem with this, i just don't like to put this common things like DB operations inside classes that need to be instantiated. In old java i opted to static classes
The officially recommended way to create namespaces in Kotlin is to put properties and functions that don't need to be inside classes at the top level of the file, and to use the package statements to create a namespace hierarchy. We see the practice of creating utility classes in Java as a workaround for a deficiency in the language, and not as a good practice to be followed in other languages.
In your example, I would put all of the code in top-level functions and properties.
I don't know about the rest of the code, but I do know that you don't have to call .invoke () on code. The invoke method can always be shortened to a direct call, which in this case would be code ().

helper functions as static functions or procedural functions?

i wonder if one should create a helper function in a class as a static function or just have it declared as a procedural function?
i tend to think that a static helper function is the right way to go cause then i can see what kind of helper function it is eg. Database::connect(), File::create().
what is best practice?
IMO it depends on what type of helper function it is. Statics / Singletons make things very difficult to test things in isolation, because they spread concrete dependencies around. So if the helper method is something I might want to fake out in a unit test (and your examples of creating files and connecting to databases definitely would fall in that category), then I would just create them as instance methods on a regular class. The user would instantiate the helper class as necessary to call the methods.
With that in place, it is easier to use Inversion of Control / Dependency Injection / Service Locator patterns to put fakes in when you want to test the code and you want to fake out database access, or filesystem access, etc.
This of course has the downside of there theoretically being multiple instances of the helper class, but this is not a real problem in most systems. The overhead of having these instances is minimal.
If the helper method was something very simple that I would never want to fake out for test, then I might consider using a static.
Singleton solves the confusion.
MyHelper.Instance.ExecuteMethod();
Instance will be a static property. Benefit is you get simple one line code in calling method and it reuses previously created instance which prevents overhead of instance creation on different memory locations and disposing them.

Resources