The loop can be done independently of the data. Whether it's a List, an array, a Set, or whatever, it's a similar call.
When you want to use a function implemented in an existing class in another class, perform type conversion so that the function can be used in the type you want to use.
By using inheritance, you can standardize the general logic while expressing small differences in subclasses.
By implementing the main program using the common Factory interface and Product interface, the behavior of the generated product can be changed simply by changing the specific type of factory (implemented in the interface).
You can limit the class instance to only one.
By registering the instance of each class in the organizer (Prototype role) in advance, it is possible to copy and create the necessary instance from the instance registered in the organizer.
--Eliminates the need to write new Something in the program itself when creating an instance. --Many classes can be managed together. --You can create an instance that is difficult to create by copying.
It's similar to the Template Method, but it uses delegation rather than inheritance, and uses the methods of the class that represent the subtle differences to build the general logic. As a result, the logic can be shared
Use when you want to create something (for example, html file). The interface of the method that creates the parts that make up the thing you want to make is fixed, and you call the method as many times as you like with any argument to create what you want to make. By leaving the implementation of the method that creates the parts that make up what you want to make to the implementation class, you can express the difference and make it resistant to changes. For example, in creating html. When you click the link part, you can express whether to transition in the same tab or open the link in a new tab by changing the implementation class.
You can separate the function class and the implementation class (small differences). That is, the logic is assembled in the function class using a common interface for the implementation class. By doing this, when adding a function, the function class should be added, and if the behavior (implementation) is to be changed, the implementation class should be changed.
Only the algorithm can be switched by delegation. This makes it possible to easily switch the output calculation method for input. For example, "fast but inaccurate algorithm" and "slow but accurate algorithm" can be used properly depending on the situation.
Make it possible to identify the container (plural) and the contents (singular). For example, in the implementation of HashMap, the contents (singular) basically correspond to each hash value, but when a collision occurs, it corresponds by putting a container (plural) in the hash value.
If the class implements the same interface, you can add functions as many times as you like by delegation. The image is for the person who put up the shield
When thinking about, the image is that when you add it, you can add it as it is to the previous state, and eventually a person who has acquired all the functions will be born. This pattern is used in InputStreamReader, BufferReader, FileReader, etc. of java.io.
Separate data structure and processing. When a class that performs processing (what to do depends on the implementation) is given to the class that expresses the data structure, the method that performs the processing is called by delegation and the data is processed.
14. Chain of Responsibility
Turn the process around by delegation. The role of Handler (processing role) performs only the processing that can be done by oneself, and entrusts the processing to the next class if it cannot be kept in the hands of oneself. It is also possible to change the class that processes during program operation. As a disadvantage, processing may be slow.
To reduce the number of interfaces and express one function, one method needs to be called. The process of calling other specific classes is aggregated in the Facade role.
Combine the state change logic into one class. By using delegation, the processing of the target class is called according to the state change.
Effective for describing processing according to state changes. Register the role of each Observer (observer) in the role of Subject, and call the target method of Observer through delegation when the state in Subject changes.
It is used when you want to save the state of the instance. The Originator role creates a Memento role when he wants to save his state, and returns to the state previously saved in Memento at the discretion of the Caretaker role. At this time, the role of Memento widens the interface for the role of Originator (such as making the package the same) and narrows the interface for the role of Caretaker so that the encapsulation is not destroyed. (Reduce the methods that can be called.)
Manage state by class. By overloading, it is possible to describe processing based on the state without using conditional branching such as if statement.
Share your instance and don't waste new. When an instance is needed, it is created from the Factory class, and in the Factory class, if the instance already exists, that instance is used as it is. This saves memory and new time (constructor processing time).
When the Subject role is a class that performs heavy processing, a Proxy role is prepared as a substitute for it. The Proxy role and the Subject role share an interface, and the Proxy role performs processing as much as possible so that the Subject role is not used until the last minute. Like an HTTP proxy, the image is that in response to a request from a client, information is fetched to the server only when the cache does not exist.
Manage processing instructions as one instance. Command history management becomes possible. Specifically, prepare a class that describes detailed instructions and a class that saves and manages those instructions in java.util.Stack. Both use the same interface.
Use the Java language to create a simple language that suits your purpose. You can write regular expressions and batch processing languages.
Recommended Posts