Author: Tom brother wechat public account: Micro technology

In the face of complex business scenarios and ever-changing customer requirements, how to quickly implement the system with the minimum development cost, and ensure that the system has a low complexity, can ensure the continuous iteration ability of the system, so that the system has a high scalability.

These are the basic internal skills that a qualified architect must practice, but how to practice this magic??

Don’t worry. Take your time. Learned the real skill, took the offer of Ali, headline, goddess will be far away! ❤ ️ 💖 💘

Next, we will systematically summarize the design patterns that software architecture design needs to know, mainly extracting the essence, core design ideas, code examples, and application scenarios.

CRUD can be developed without understanding design patterns, but when developing and maintaining large software systems, it is very painful. If you understand what I am saying, you will understand what I am saying. If you do not understand what I am saying, you will not understand.

I will be commonly used software design patterns, do a summary, as follows:

Considering the large length of the content, in order to facilitate your reading, the software design pattern series (a total of 23) is divided into four articles. Each article explains the six design patterns, which are distinguished by different colors to facilitate your quick digestion and memory

This article is the first to explain the singleton pattern, builder pattern, abstract factory, factory method, prototype pattern, adapter pattern, a total of six design patterns.

Micro technology

Former Ali architect, graduate student, CSDN blog expert. Responsible for e-commerce transactions, community group buying, traffic marketing and other businesses. Share backend architecture skills, interview experience with first-line big factory, team management and other topics. Welcome to attention

91 original content

The public,

1. Singletons

Definition:

The Singleton pattern allows one and only one instance of a given class to exist. It provides a mechanism for any entity to access the instance.

Core ideas:

1️ guarantees that there is only one instance of a class. If the object has already been created, the existing object is returned. Why is it designed like this? This is because some business scenarios control access to shared resources, such as databases or files.

2️ provides a global access to the instance and a static access method.

Example code:

