Facade Design Patterns and Subclassing
I am using a facade design pattern for a C# program. The program basically looks like this...
public class Api
{
#region Constants
private const int 开发者_如何学Goversion = 1;
#endregion
#region Private Data
private XProfile _profile;
private XMembership _membership;
private XRoles _role;
#endregion Private Data
public Api()
{
_membership = new XMembership();
_profile = new XProfile();
_role = new XRoles();
}
public int GetUserId(string name)
{
return _membership.GetIdByName(name);
}
}
Now, as I would like subclass my methods into three categories: Role, Profile, and Member. This will be easier on the developers eye because both Profile and Membership expose a lot of methods that look similar (and a few by Role). For example, getting a user's ID would look like:
int _id = Namespace.Api.Member.GetUserId("Henry222");
Can somebody "illustrate" how subclassing should work in this case to achieve the effect I am looking for?
Thanks in advance.
I would argue that you might mean "inner class" from the context of your question. You might try something like the following...
public class Api
{
public class MemberApi
{
private readonly Api _parent;
internal MemberApi(Api parent) { _parent = parent; }
public int GetUserId(string name)
{
return _parent._membership.GetIdByName(name);
}
}
#region Constants
private const int version = 1;
#endregion
#region Private Data
private XProfile _profile;
private XMembership _membership;
private XRoles _role;
public MemberApi { get; private set; }
#endregion Private Data
public Api()
{
_membership = new XMembership();
_profile = new XProfile();
_role = new XRoles();
Member = new MemberApi(this);
}
}
精彩评论