几乎相似的对象的一个​​很好的设计模式 [英] A good design pattern for almost similar objects

查看:153
本文介绍了几乎相似的对象的一个​​很好的设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个网站有几乎相同的数据库模式。唯一的区别是,一个网站中的一些表格有一个或两个额外的字段,另一个表示另一个,反之亦然。



我想要使用相同的数据库访问层类来操纵两个网站。



什么可以是一个很好的设计模式,可以用来处理这一点。



例如,我的DAO类中有一个方法 createAccount(帐户帐户),但网站A和网站B之间的实现将略有不同。

解决方案

如果对象的实现也将几乎相同,我建议使用抽象基类。通过继承来扩展类,并确保您不知道扩展字段的函数需要基类,而不是派生类。

  class MercedesBenzC300:Car 
{
int brake();
void TurnOnRadio();
}

class MercedesBenzC300Business:MercedesBenzC300
{
int EnableCruiseControl();
}

所以在这个例子中,我们有两辆几乎完全一样,一个是商业版,所以它有巡航控制。所有不与cruisecontrol相互作用的功能也可以看作普通车。只有那些应该使用巡航控制的人才能得到派生类。


I have two websites that have an almost identical database schema. the only difference is that some tables in one website have 1 or 2 extra fields that the other and vice versa.

I wanted to the same Database Access layer classes to will manipulate both websites.

What can be a good design pattern that can be used to handle that little difference.

for example, I have a method createAccount(Account account) in my DAO class but the implementation will be slightly different between website A and website B.

解决方案

If the implementation of the objects would also be almost the same, I would suggest to use an Abstract Base Class. Extend the class by inheritance and make sure your functions that are not aware of the extended field require the base class and not the derived class.

class MercedesBenzC300 : Car
{
     int brake();
     void TurnOnRadio();
}

class MercedesBenzC300Business : MercedesBenzC300
{
    int EnableCruiseControl();
}

So in this example we have two cars which are almost exactly the same, however, one is a business edition so it has cruise controle. All functions that do not interact with the cruisecontrol can also see it as an ordinary car. Only those that should use the cruise control, should now get the derived class.

这篇关于几乎相似的对象的一个​​很好的设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