Mixing OOP and FP

So far, we have seen that adding FP capabilities to an OOP language leads to benefits in the OOP design.

In summary, OOP fits perfectly with FP when our objects are as immutable as possible. To make our objects as immutable as possible, we can consider the following principles:

  • Objects should be types that encapsulate related pieces of data.
  • Objects can have methods; however, these methods shouldn't change the object and should instead return a new one of the appropriate type.
  • All the required state data should be injected into the class's initialization so that it will be ready to use immediately.
  • Static methods can be used freely and static variables should be avoided. Protocols and generics should be used to avoid code duplicates.

These principles not only empower us to employ functional design patterns, but also enrich our object-oriented code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset