Java – Spring to inject multiple service instances having same interface

Create service for multiple name providers

interface NameProvider {
    String name();
}

@Service
public class SomeName implement NameProvider {
    public String name() { return "SomeName"; }
}

@Service
public class AnotherName implement NameProvider {
    public String name() { return "AnotherName"; }
}

NameService class can inject multiple NameProviders with the following constructor injection.

@Service
public class NameService {
    private List<NameProvider> nameProviders;

    NameService(List<NameService> nameProviders) {
        this.nameProviders = nameProviders;
    }
}
Advertisements
Posted in Java, Programming | Leave a comment

Java – Importance of final fields for immutable object

Java will guarantee that all thread will see correct values of final fields that were set by the constructor, regardless of how object is published.

Posted in Java, Programming | Leave a comment

C++17 – Simple and clear view of C++17 features

https://github.com/tvaneerd/cpp17_in_TTs/blob/master/ALL_IN_ONE.md

Posted in C++, Programming | Leave a comment

VC++ – Visual C++ package manager for Windows

https://github.com/Microsoft/vcpkg

Posted in C++, Programming | Leave a comment

FP – Designing multi threaded and concurrent enabled software with functional programming

Modern CPU architecture has been enforcing me to create multi-threaded and concurrent enabled software.

I have been creating multi-thread software with Gang of four design principal but this design principal didn’t show me the best way on creating high scalable software without losing the maintainability and performance.

Gang of four design pattern encourage me to hide details and abstract operations but this created often harder problems on dealing with concurrent processing. In order to make a correct software, I had to use the locking API to avoid the race condition and this degraded the performance of application. And hiding details make harder to understand logic. Eventually it decreased the maintainability of software.

But after understanding the functional programming concepts such as Monoid, Functor, Applicative Functor and Monads, it showed me another approach to solve this problem with minimizing of losing maintainability and performance.

Functional programming don’t hide details of logic instead it encourage to write declarative code on describing logic. This approach improve the maintainability of software. And immutable data structure and Monoid allow to maximize the utilization of multiple cores without worrying about race condition and thread locking.

However, functional programming approach for concurrent application requires some basic infrastructure. The most visible one is to have a garbage collection environment. If it is mandatory to maintain the memory manually, there are some limitation on applying the functional programming approach but reference counting based memory management can help a lot for this case.

Posted in Functional Programming, Programming | Leave a comment

FP – Relationship between Applicative Functor and Monad

Applicative Functor has the following two properties.

Pure: create a functor from value.
Apply: a function taking arguments which are a Functor having a function and a Pure. This function will return the Functor after applying the function inside the first Functor with a value from a Pure by unwrapping.

Example)


Optional<int> apply(Optional<function<int(std::string)>> f, Optional<std::string> v) {
   if (f & v) {
      return Optional<int>(*f (*v));
   }

   return Optional(nullptr);
}

Java example)


Optional<Integer> apply(Optional<Function<String, Integer>> f, Optional<String> v) {
   return f.flatMap(a -> v.map(a(v))
}

Monad is the Applicative Functor with joining to flat the returning Functor.
So Monad is Applicative Functor with a join.
Join: flattening the Functor

Join Example)

Optional<int> join(Optional<Optional<int>> v) {
   if (v) {
      return *v;
   }

   return Optional<int>(nullptr);
}

Monad C++ definition:

template<typename T, typename U>
Monad<U> bind(Monad<T> m, std::function<Monad<U>(T)> f) {
   return join(apply(pure(f), m));
}
Posted in Functional Programming, Programming | Leave a comment

Java – Open addressing hash implementation in Java

IdentityHashMap class

Map<String,String> iMap = new IdentityHashMap<String,String>();
Posted in Java, Programming | Leave a comment