Contextual Friendship Framework

23

Jun

Contextual Friendship Framework

Recently one of my colleague,  Rahul Rathore and I were on a conversation on object-oriented techniques and we both agree that it has lots of inspiration from the real world. Below is the background of our conversation and what emerged out of that.

Background:

The object-oriented languages like C#, Java are more close to real world. We can mimic the real world behaviors in these languages easily. Scenarios are well captured because of their object-oriented abilities. Inheritance, polymorphism, and encapsulation are principles derived from the real world. In the real world, there are more concepts which are applicable to humans but are not well mimicked in the computer world. One of them is Friendship (between objects).

For better code re-usage, Object-oriented programming languages like C++ have a feature called Friend classes. A class in C++ allows access to all the private & protected members to its friend classes.

But in the real world, we share only a few things with our friends based on the context we are in. We have complete control over what we want to share with our friends and families. This is not the case with C++ friend classes; it shares all the private & protected members to its friends. This poses a threat to the object-oriented theory of encapsulation.

Because of this threat, advanced programming languages like C# and Java have completely removed friendship between objects. But friendship can significantly increase code re-usage and cohesion in objects than breaking them. We wanted to have friendship in C# and Java but still, follow other object-oriented principles.

We observe that C# is very easy to use and highly extendable. So we embraced C# and extended it with the custom module which will enable friendship between classes.

Contextual Friendship Framework:

The framework extends the Microsoft.Net framework to enable friendship among classes. This Friendship framework allows developers to add attributes to classes and its members to enable friendship. There are 2 attributes available.

  1. FriendOf – for classes
  2. AvailableToFriends – for members
  3. FriendOf attribute can be applied to a class whose private and protected members need to be made available to specific friend classes. It takes an array of .Net Types as a parameter. AvailableToFriends attribute can be applied to a class member to allow its access to specific friends only.

A Friend class can access a private/protected member of its friend class by using the ‘MakeFriendlyCall’ extension method exposed by the friendship framework. MakeFriendlyCall method is an extension method that internally uses .Net Reflection to reach to private and protected members. MakeFriendlyCall will allow making calls to private/protected members with AvailableToFriends attribute.

Friendship enables selective sharing of members with friend class based on the class definition. The module provides facility to decorate members of a class for granting access to its friend. Let’s understand this using the below example

 
    [FriendOf(typeof(World))]
    public class Hello
    {
        public string Name { get; private set; }
 
        public void HelloPub()
        {
            Console.WriteLine("Public Hello");
        }
 
        [AvailableToFriends(typeof(World))]
        private string PrivateMethodsAvlToFriends(string name)
        {
            Name = name;
            Console.WriteLine("Private Hello : " + Name);
            return Name;
        }
 
        private void PrivateMethod(string d)
        {
            Console.WriteLine("Private method" + d);
        }
    }
 

    public class World
    {
        private void DoSomething()
        {
            var h = new Hello();
            h.MakeFriendlyCall("PrivateMethodAvlToFriends", "John");
        }
    } 

Here we have 2 classes, Hello and World. The Hello class has two private methods “PrivateMethodsAvlToFriends” and “PrivateMethod”. PrivateMethodsAvlToFriends is decorated with AvailableToFriends attribute. Now the Friend class “World” can make a call to this private method. This can be done by using the ‘MakeFriendlyCall’ extension method exposed generically.

Class Diagram Of the dependencies:

FriendshipFramework

Alternative solutions:

  • Friend class in C++
  • C# has Friend Assemblies which allow all internals of a class visible to another assembly using InternalsVisibleTo attribute. This is more generic than the C++ friend class and does not allow selective access.

Comparison of C++ Friend class Vs Friendship Framework:

Criteria C++ Friend Class Friendship Framework
Security All members are available to friends Granular control over what is available to Friends
Encapsulation Breaks Encapsulation Enhances Encapsulation
Re-Usability Part of the C++ library, so re-usable Fully re-usable as the framework is shipped as a package.
Design Pattern Access Modifier Decorator pattern is used. Also, can be implemented using Access Modifier

Future scope:

We have enabled friendship between classes without breaking encapsulation and security. However, this design can be extended to objects of classes, which makes it closer to the real world. After all, we are not sharing our car with a friend all the times.

Also, the Friendship attributes ‘FriendOf’ and ‘AvailableToFriends’ can be converted to new access modifiers like Public, private, protected. This could be done using Roslyn which is a complier extension to .Net. The Friend framework is available in .Net, but can be implemented to Java framework as well.

 

 

4 thoughts on - Contextual Friendship Framework

  • addys
    Reply Jun 23, 2014 at 12:09 pm

    This post totally missed interfaces. Interfaces is one way to specify ‘context’; since classes can implement multiple interfaces they can easy expose different subsets/views of their internals for different usages.

    This is somewhat more generic than the friend concept since it shifts the emphasis from concrete “friends” (implementation) to more abstract “context” (contract)

    • Shanky
      Reply Jun 23, 2014 at 7:31 pm

      We agree that, with Interfaces we can create contexts. But we feel there is more to contexts than, interfaces. As there could be private methods, which is useful for another class.

  • Pingback: Contextual Friendship Framework – Interesting Read | Ace Infoway

  • learning styles
    Reply Apr 17, 2017 at 11:38 pm

    I am regular reader of your blogs? This post posted at this website is really good.

Leave a Reply

Your email address will not be published. Required fields are marked *