1 What is a design pattern?A design pattern is a reusable solution to a software engineering problem. Unlike solutions to many specific programs, design patterns can be applied to many different programs. A design pattern is not a finished product, but a template that can be applied to multiple situations and improved over time. It is a very powerful software engineering tool. Using proven prototypes can increase development speed, and developers who use design pattern templates can improve coding efficiency and the readability of the final product. 2 Pattern #1 đź‘Ś SingletonThe singleton pattern is a class that allows the creation of a unique instance and access to that instance. It contains a private static variable that can hold the only instance of the class. The singleton pattern is often useful when you need to limit the instantiation of a class to one object. The singleton pattern is usually used when you need to coordinate a single object operating in a system. 3 Properties of Singleton Class
4 Rules for making singleton classesFollow these rules to create a singleton class:
5 Singleton ExampleFollowing is an example of Singleton class in Java: Following is an example of a Singleton class in Kotlin: 6 Pattern #2 👌 Factory PatternThe factory pattern is a design pattern for creating objects that gets its name from its factory-like behavior. In the factory pattern, the factory class is responsible for controlling the logic of instantiating an object. The factory pattern is useful when you need to create multiple objects with similar behavior. You can use the factory pattern to create objects without specifying a specific class. This makes the code more flexible and easier to modify and maintain. Please see the following code for better understanding: 7 Pattern #3 👌 Builder PatternThe Builder pattern aims to “decouple the construction of a complex object from its representation so that the same construction process can create different representations.” It is used to build a complex object step by step and the last step will return the object. 8 Rules for making Builder classesThe following rules are followed when making a Builder class:
Following is an example of a Builder class in Kotlin: 9. Pattern #4 đź‘Ś Appearance PatternThe Facade pattern provides a higher-level interface that makes a set of other interfaces easier to use. It encapsulates the complexity of a set of classes and provides a higher-level interface to simplify access to those classes. The following diagram illustrates this idea more clearly. Retrofit by Square is an open source Android library that helps you implement the facade pattern. You can create an interface to provide API data to the client. 10 Pattern #5 đź‘Ś Dependency InjectionDependency injection is like moving into a fully furnished apartment; everything you need is already there, without having to wait for the furniture to be delivered or follow the furniture company's instruction guide to assemble it. In software terms, dependency injection requires that you provide any required objects to instantiate a new object. This new object does not need to construct or customize the object itself. In Android, you may find that you need to access the same complex object from various points in your application, such as a network client, an image loader, or The following example shows the code for a This approach is problematic because What does the code look like using dependency injection? Instead of each 11 Pattern #6 đź‘Ś Adapter PatternThe adapter pattern is a bridge pattern used to connect two incompatible interfaces. This pattern involves a single class that is responsible for connecting the functionality of separate or incompatible interfaces. A real-life example might be a card reader that acts as an adapter between a memory card and a laptop. You insert the memory card into the card reader and the card reader into the laptop so that the memory card can be read by the laptop. |
Compiled by: Gong Zixin We know that genetics and...
Recently, Foxconn and Fisker jointly announced th...
We also need to arrange a good-looking layout How...
Nowadays, short videos are in a very fierce compe...
Your browser does not support the video tag In tr...
On June 4, 2020, when Amazon SageMaker machine le...
Content at a glance: Extreme weather such as thun...
The performance of smart phones is becoming more ...
Marketing status: The advertiser is a local O2O d...
Operations is a very complex business system. Gen...
A friend of Huazi has been having headaches recen...
The video account operates the book list project,...
What is Juliang Qianchuan? Bytedance Qianchuan is...
The secret to successful sales is to skillfully l...
Audit expert: Li Weiyang Well-known science write...