本文介绍了我如何设计更好? (避免使用面向对象设计的switch语句)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一点关于,但我不知道如何在我的代码中使用这些原则。以下是我正在进行的工作:

  public void Query(Agency Agency,Citation queryCitation){
queryCitation.AgencyCode = agency.AgencyCode;

switch(agency.ClientDb.Type){
caseSQL:
QueryOracle(agency,queryCitation);
break;
casePIC:
QueryPick(agency,queryCitation);
break;
}
}

(大部分是NHibernate的对象, m使用遗留数据库系统,并将其部分重构为代码库。)显然,我可以在这里做不同的处理,因此我不需要具有相同输入的不同数据库查询的重复功能。它应该只是基于代理对象知道是使用Oracle数据库还是使用Pick数据库连接。 (如果你从来没有听说过Pick数据库,那么我直到我开始在这里工作就没有,我们通过HTTP请求来查询它,所以不是SQL。)



应该做一个接口,例如叫做ClientDbConnection,然后使两个实现该接口的类,移动代码来查询数据库,然后使用agency.clientDb.Query(queryCitation)取代这个整个功能?我想我在这里大声思考,但是对此的任何意见都将不胜感激。

解决方案

如果是这样的话:

  public abstract class GenericDb 
{
public abstract void Query(parms );
}

在您的代理商类中,您可以拥有

  public GenericDb ClientDb {get; set;} 

然后有一个SqlDb类,如:

  public class SqlDb:GenericDb 
{
public void Query(parms);
}

public class PicDb:GenericDb
{
public void Query(parms);
}

然后这段代码:

  public void Query(Agency Agency,Citation queryCitation){
queryCitation.AgencyCode = agency.AgencyCode;

switch(agency.ClientDb.Type){
caseSQL:
QueryOracle(agency,queryCitation);
break;
casePIC:
QueryPick(agency,queryCitation);
break;
}
}

成为

  public void Query(Agency Agency,Citation queryCitation){
queryCitation.AgencyCode = agency.AgencyCode;
agents.ClientDb.Query(queryCitation);
}

由于继承,它将会知道ClientDb有一个GenericDb的基类。它将通过ClientDb参数的类型知道是否应该运行SqlDb或PicDb或Oracle等。


I know a little bit about Object Oriented design, but I'm not sure how to go about using those principles in my code. Here's what I'm working on:

    public void Query(Agency agency, Citation queryCitation) {
        queryCitation.AgencyCode = agency.AgencyCode;

        switch (agency.ClientDb.Type) {
            case "SQL":
                QueryOracle(agency, queryCitation);
                break;
            case "PIC":
                QueryPick(agency, queryCitation);
                break;
        }
    }

(Most of these are objects from NHibernate. I'm working with a legacy database system and am refactoring parts of it into a code library.) Clearly, I could do something differently here so that I don't need duplicate functions for different database queries that have the same input. It should just know based off of the agency object whether to use an Oracle database or a Pick database connection. (If you've never heard of a Pick database, well I hadn't either until I started working here. We make queries to it through HTTP requests, so it's not SQL.)

Should I make an interface, for example called "ClientDbConnection" and then make two classes that implement that interface, move the code to query the database to those and then have something like "agency.clientDb.Query(queryCitation)" replace this whole function? I guess I'm thinking aloud here, but any input on this would be appreciated.

解决方案

Is Agency a class you control? If so do something like this:

public abstract class GenericDb
{
    public abstract void Query(parms);
}

In your Agency Class, you could have

public GenericDb ClientDb {get; set;}

Then have a SqlDb class like:

public class SqlDb : GenericDb
{
    public void Query(parms);
}

public class PicDb : GenericDb
{
    public void Query(parms);
}

Then this code:

public void Query(Agency agency, Citation queryCitation) {
        queryCitation.AgencyCode = agency.AgencyCode;

        switch (agency.ClientDb.Type) {
            case "SQL":
                QueryOracle(agency, queryCitation);
                break;
            case "PIC":
                QueryPick(agency, queryCitation);
                break;
        }
    }

becomes

public void Query(Agency agency, Citation queryCitation) {
        queryCitation.AgencyCode = agency.AgencyCode;
        agency.ClientDb.Query(queryCitation);
    }

Because of inheritance, it will know that ClientDb has a base class of GenericDb. It will know by the type of the ClientDb parameter whether it should run the SqlDb or the PicDb or Oracle etc.

这篇关于我如何设计更好? (避免使用面向对象设计的switch语句)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:20