简体   繁体   English

设计方法(域驱动或服务驱动)

[英]Design approach (Domain driven or Service Driven)

My problem statement is : 我的问题陈述是:

I want to write design file management (add, copy, delete etc. operations). 我想写设计文件管理(添加,复制,删除等操作)。 There are two approach : 有两种方法:

  1. Service Driven approach 服务驱动的方法

Write file VO which contains only file attributes. 写入仅包含文件属性的文件VO。 For eg 例如


public Class File {
    private boolean hidden;
    private boolean read;
    private boolean write;

    public boolean isHidden() {
        return hidden;
    }
    public void setHidden(boolean hidden) {
        this.hidden = hidden;
    }
    public boolean isRead() {
        return read;
    }
    public void setRead(boolean read) {
        this.read = read;
    }
    public boolean isWrite() {
        return write;
    }
    public void setWrite(boolean write) {
        this.write = write;
    }
}

and separates service for File related operations. 并为文件相关操作分离服务。 For eg : 例如:


public Class FileService {
        public boolean deleteFile(File file) {
            //Add delete logic.
        }
        //Same way you can add methods for Add and copy file.
}
  1. Domain Driven approach (I might be wrong here.) 领域驱动的方法 (我可能在这里错了。)

Where file VO contains all the attributes plus required operations : 文件VO包含所有属性和所需操作的位置:


public class File {
    private boolean hidden;
    private boolean read;
    private boolean write;

    public boolean isHidden() {
        return hidden;
    }
    public void setHidden(boolean hidden) {
        this.hidden = hidden;
    }
    public boolean isRead() {
        return read;
    }
    public void setRead(boolean read) {
        this.read = read;
    }
    public boolean isWrite() {
        return write;
    }
    public void setWrite(boolean write) {
        this.write = write;
    }
        public boolean deleteFile() {
            //Add delete logic.
        }
        //Same way you can add methods for Add and copy file.
}

So what are the pros and cons of both the approach ? 那么这两种方法的优点和缺点是什么?

In an object oriented language, putting the logic in the class itself, rather than a service class, is the typical approach (and better IMO). 在面向对象的语言中,将逻辑放在类本身而不是服务类中是典型的方法(以及更好的IMO)。 It follows the "tell, don't ask" principle, for example, by telling a File to delete itself, rather than asking some service to delete it. 它遵循“告诉,不要问”原则,例如,通过告诉文件删除自己,而不是要求某些服务删除它。 One of the main reasons behind this is to allow for inheritance. 这背后的主要原因之一是允许继承。 For example, if you have a subclass of File and wanted to have it write a log message before it was deleted, that would be difficult to do with a service class because you would need a different service class for every subclass. 例如,如果您有一个File的子类并希望在删除它之前编写一条日志消息,那么对于服务类来说这很难做到,因为您需要为每个子类使用不同的服务类。

In terms of a service-oriented approach, this is typically thought of at a higher level (ie a service oriented architecture). 就面向服务的方法而言,这通常被认为是更高层次的(即面向服务的架构)。 Consider a financial stock system, you might have a "buy stock" service and a "sell stock" service. 考虑一个金融股票系统,您可能有“买入股票”服务和“卖出股票”服务。 Have a service class that corresponds to individual classes (ie a Stock service, which knows how to buy and sell stocks) wouldn't be very object oriented. 拥有一个对应于各个类的服务类(即一个知道如何买卖股票的股票服务)不会非常面向对象。

You might also have a service layer in your system, which provides integration points with other external services (ie a database), but again, I don't think this is what you're talking about here. 您可能还在系统中有一个服务层,它提供与其他外部服务(即数据库)的集成点,但同样,我不认为这就是您在这里所说的。 So, I could stick with the approach of encapsulating the logic in the File class itself. 所以,我可以坚持在File类本身封装逻辑的方法。

Without much information about what kind of system your are desigining it's hard to pronounce. 如果没有关于你正在设计什么样的系统的信息很多,那么很难发音。 To me, the choice depend on the system boundaries. 对我而言,选择取决于系统边界。

If you need to offer an API that is exposed as a service and accessible to external consumer, go for solution 1, that's the only way. 如果您需要提供作为服务公开且可供外部消费者访问的API,请转到解决方案1,这是唯一的方法。 If you system is rather an library whose API will be used internally by other applications, go for a rich domain model as in solution 2, it's a lot more OO. 如果你的系统是一个库,其API将由其他应用程序在内部使用,那么在解决方案2中寻找一个丰富的域模型,它就是更多的OO。 You don't want to bloat your API with service-, manager-, and utility classes for which no real reason exist. 您不希望使用没有真正原因的服务,管理器和实用程序类来扩展您的API。

But again, without knowing your final goal, it's hard to say. 但同样,在不知道你的最终目标的情况下,很难说。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM