samedi 23 janvier 2016

Adapting design patterns (facade pattern) for functional programming languages

I have finished a functional programming course recently and I have decided to rewrite one of my previous libraries in a more functional manner. Although I have learned the main concepts of the paradigm I am a little confused about how they can be applied to solving real-life problems. I have decided to use a hybrid langauge, since it allows me to also learn the balance between the two paradigms.

The first question that came to my mind was how I could decouple the inner workings of the library to the outside code. For example in the OOP paradigm I would use a facade pattern - create an ABC and then use it in my library.

# abstract class
class AbstractClass(object):
    __metaclass__ = ABCMeta

    @abstractmethod
    def some_method(self):
        pass

# library
class Library(object):
    def __init__(self, concrete):
      self.concrete = concrete

    def do_something(self):
        self.concrete.some_method()

# usage of the library
class ConcreteClass(AbstractClass):
    def some_method():
        return None

concrete = ConcreteClass()
library = Library(concrete)
result = library.do_something()

I'm asking for a concrete example and a more general (langauge agnostic) explanation on how I could achieve this using immutable datatypes for the library's api input (concrete class in the example above).

Aucun commentaire:

Enregistrer un commentaire