Public class Singleton {private static Singleton instance = new Singleton(); // Make the constructor private, Private Singleton() {} public static Singleton getInstance() {return instance; }}Copy the code

Add a private static member variable to the class to hold the singleton instance and declare a public static build method to get the singleton instance.

Notes:

Multiple business scenarios, multiple threads accessing global variables of the same class instance, frequent write operations, may cause thread safety issues. In addition, to prevent other objects from using the singleton class’s new operator, you need to code the default constructor to be private.

If you want to initialize objects with delay, you may have concurrency security issues when multiple threads initialize concurrently. Suppose thread A and thread B both block on the lock acquisition step, where thread A acquires the lock and instantiates the object —- to release the lock; Thread B then acquires the lock and instantiates the object, which violates the purpose of our singleton pattern.

How to solve it?

Double void check is adopted. First, it is safe and high performance can be maintained in the case of multi-threading. The first if judgment avoids the waste of performance caused by the contention lock of other useless threads. The second if judgment can intercept threads other than the first one to acquire the object lock.

/** * @author */ public class SingleonLock {private static SingleonLock doubleLock; Private SingleonLock() {} public static SingleonLock getInstance() {if (doubleLock == null) {synchronized (SingleonLock.class) { if (doubleLock == null) { doubleLock = new SingleonLock(); } } } return doubleLock; }}Copy the code

2. Builder mode

Definition:

Builder mode, also known as Builder mode.

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

Simply put, the builder pattern is how do you build an object with multiple components step by step, and the same build process can create different products

Core ideas:

role category instructions
Builder Interface or abstract class The abstract builder,Not required
ConcreteBuilder Concrete builders There can be multiple “because each build style may be different”,There must be
Product Ordinary class The object that is finally constructed,There must be
Director commander Unified command builders to build targets,Not required

Example code:

/** * @author */ public class Person {private String name; private int age; private String address; public static PersonBuilder builder() { return new PersonBuilder(); } private Person(PersonBuilder builder) { this.name = builder.name; this.age = builder.age; this.address = builder.address; } static class PersonBuilder {private String name; private int age; private String address; public PersonBuilder() { } public PersonBuilder name(String name) { this.name = name; return this; } public PersonBuilder age(int age) { this.age = age; return this; } public PersonBuilder address(String address) { this.address = address; return this; } public Person build() { return new Person(this); }}}Copy the code
  • Create a static inner class PersonBuilder in Person, and then copy the parameters from Person into the PersonBuilder class.

  • Create a private constructor in Person, taking the PersonBuilder type

  • Create a public constructor in the PersonBuilder

  • Create a set function in PersonBuilder that assigns values to the optional arguments in Person and returns an instance of type PersonBuilder

  • Create a build() method in the PersonBuilder that builds an instance of Person and returns it

/** * @author Public class PersonBuilderTest {public static void main(String[] args) {Person Person = person.builder () .name("Tom brother ").age(18).address(" Hangzhou ").build(); System.out.println(JSON.toJSONString(person)); }}Copy the code

The client uses chained calls to build the object step by step.

Application scenario:

  • The phased and step-by-step method is more suitable for multiple operation result classes to create scenarios. For example, the parameters for creating a class instance are not ready at one time, and some parameters may need to call multiple service operations to get them. In this case, we can create the class in advance according to the known parameters, and set the following parameters when they are ready.

  • A specific algorithm implementation that doesn’t care about a particular type of builder. For example, we don’t care about the code implementation of StringBuilder, only that it provides string concatenation.

Using the builder pattern makes it easier to instantiate objects on demand, avoid writing constructors with many different arguments, and overcome the disadvantage of writing only one constructor for a parameter of the same type.

Finally, in real projects, to simplify coding, you can often implement the Builder pattern of the class itself directly using Lombok’s @Builder annotation.

Abstract factory pattern

Definition:

The abstract factory pattern creates other factories around a Gigafactory, also known as factories of other factories. Is a creative design pattern that creates a set of related objects without specifying their concrete classes.

Key to the abstract factory pattern: how to find the right abstraction.

For the software caller, they are more concerned with what the software provides. As for how it’s done internally, they don’t care. In addition, the internal implementation details are often hidden for security reasons.

Take the production of TV, refrigerator, washing machine and other household appliances as an example, many manufacturers like Haier, Sony, Xiaomi, Hisense can produce the above appliances, but there are differences in appearance, performance, power, intelligence, features and other functions. Faced with such requirements, how can we implement coding with the help of abstract factory patterns?

The abstract factory pattern is defined as an abstract factory class, which is inherited by several different concrete factories, and then each implements the same abstract function to achieve polymorphism in the code.

Example code:

/** * @author */ public abstract class AbstractFactory {// createTV abstract Object createTV(); // Production of washing machine; // Abstract Object createRefrigerator(); } public class HaierFactory extends AbstractFactory { @Override Object createTV() { return null; } @Override Object createWasher() { return null; } @Override Object createRefrigerator() { return null; } } public class XiaomiFactory extends AbstractFactory { @Override Object createTV() { return null; } @Override Object createWasher() { return null; } @Override Object createRefrigerator() { return null; }}Copy the code

AbstractFactory is an AbstractFactory class that can create abstract products such as TVS, washing machines, and refrigerators. HaierFactory and XiaomiFactory are specific factories that produce specific products. When we want to produce a concrete product, we just need to tell AbstractFactory.

Solve the problem:

  • When different product families have more common features, the abstract factory pattern can be used to improve the reusability of components.

  • Separating the creation and use of objects is an effective way to unify code to a single level when it comes to making code more scalable and less expensive to maintain.

Application scenario:

  • Solve cross-platform compatibility issues. When an application needs to support multiple operating systems such as Windows, Mac, and Linux.

  • The commodity, order and logistics systems of e-commerce need to be differentiated according to regional policies and users’ buying habits

  • Different database products, JDBC is for the database to add, delete, change and check the establishment of the abstract factory class, no matter what type of database, as long as the specific database components can support JDBC, you can read and write the database operation.

4. Factory method mode

The factory method pattern is similar to the abstract factory pattern. The factory method pattern is more common in projects because it is used for object creation and use around only one type of interface.

Definition ** : **

Define an interface for creating an object and let its subclasses decide which class to instantiate. The factory pattern delays creation to the subclasses.

Core point: Encapsulate the object creation process and improve the reusability of the methods used to create objects.

The factory method pattern contains three key roles: abstract product, concrete product, and factory class.

Define an abstract product interface ITV, HaierTV and XiaomiTV are concrete product classes, TVFactory is the factory class, responsible for producing concrete object instances.

Example code:

/** * @author */ public interface ITV {// describe Object desc(); } public class HaierTV implements ITV {@override public Object desc() {return "implements ITV "; }} public class implements ITV {@override public Object desc() {return "Implements ITV "; } } public class TVFactory { public static ITV getTV(String name) { switch (name) { case "haier": return new HaierTV(); case "xiaomi": return new XiaomiTV(); default: return null; } } } public class Client { public static void main(String[] args) { ITV tv = TVFactory.getTV("xiaomi"); Object result = tv.desc(); System.out.println(result); }}Copy the code

The factory method pattern encapsulates the creation of an object around a specific abstract product (interface), and the Client only needs to create a concrete object instance through the factory class and then use its functionality.

The factory method pattern separates the creation and use of objects, reducing code coupling.

5. Prototype mode

The prototype pattern is a type of creation pattern that is characterized by “copying” an existing instance to return a new instance, rather than creating a new instance. The copied instance is what we call a “prototype,” which is customizable.

Definition:

Use stereotype instances to specify the types of objects to create, and then create new objects by copying those stereotypes.

Example code:

/** * @author Micro technology * / public interface Prototype extends Cloneable {public Prototype clone () throws CloneNotSupportedException; } public class APrototype implements Prototype { @Override public Prototype clone() throws CloneNotSupportedException { System.out.println(" Start cloning microtech objects "); return (APrototype) super.clone(); } } public class Client { @SneakyThrows public static void main(String[] args) { Prototype a = new APrototype(); Prototype b = a.clone(); System.out.println("a object reference: "+ a); System.out.println("b object reference: "+ b); }}Copy the code

Execution result:

Start cloning microtechnology object A object reference: course.p14.p5.APrototype@7cc355be object reference: course.p14.p5.APrototype@6e8cf4c6Copy the code

Print out the addresses of two objects and find that they are not identical in memory for two objects.

The Cloneable interface itself is an empty method, and the clone() method called is actually the object.Clone () method

Advantages:

  • Excellent performance. Instead of reinitializing the object, the runtime state of the object is dynamically obtained.

  • You can get rid of the constructor constraint.

Special attention:

Clone () is a shallow copy, that is, the basic type of data, and will give you a new copy. But reference types (objects within objects) are not copied. Reference types such as bean instance reference, collection and other reference types.

How to solve it?

You need to get the shallow copy object after super.Clone (), and then manually reconstruct the object and assign values to the global variables in it. And, of course, when you go through this process, you get what we call a deep copy.

Application scenario:

  • Deserialization, such as fastJSON’s json.parseObject (), turns strings into objects

  • Creating a new object consumes a lot of resources

  • There are so many properties in an object that it is painful to copy and paste objects created by get and set methods

Snacks:

Spring framework provides a tool class, BeanUtils. CopyProperties can easily complete the copy of object properties, in fact, is also shallow copy, only the basic type of data, object reference copy. When using, pay special attention to, if the global variable has an object type, prototype object and cloned object will be modified twice, special treatment, using deep copy, otherwise it will cause security problems.

6. Adapter mode

We all know that the voltage in the United States is 110V, while the voltage in China is 220V. If you travel to the United States, you must remember to bring a power adapter to convert the power current standard used in different countries into the standard suitable for our own appliances, otherwise it is easy to burn out the electronic equipment.

Definition:

By transforming the interface of a class into another interface that the customer expects, an adapter can make two incompatible classes work together. It’s all about conversion!

Core ideas:

Wrapping a new adapter layer around an existing interface or class gives the effect of extending the object structure indefinitely.

  • Adaptee: indicates the source interface that needs to be adapted

  • Target: indicates the Target interface that is exposed

  • Adapter: ADAPTS the source interface to the target interface

Application scenario:

  • If the original interface cannot be modified, some new functions must be quickly compatible

  • When the external system needs to be relied on, a separate anticorrosion layer is generally encapsulated to reduce the impact of unexpected risks from the external system

  • Suitable for different data formats, different interface protocol conversion

  • The old interface was upgraded

Case study:

For example, to check logistics information, because the systems of logistics companies are independent, there are great differences in programming languages and interaction modes, it is necessary to do separate adaptation for different logistics companies. At the same time, different response timeout times are configured according to the system performance of different companies

The adaptor pattern is called “best to patch mode” because it can be used to fit any interface.

Write in the last

Design patterns have been studied by many people, but they are always dizzy when the project is practical. The reason is that they do not understand what the core is and what the underlying logic is. “Design Patterns: The Foundation of Reusable Object-oriented” has talked about.

Think about what should change in your design, and encapsulate the concept of what will change.

The essence of software architecture: Find change, encapsulate change.

Businesses are constantly changing, there is no fixed coding answer, and do not hardwire design patterns. No matter which design pattern is selected, it should try to meet the SOLID principle and whether the self-review meets the continuous extensibility of the business. As the saying goes, “it doesn’t matter whether a cat is white or black, as long as it can catch mice.”


About me: Former Alibaba architect, patent, competition award, CSDN blog expert, responsible for e-commerce transactions, community fresh, marketing, finance and other businesses, many years of team management experience, love thinking, like to make friends

Recommended reading

How many pieces of data can a B+ tree store in mysql?

Learn these 10 design principles and be an architect!!

How to design the Redis cache for the 100mb system??

[High concurrency, High performance, high availability] system design experience

Everyone is an architect?? It’s not easy!!

[10,000 level concurrent] How to design e-commerce inventory deduction? Not oversold!