67

Вопрос по java – Должны ли интерфейсы быть размещены в отдельном пакете? [закрыто]

Я новичок в команде, работающей над довольно большим проектом, с большим количеством компонентов и зависимостей. Для каждого компонента естьinterfaces пакет, в котором размещены открытые интерфейсы для этого компонента. Это хорошая практика?

Моя обычная практика всегда заключалась в том, что интерфейсы и реализации идут в одном пакете.

  • Error: User Rate Limit ExceededpubliclyError: User Rate Limit Exceededstackoverflow.com/questions/4388715/…)

    от
  • Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit ExceededfriendError: User Rate Limit ExceededinternalError: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от
  • Error: User Rate Limit Exceeded

    от finnw
14 ответов
  • 9

    Error: User Rate Limit Exceeded

  • 3

    Error: User Rate Limit Exceeded

  • 1

    Error: User Rate Limit Exceeded

  • 7

    Error: User Rate Limit Exceeded

  • 0

    Error: User Rate Limit Exceeded

  • 2

    Error: User Rate Limit Exceeded

    Separately packaged interfaces and implementations are for a different use case than for the various types of Map or Set. There is no reason to have a package just for trees (java.util.tree.Map, java.util.tree.Set). It is just a standard datastructure, so put it together with the other datastructures. However, if you are dealing with a puzzle game that has a really simple debug interface and a pretty production interface, as part of its front-end you might have a com.your.app.skin.debug and a com.your.app.skin.pretty. I would not put these in the same package, because they do different things and I know I would resort to some SmurfNamingConvention (DebugAttackSurface, DebugDefenceSurface, PrettyAttackSurface etc) to create some informal namespace for the two if they were in the same package.

    My workaround for the problem of finding related interfaces and implementations that are in separate packages is to adopt a naming configuration for my packages. E.g. I can have all my interfaces in com.your.app.skin.framework, and know that other packages on the same level of the package tree are implementations. The disadvantage is that this is an unconventional convention. Honestly, I will see how good this convention is in 6 months time :)

    I do not use this technique religiously. There are interfaces which only make sense in a particular implementation. I don't stick them in the framework package. There are some packages where it doesn't look like I'm going to create 40 different implementation classes, so I don't bother.

    My application uses guice and has very many interfaces.

  • -1

    Error: User Rate Limit Exceeded

  • 0

    Error: User Rate Limit Exceeded

  • 2

    Error: User Rate Limit Exceeded

  • 17

    Error: User Rate Limit Exceeded

  • 85

    Error: User Rate Limit Exceeded

    java.util

    Set, Map, ListHashSet, HashMapArrayList.

    InterfacesClasses

    Prefix the interface name with an I. This approach is taken with the interfaces in the .NET framework. It would be fairly easy to tell that IList is an interface for a list.

    Use the -able suffix. This approach is seen often in the Java API, such as Comparable, Iterable, and Serializable to name a few.

  • 4

    Error: User Rate Limit Exceeded

  • 5

    Error: User Rate Limit Exceeded



    Downward Dependency Principle (DDP) Upward Notification Principle (UNP) Neighbor Communication Principle (NCP) Explicit Association Principle (EAP) Cycle Elimination Principle (CEP) Class Naming Principle (CNP) Acquaintance Package Principle (APP)

    The Neighbor Communication Principle demands that a package can only communicate directly with its neighbor package. This principle ensures that the system does not disintegrate to an incompressible network of intercommunicating objects. To enforce this principle, message passing between non-neighboring objects uses delegation (Section 9.1.5.1). In more complex scenarios, acquaintance package (Section 9.1.8.2) can be used to group interfaces to assist in collaboration that engages distant packages.

    The Acquaintance Package Principle is the consequence of the Neighbor Communication Principle. The acquaintance package consists of interfaces that an object passes, instead of concrete objects, in arguments to method calls. The interfaces can be implemented in any PCMEF package. This effectively allows communication between non-neighboring packages while centralizing dependency management to a single acquaintance package. The need for acquaintance package was explained in Section 9.1.8.2 and is discussed again next in PCMEF context.

  • 0

    Error: User Rate Limit Exceeded