哪种设计最适合这种情况?
Which design is the best for this scenario?
谁能告诉我哪种设计最适合这种情况?
我有 类 叫 BasicAccount
、SavingAccount
和 CurrentAccount
。
SavingAccount
和 CurrentAccount
应该具有 BasicAccount
的所有功能。稍后可能会出现这种情况,我们可能会引入一个名为 AdvanceAccount
的新帐户,它应该具有 CurrentAccount
和 SavingAccount
的所有功能。我应该如何设计结构?
我的回答:
让BasicAccount
保持抽象,SavingAccount
也是抽象,并实现BasicAccount
创建一个接口ICurrentAccount
和currentaccount实现BasicAccount
和ICurrentAccount
,
如果我们有 AdvanceAccount
然后用 SavingAccount
和 ICurrentAccount
实现它。
有没有更好的方法?这是在面试中被问到的,我猜面试官对我的回答不满意。
我会使用这样的东西:
abstract class BasicAccount {}
interface ISavingAccount {}
interface ICurrentAccount {}
class SavingAccount : BasicAccount, ISavingAccount {}
class CurrentAccount : BasicAccount, ICurrentAccount {}
class AdvanceAccount : BasicAccount, ISavingAccount, ICurrentAccount {}
如果SavingAccount
和CurrentAccount
有很多功能,可以在AdvanceAccount
实现中使用聚合:
class AdvanceAccount : BasicAccount, ISavingAccount, ICurrentAccount
{
private readonly SavingAccount savingAccount;
private readonly CurrentAccount currentAccount;
public AdvanceAccount()
{
savingAccount = new SavingAccount(...);
currentAccount = new CurrentAccount(...);
}
// redirect ISavingAccount and ICurrentAccount implemetation
// to savingAccount and currentAccount respectively
}
UPD.
请注意,在 AdvanceAccount
中直接实例化 SavingAccount
和 CurrentAccount
只是一个示例。 IRL 你可能会使用 IoC 容器。
我想说装饰器模式在这里比传统的继承更适用。
public interface IBasicAccount { }
public interface ISavingAccount { }
public interface ICurrentAccount { }
public interface IAdvanceAccount { }
public class BasicAccount : IBasicAccount { }
public class SavingAccount : ISavingAccount
{
// methods may use basic account internally to delegate some function calls
private readonly IBasicAccount _basicAccount;
public SavingAccount(IBasicAccount basicAccount)
{
_basicAccount = basicAccount;
}
}
public class CurrentAccount : ICurrentAccount
{
private readonly IBasicAccount _basicAccount;
public CurrentAccount(IBasicAccount basicAccount)
{
_basicAccount = basicAccount;
}
}
public class AdvanceAccount : IAdvanceAccount
{
private readonly ISavingAccount _savingAccount;
private readonly ICurrentAccount _currentAccount;
public AdvanceAccount(ISavingAccount savingAccount, ICurrentAccount currentAccount)
{
_savingAccount = savingAccount;
_currentAccount = currentAccount;
}
}
您可以在需要时将调用委托给内部 IBasicAccount、ISavingAccount 和 ICurrentAccount 实现。
此外,如果您的业务模型需要,您可以使 ISavingAccount
和 ICurrentAccount
继承自 IBasicAccount
。
这是一个经典的菱形问题https://en.wikipedia.org/wiki/Multiple_inheritance
有多种方式来看待它。最后还是要看你其他方面的设计了。
- 其中一种方法是使用行为创建继承
- 其他是利用数据创建继承(派生类主要是共享数据)
如果您可以使用服务拆分行为,您应该能够将行为添加/删除到您想要的任何新 类 中,如果它们可以派生,它们应该只从基本帐户派生来自其他子 类 那么这是一个奖励...
除了已经说过的之外,如果我们不同时需要 SavingAccount
和 CurrentAccount
实例,我们可以使用这样的东西:
abstract class BasicAccount
{
// .. some implementation here
}
class SavingAccount : BasicAccount { }
class CurrentAccount : BasicAccount { }
class AdvancedAccount<T> where T : BasicAccount
{
private readonly T _instance;
public T AccountToWorkWith { get { return _instance; } }
public AdvancedAccount(BasicAccount instance)
{
this._instance = instance as T;
}
}
谁能告诉我哪种设计最适合这种情况?
我有 类 叫 BasicAccount
、SavingAccount
和 CurrentAccount
。
SavingAccount
和 CurrentAccount
应该具有 BasicAccount
的所有功能。稍后可能会出现这种情况,我们可能会引入一个名为 AdvanceAccount
的新帐户,它应该具有 CurrentAccount
和 SavingAccount
的所有功能。我应该如何设计结构?
我的回答:
让
BasicAccount
保持抽象,SavingAccount
也是抽象,并实现BasicAccount
创建一个接口
ICurrentAccount
和currentaccount实现BasicAccount
和ICurrentAccount
,如果我们有
AdvanceAccount
然后用SavingAccount
和ICurrentAccount
实现它。
有没有更好的方法?这是在面试中被问到的,我猜面试官对我的回答不满意。
我会使用这样的东西:
abstract class BasicAccount {}
interface ISavingAccount {}
interface ICurrentAccount {}
class SavingAccount : BasicAccount, ISavingAccount {}
class CurrentAccount : BasicAccount, ICurrentAccount {}
class AdvanceAccount : BasicAccount, ISavingAccount, ICurrentAccount {}
如果SavingAccount
和CurrentAccount
有很多功能,可以在AdvanceAccount
实现中使用聚合:
class AdvanceAccount : BasicAccount, ISavingAccount, ICurrentAccount
{
private readonly SavingAccount savingAccount;
private readonly CurrentAccount currentAccount;
public AdvanceAccount()
{
savingAccount = new SavingAccount(...);
currentAccount = new CurrentAccount(...);
}
// redirect ISavingAccount and ICurrentAccount implemetation
// to savingAccount and currentAccount respectively
}
UPD.
请注意,在 AdvanceAccount
中直接实例化 SavingAccount
和 CurrentAccount
只是一个示例。 IRL 你可能会使用 IoC 容器。
我想说装饰器模式在这里比传统的继承更适用。
public interface IBasicAccount { }
public interface ISavingAccount { }
public interface ICurrentAccount { }
public interface IAdvanceAccount { }
public class BasicAccount : IBasicAccount { }
public class SavingAccount : ISavingAccount
{
// methods may use basic account internally to delegate some function calls
private readonly IBasicAccount _basicAccount;
public SavingAccount(IBasicAccount basicAccount)
{
_basicAccount = basicAccount;
}
}
public class CurrentAccount : ICurrentAccount
{
private readonly IBasicAccount _basicAccount;
public CurrentAccount(IBasicAccount basicAccount)
{
_basicAccount = basicAccount;
}
}
public class AdvanceAccount : IAdvanceAccount
{
private readonly ISavingAccount _savingAccount;
private readonly ICurrentAccount _currentAccount;
public AdvanceAccount(ISavingAccount savingAccount, ICurrentAccount currentAccount)
{
_savingAccount = savingAccount;
_currentAccount = currentAccount;
}
}
您可以在需要时将调用委托给内部 IBasicAccount、ISavingAccount 和 ICurrentAccount 实现。
此外,如果您的业务模型需要,您可以使 ISavingAccount
和 ICurrentAccount
继承自 IBasicAccount
。
这是一个经典的菱形问题https://en.wikipedia.org/wiki/Multiple_inheritance
有多种方式来看待它。最后还是要看你其他方面的设计了。
- 其中一种方法是使用行为创建继承
- 其他是利用数据创建继承(派生类主要是共享数据)
如果您可以使用服务拆分行为,您应该能够将行为添加/删除到您想要的任何新 类 中,如果它们可以派生,它们应该只从基本帐户派生来自其他子 类 那么这是一个奖励...
除了已经说过的之外,如果我们不同时需要 SavingAccount
和 CurrentAccount
实例,我们可以使用这样的东西:
abstract class BasicAccount
{
// .. some implementation here
}
class SavingAccount : BasicAccount { }
class CurrentAccount : BasicAccount { }
class AdvancedAccount<T> where T : BasicAccount
{
private readonly T _instance;
public T AccountToWorkWith { get { return _instance; } }
public AdvancedAccount(BasicAccount instance)
{
this._instance = instance as T;
}
}