Public Object with Internal Functions

Sometimes we would like to create an object with the objective to be able to be constructed by external projects. However, we would like to hide some of the internal operation to be only accessible by the current project. This may be because we want to have those operations hidden are internal, and we simply do not want to have other projects to be able to access to the internals.

In order to do this, we have to initialize few things.

First, let’s initialize the public interface. These are the methods / properties that will be accessible by other projects.

public interface IPerson {
    string FirstName { get; }
    string LastName { get; }
    void ChangeName(string firstName, string lastName);
}

Then, we initialize the internal interface that is only accessible by its own project. The following interface could be created on its own like so, or inherited from IPerson.

internal interface IBloodFlow {
    void ActivateBloodFlow();
}

Then we can instantiate the concrete class using the two interfaces inside project A.

Project A:
public class Person : IPerson, IBloodFlow {
    public FirstName { get; private set; }
    public LastName { get; private set; }

         public Person(string firstName, string lastName) { … }
    public ChangeName(string firstName, string lastName) { … }

    /// IBloodFlow function.
    void IBloodFlow.ActivateBloodFlow() { ... }
}

What we would be passing on to the external project is of type IPerson. The concrete class Person would also be accessible by external project, allowing us to do:

Project B:
public void ProjectBFunction {
    var newPerson = new Person(“Vampire”, “Change”);
    newPerson.ChangeName(“Other”, “Name”);
    newPerson.ActivateBloodFlow() /// Would not compile
    projectA.CreateAPerson(newPerson); /// passing it back to project A
}

Then inside Project A, we could do the following:

public void CreateAPerson(Person person) {
    person.ActivateBloodFlow();
}

A more useful way would be to have the Person class as an abstract and to have internal methods that need to be overridden. In this case, we would have the following:

public abstract class Person : IPerson, IBloodFlow {
    public FirstName { get; private set; }
    public LastName { get; private set; }
    
    public Person(string firstName, string lastName) { … }
    public ChangeName(string firstName, string lastName) { … }

    void IBloodFlow.ActivateBloodFlow() {
        ActivateBloodFlow(); 
    }
    protected abstract void ActivateBloodFlow();
}

When we have an abstract like above, we could inherit the ActivateBloodFlow operation as per needed like so:

public class Earthlings : Person {
    protected override void ActivateBloodFlow() { ... }
}
public class Saiyans : Person {
    protected override void ActivateBloodFLow() { … }
}

 

Using the above implementation techniques, it would allow us to create an object which acts as a container to be created from other projects with internal operation hidden.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s