包裹可见性 [英] package visibility

查看:36
本文介绍了包裹可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么使用包可见性(默认),除非类在 java 中应该是公共的

Why use package visibility (the default), unless the class should be public in java

推荐答案

正如 Rostislav Matl 所说,当你想要制作一些不构成包界面的一部分的东西时,它很有用.

As Rostislav Matl said, it's useful for when you want to make something that doesn't form part of your package's interface.

举个例子,假设你有一个包,它提供了一个接口和至少一个服务的具体实现.

As an example, imagine you have a package and it provides an interface and at least one concrete implementation of a service.

使用此服务的人会关心您提供的接口并使用您提供的具体类之一,但除此之外他们不会关心其他太多事情.我们的服务必须与数据库通信,并且需要能够将数据库查询的结果映射到它自己的数据类型(形成它的契约).

People who use this service are going to care about the interface you provide and use one of the concrete classes you provide but they aren't going to care about much else beyond that. Our service has to talk to a database and it needs to be able to map the result from database queries into its own data type (that form its contract).

我发现我经常创建包私有帮助程序类,这些类包含实用程序类型方法或执行我们需要的映射等任务.默认(包私有)可见性非常适合这种情况,因为包内的其他类可以使用这些帮助程序,但包外的人无法看到它们,因此您可以随时更改它们.

I find that I regularly create package private helper classes that contain utility type methods or perform tasks like the mapping that we need. Default (package private) visibility is perfect for this because other classes inside your package can use these helpers but no-one outside the package can see them so you're free to change them whenever you like.

这是一个使用一些代码的例子:

This is a an example using some code:

我们有我们的界面:

public interface UsefulService {
    Collection<DataThings> getThings(Identifier id);
}

...以及我们的具体实现:

...and our concrete implementation:

public class JdbcUsefulServiceImpl implements UsefulService {

     //We can break the code for the mapping out into its own class
    private Mapper mapper;

    @Override
    public Collection<DataThings> getThings(Identifier id){
        DatabaseQueryResult queryResult = //Code to hit a database and return objects from that domain model
        Collection<DataThings> result = mapper.mapFromDatabaseToServiceDomain(queryResult);
        return result;
    }
}

然后我们有了我们的映射器.我们不需要包外的任何人来关心内部的服务工作,所以我们使用包私有可见性,我们可以拥有尽可能多的类来完成工作:

Then we have our mapper. We don't need anyone outside the package to care about the service works internally so we use package private visibility and we can have as many classes as we want to get the job done:

class Mapper {
    Collection<DataThings> mapFromDatabaseToServiceDomain(DatabaseQueryResult queryResult){
        //magic to map objects goes here
    }
}

我们的好处是我们可以随时更改这个 Mapper 类,无论我们想要或删除它或创建新的包私有类,而且我们知道我们唯一能引起的(立即)影响是在这个包内.我所说的即时影响是指编译器错误和诸如此类的严重问题.显然,如果您更改其行为,您可能会破坏您的服务,但这就是您的自动化测试套件要捕获的内容:P

The benefit that we have is that we can always change this Mapper class however we want or delete it or create new package private classes and we know the only (immediate) effects we can cause are inside this package. By immediate effects I mean compiler errors and serious things like that. Obviously you could break your service if you change its behaviour but that's what your automated test suite is there to catch :P

这篇关于包裹可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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