java 类、对象、实例、依赖倒置、控制反转、依赖注入等概念

java某些书和网文错误的使用词汇,导致理解上的偏差。只有通过阅读原文,才能明晰。

参考:https://www.oracle.com/technetwork/java/glossary-135216.html

primitive type

A variable data type in which the variable's value is of the appropriate size and format for its type: a number, a character, or a boolean value.

原始类型(数据类型):包括数字、字符和布尔。数字又分整型、浮点,字符分为char和Unicode。

 

class

    In the Java programming language, a type that defines the implementation of a particular kind of object. A class definition defines instance and class variables and methods, as well as specifying the interfaces the class implements and the immediate superclass of the class. If the superclass is not explicitly specified, the superclass will implicitly be Object.

class is a type(和原始类型对比,理解为类类型),class定义规定了实例、类变量和类方法,除此之外还有class实现的接口描述和class的超类

数组、集合、散列、枚举都是数据结构,在java中被封装为类,可以用类类型来称呼。

class method

    A method that is invoked without reference to a particular object. Class methods affect the class as a whole, not a particular instance of the class. Also called a static method . See also instance method .

 

class variable

    A data item associated with a particular class as a whole--not with particular instances of the class. Class variables are defined in class definitions. Also called a static field . See also instance variable .

静态域和静态方法是class才有的描述。

 

object

    The principal building blocks of object-oriented programs. Each object is a programming unit consisting of data ( instance variables ) and functionality ( instance methods ). See also class .

对象是oop的主要构建块,每一个对象都是程序(名词)单元,由实例变量和实例方法组成。

 

instance

    An object of a particular class. In programs written in the Java programming language, an instance of a class is created using the new operator followed by the class name.

实例是专门类的对象(实例就是对象,对象从程序角度,实例从类的角度)

 

instance method

    Any method that is invoked with respect to an instance of a class. Also called simply a method . See also class method .

 

instance variable

    Any item of data that is associated with a particular object. Each instance of a class has its own copy of the instance variables defined in the class. Also called a field . See also class variable .

 

member

A field or method of a class. Unless specified otherwise, a member is not static.

成员是类的域或方法。不特别声明,成员不是static(静态)

 

reference

A variable data type in which the variable's value is an address.

引用(引用变量),被引用变量的数据类型,其值是地址

 

依赖倒置Dependency inversion principle

原本pizza店依赖辣椒pizza、海鲜pizza、蘑菇pizza(向下的依赖箭头)。在工厂模式中,各种pizza先依赖pizza接口(向上的依赖箭头),pizza店依赖pizza接口。从UML图上看是倒置了依赖关系。从思考的角度看,开个pizza店,不需要先考虑pizza菜单,先抽象一个pizza(只有一个属性,名字就叫做pizza),然后开店,然后再设计pizza菜单......(依赖倒置主要是设计思考维度)

java 类、对象、实例、依赖倒置、控制反转、依赖注入等概念

控制反转Inversion of Control

UML中A依赖B和A create B是不一样的。A create B表示A控制B的实例化。控制反转表示A 不create B了,B交给第三方create,A仅仅是依赖B。IoC少不了第三方,如图中的Client类,以后就称为IoC容器。(控制反转主要是设计维度)

java 类、对象、实例、依赖倒置、控制反转、依赖注入等概念

 

依赖注入Dependency injection

C Primer Plus有这样的描述:符号=不表示“相等”,而是一个赋值运算符。下面的语句将值2002赋给名字为bmw的变量:

bmw = 2002;

Java同理,下面的语句表达的意思是:构造一个ClassA对象,并赋值给classA引用。

ClassA classA = new ClassA()  

 

constructor

A pseudo-method that creates an object. In the Java programming language, constructors are instance methods with the same name as their class. Constructors are invoked using the new keyword.

 

赋值符号=的左边就是构造一个ClassA对象,=右边是ClassA类~类型的reference(引用),并且其值只能是地址。(如同c的指针)

Class reference = constructor

注入理解为赋值,依赖注入就是依赖赋值。

设计上有了DIP和IoC,代码层面上就要有实现方法了。

方法1:构造函数注入:在构造的时候,将依赖的ClassB对象作为参数传递给ClassA对象。(ClassA中有reference等着被赋值)

方法2:setter注入:通过ClassA对象的setter方法,将ClassB对象作为参数传递给ClassA对象。(ClassA中有reference等着被赋值)

方法3:定义一个接口A,ClassA类实现接口A的某个方法,将ClassB对象作为参数传递给ClassA对象。(ClassA中有reference等着被赋值)

方法2和方法3的区别还是解耦,任何类只要实现了接口A的这个方法都有依赖注入(依赖赋值)能力。