I spent the weekend looking through the JDK source code, hoping to learn some object-oriented design ideas.

Read mainly from the most commonly used Collection. Take the time to do some summary, if there is any mistake, welcome to correct.

ArrayList design path:

  1. It defines the concept of a Collection and what a Collection can accomplish
  2. On the one hand, AbstractCollection is formed by externalizing the common part of the set.
  3. On the other hand, subdivide a Collection into interfaces such as List and Set, each inheriting the Collection and adding its own ‘Collection’ characteristics (e.g., Set is non-repeatable, List is repeatable, etc.)
  4. AbstractList abstract class AbstractList abstract class AbstractList abstract class AbstractList abstract class AbstractList abstract class AbstractList abstract class AbstractList abstract class AbstractList abstract
  5. AbstractList completely externalizes AbstractList and implements it in different forms according to the interface specification: ArrayList, LinkList; At this point, a design link is complete, and Cloneable() is implemented in the embodiment of ArrayiList.

Collection collectionString = new ArrayList ();

  1. The answer is yes, because it is derived from Collection
  2. Why doesn’t anyone use it that way? My understanding is that it is impossible to customize the ability to use the List, only the ability to use the most basic collection, there is no need to do so.

It’s interesting that ArrayList inherits AbstractList. AbstractList implements List, but ArrayList implements List. Found a saying on Stack Overflow:



That is the original author said, this is a mistake, before feel is meaningful.

Summary methodology:

  1. Find the abstraction at the top of the concept and define the basic functionality. (Example: file storage, abstract: FileStore interface; UpLoad (), downLoad() methods
  2. The basic functions defined are carefully divided down. If the basic functions can be processed uniformly, a template method is used to Abstract an Abstract class and do common processing in this class. (If not, I don’t need to do this step, but I usually have an Abstract between interface and class for further extension.)
  3. Visualize the top level abstraction and decompose it into different implementations. (For example, cloud storage cloudFilestore Interface and LocalFileStore interface; And then half of the cloud will have keys, encryption, decryption, etc., defined in this interface.)
  4. Do the final implementation for different ways. (Example: cloudFileStore interface is visualized as AliCloudFileStore class, TencentCloudFIleStore; The ability of the two classes to do their own file storage)
  5. FileStore FileStore = new AliCloudFileStore() or CloudFileStore store = new AliCloudFileStore(); fileStore.upLoad();

What are the benefits:

  1. Easy to expand. If new requirements arise in the project, new storage methods need to be added, such as Baidu cloud storage. You only need to find CloudFileStore interface and implement it.
  2. The user does not care what FileStore is used for storage. The user can request that a FileStore interface be passed in. It does not matter whether you pass in AliCloudFileStore or TencentCloudFIleStore. Upload () on the user.