Java底层系列:通过OOP与AOP思想看java.util中的College类

OOP(Object Oriented Programming):面向对象编程;

AOP(Aspect Oriented Programming):面向切面编程;

如果OOP是纵向的,那么AOP就是横向的;如果OOP是横向的,那么AOP就是纵向的。总之,它们是交叉的,一个主线,一个插曲。

17138799-9aa6d6da3b2044f8
Java底层系列:通过OOP与AOP思想看java.util中的College类

图1

图1中,有以下接口,它们的声明都带有@FunctionalInterface注解,即代表某种功能,如下,

interface Comparator<T>:比较器

interface Function<T, R>:Represents a function that accepts one argument and produces a result,R代表result, 表示一有进有出(有输入有返回值)的功能对象

interface Consumer<T>:Represents an operation that accepts a single input argument and returns no result. Unlike most other functional interfaces, {@code Consumer} is expected to operate via side-effects, 表示一有进无出(有输入无返回值)的消耗对象

interface Predicate<T>:Represents a predicate (boolean-valued function) of one argument, 表示判断是否的谓动词对象

interface Supplier<T>:Represents a supplier of results,表示一目标结果T输出型对象

17138799-9d00f4cc3ca3dca3
Java底层系列:通过OOP与AOP思想看java.util中的College类

图2, 向上的条状箭头指向方法返回的类型,向下的线箭头指向子类

图2 中是集合类对象,具体如下

final class Optional<T>:A container object which may or may not contain a non-null value. 可含一个value的容器对象

Iterator<E>:An iterator over a collection. 一集合迭代器

interface Iterable<T>:Implementing this interface allows an object to be the target of the "for-each loop" statement,迭代器的扩展,将迭代元素T送入foreach,不需要返回

<pre style="-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">public interface Iterable<T> {
 Iterator<T> iterator();
 default void forEach(Consumer<? super T> action) {
 Objects.requireNonNull(action);
 for (T t : this) {
 action.accept(t);
 }
 }
 default Spliterator<T> spliterator() {
 return Spliterators.spliteratorUnknownSize(iterator(), 0);
 }
 }
</pre>

interface Stream:A sequence of elements supporting sequential and parallel aggregate operations. 一元素序列对象,支持并列、顺序等聚合操作。

interface Collection<E>: The root interface in the collection hierarchy. 集合类的根接口

图1,从迭代器到集合,从迭代器到序列流,反映的是一种OPP思想,是对象之间的关系,是父子继承关系,或是方法返回值关联关系;而无论迭代器、流,还是集合,都或多或少有用到图2的功能对象来切入实现某个局部,如上述Iterable定义中的Consumer,这是一种AOP思想。

转载于:https://www.jianshu.com/p/fb75b6932aa6

猜你喜欢

转载自blog.csdn.net/weixin_34208185/article/details/91303292
今日推荐