您的位置 首页 java

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

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

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

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

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输出型对象

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 ,不需要返回

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);
 }
 }
 

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思想。

文章来源:智云一二三科技

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

文章地址:https://www.zhihuclub.com/192852.shtml

关于作者: 智云科技

热门文章

网站地图