Question about Role Object Template

I look at an article by Martin Fowler titled " Role-Based Work . " In it, Fowler discloses three basic strategies for working with roles for a Person in an organization (for example, Employee, Engineer, Manager, Salesman), which include role subtyping, object role, and role relationships.

Being a “working draft”, it was written in 1997 and, of course, was old, it also has some errors that would otherwise not have been there. I am confused by going over to the example of the Role object through which it passes, and has included my C # interpretation of some of its java code below.

I have three questions:
(1) there is a lot of type identification with strings that seem to be replaced by generics, but I can't figure out how to do this. How to implement this code using generics?
(2) The JobRole is in the code as the string name for the type, but it is not defined specifically with the rest of the code. I can’t say whether this is the base class for PersonRole or not. What is the definition of JobRole? Is unit test similar to a valid example of using a template?
(3) Does anyone have links to a later implementation and an example of using a Role object?

Cheers,
Berryl

public class PersonWithRoles : Person
{
    private readonly IList<PersonRole> _roles = new List<PersonRole>();

    public static PersonWithRoles CreatePersonWithRoles(string identifierName) {
        ...
    }

    public void AddRole(PersonRole role) { _roles.Add(role); }

    public PersonRole RoleOf(string typeName) { return _roles.FirstOrDefault(x => x.HasType(typeName)); }
}

public class PersonRole
{
    public virtual bool HasType(string typeName) { return false; }
}

public class Salesman : PersonRole
{
    public override bool HasType(string typeName)
    {
        if (typeName.Equals("Salesman", StringComparison.InvariantCultureIgnoreCase)) return true;
        if (typeName.Equals("JobRole", StringComparison.InvariantCultureIgnoreCase)) return true;

        return base.HasType(typeName);
    }

    public int NumberOfSales { get; set; }

}

[TestFixture]
public class RoleUsageTests
{
    [Test]
    public void Test() {
        var p = PersonWithRoles.CreatePersonWithRoles("Ted");
        var s = new Salesman();
        p.AddRole(s);

        var tedSales = (Salesman) p.RoleOf("Salesman");
        tedSales.NumberOfSales = 50;
    }
}
+5
source share
2 answers

, , Person, . - , , .

, , , , .

class programmer {
 name ...
 email ...
 seat location ...
}

class html_coder extends programmer {
 canCodeHTML ...
}

class script_coder extends programmer {
 canCodeHTML ...
 canCodeJavascript ...
}

class senior_developer extends programmer {
 canCodeHTML ...
 canCodeJavascript ...
 canAccessDatabase ...
 canEditProjectArchitectureFiles ...
 canWearTennisShoesToWork...
}

... , ...

+5

1) generics #
2) JobRole - PersonRole

, , . Decorator . , , , .

Cheers,
Berryl

public class Person
{
    public FullName FullName { get; set; }

    public Person(FullName fullName) { FullName = fullName; }

    public IList<IRole> Roles { get; private set; }

    public IRole GetRoleOf<T>() where T: IRole{return Roles.FirstOrDefault(x => x.HasType(typeof(T))); }

    public void AddRole(IRole role) { Roles.Add(role); }

    public bool RemoveRole(IRole role) { return Roles.Remove(role); }

}

public interface IRole
{
    bool HasType(Type type);
}

public abstract class Role : IRole
{
    public virtual bool HasType(Type type) { return false; }
}

/// <summary>
/// Base type for any type of role for a person.
/// </summary>
public class PersonRole : Role
{
    public override bool HasType(Type type) { return type.Equals(typeof(PersonRole)); }
}

/// <summary>
/// Base type for any type of role for a person.
/// </summary>
public class JobRole : Role
{
    public override bool HasType(Type type) {
        if(type.Equals(GetType())) return true;
        return base.HasType(type);
    }
}

/// <summary>
/// Behavior specific to a salesman
/// </summary>
public class Salesman : JobRole, ISalesman
{
    public override bool HasType(Type type)
    {
        if (type.Equals(GetType())) return true;
        return base.HasType(type);
    }

    public int NumberOfSales { get; set; }
}

[TestFixture]
public class JobRoleTests : BaseTestFixture
{
    private PersonEx _person;

    protected override void OnFixtureSetUp() {
        _person = new PersonEx(new OneNameFullName("schmuck"));
        // can be a Salesman
        _person.AddRole(new Salesman());
    }

    [Test]
    public void Salesman_CanGet() {
        var salesman = _person.GetRoleOf<Salesman>() as Salesman;
        Assert.That(salesman, Is.Not.Null);
        salesman.NumberOfSales = 50;
        Assert.That(salesman.NumberOfSales, Is.EqualTo(50));
    }
}
+1

All Articles