Design Patterns
Single-Case mode
Definition: Ensure that a class has only one instance, and that it instantiates itself and provides this instance to the system as a whole.
Description: You can only use the methods provided by the Singleton class to get the Singleton object, do not use reflection, or you will instantiate a new object. Do not break a dangerous operation that breaks a static reference to a singleton class object with a class. Use a single instance of multithreading when using shared resources, pay attention to thread safety issues.
Factory method Mode
Definition: Defines an interface that is used to create an object, so that subclasses decide which class to instantiate, and the factory method defers the instantiation of a class to its subclasses.
Description: In the factory method mode, the core factory class is no longer responsible for the creation of all objects, but rather the creation of the work to the sub-class to do. This core class is transformed into an abstract factory role that is only responsible for giving the interfaces that a specific factory subclass must implement, without touching which class should be instantiated in this detail.
Abstract Factory mode
Definition: Provides an interface for creating a set of related or interdependent objects without specifying their specific classes.
Description: Applies to the Factory method mode in the following cases:
(1) When a class does not know the class of the object it must create.
(2) When a class wants to specify the object it creates by its subclasses.
(3) When a class delegates the responsibility of creating an object to one of several helper subclasses, and you want to use which helper subclass is the proxy information localization.
Template method Mode
Definition: Defines a framework for an algorithm in an operation, and delays some steps into subclasses so that subclasses can redefine some specific steps in the algorithm without altering the structure of the algorithm.
Description: Subclasses can displace mutable portions of a parent class, but subclasses cannot change the top-level logic represented by a template method.
Whenever a new subclass is defined, do not think in terms of the flow of control, but in accordance with the "responsibility" thinking. In other words, you should consider which operations must be replaced, which operations can be displaced, and which actions are not replaceable. Using template mode can make these responsibilities clear.
Proxy mode
Definition: Provides a proxy for other objects to control access to this object.
Description: The so-called agent is one person or institution acting on behalf of another person or institution. In some cases, a client does not want or cannot refer directly to an object, whereas a proxy object can act as an intermediary between the client and the target object.
Adapter mode
Definition: Provide interfaces to customers based on the services provided by existing classes to meet customer expectations. The adapter mode is intended to change the interface of the source so that it is compatible with the target interface. The default adaptation is slightly different, and it is a mediocre implementation provided to facilitate the creation of a non-mediocre adapter class.
Description: Advantages of Adapter Mode
Better reusability
The system needs to use the existing classes, and the interfaces of this class do not meet the needs of the system. The adapter mode allows for better reuse of these features.
Better extensibility
When implementing the adapter functionality, you can invoke the features you have developed to naturally extend the functionality of the system.
Disadvantages of Adapter Mode
Excessive use of the adapter, will make the system very messy, not easy to grasp the overall. For example, clearly see the call is a interface, in fact, the interior is adapted to the implementation of the B interface, a system if too many occurrences of this situation, is tantamount to a disaster. So if it's not necessary, you can refactor the system without using the adapter.
Iterator mode
Definition: Provides a way to access individual elements in a container object without exposing the internal details of the object.
Description: In the JDK, there are two interfaces associated with iterators: Iterator and Iterable
Iterator: Iterators, Iterator and their subclasses are often the structures and methods of the iterators themselves;
Iterable: Iterative, other classes that want to use iterator functionality, such as Abstractlist HashMap, need to implement this interface.
Observer pattern
Definition: Defines a one-to-many dependency between objects so that when each object changes state, all objects that depend on it are notified and updated automatically.
The Observer pattern defines a one-to-many dependency that allows multiple observer objects to listen to a Subject object at the same time. When the subject object changes in state, all observer objects are notified so that they can automatically update themselves.
Description: In the Java language Java.util Library, a observable class and a observer interface are provided, which form the Java language support for the Observer pattern.
Common design pattern Concepts for Android