[Design Mode]-Appearance Mode

1. Introduction to Appearance Mode

1. Introduction to Appearance Mode

Appearance mode (facade pattern) also called facade pattern, is a form of structural design patterns, it is proposed mainly to hide the complexity of the system. = The system that implements the appearance mode does not need to know the complicated internal connections of the system. The system only needs to provide an interface for external reception.

2. Usage scenarios of appearance mode

The appearance mode is usually used in three scenarios:

  1. System reconstruction is difficult. At this time, existing code can be reused through appearance mode to hide implementation details.
  2. The system is relatively independent.
  3. Prevent the risk of the caller being unfamiliar with the interface logic

Second, speak mode through business

1. Provide a business scenario

Everyone should be no stranger to stock trading, but because they are afraid of becoming a member of thousands of leeks, many people are discouraged from stock trading. So why do ordinary people think they will become leeks? The main reason is that the logic behind the stock is very complicated. We need to understand the market's first-time intelligence and familiarize with the industry background. This is undoubtedly a headache for many people with limited energy. Therefore, in order to solve this problem, many financial companies have launched their own funds, and through professional financial managers, those of us who do not understand stocks can make investments, so that we only need to buy our favorable funds to achieve the same as investing in stocks. The effect of this is actually a very typical facade mode (appearance mode). In order to write today’s Demo, I went to Alipay to invest a huge sum of money and bought two good funds:

Insert picture description here
So let's briefly analyze what is the business process of buying funds on Alipay?

2. Business Process Analysis

Since bloggers are also novices of fund investment, let's briefly summarize this investment process. There may be errors in the middle, but it will not affect our talk about the appearance mode, no wonder!
Insert picture description here

  1. The user opened the Alipay fund page, selected funds, and finally selected two funds issued by China Merchants Bank and China Europe.
  2. Alipay receives users' requests to purchase funds and distributes the requests to specific fund institutions.
  3. The fund institution completes the processing of the purchase request and returns the result to Alipay
  4. Alipay will feedback the results to users

Below we use code to gradually achieve

Third, the realization of the appearance model

1. Code Implementation

(1) Create fund products

Fund abstract class

public abstract class Fund {
    
    
	// 开户
	public abstract void openAccount();

	// 认购
	public abstract void subscription();

	// 确认
	public abstract void affrim();

}

China Merchants Bank Fund

public class ChinaMerchantsFund extends Fund {
    
    

	@Override
	public void openAccount() {
    
    
		System.out.println("招商银行基金账户开户成功");

	}

	@Override
	public void subscription() {
    
    
		System.out.println("招商银行基金认购成功");

	}

	@Override
	public void affrim() {
    
    
		System.out.println("招商银行基金确认成功");

	}

}

In order to add some differences here, we have added a process to the China Europe Fund to evaluate user information.
China Europe Fund

public class CentralEuropeFund extends Fund {
    
    
	@Override
	public void openAccount() {
    
    
		System.out.println("中欧国际基金账户开户成功");

	}

	@Override
	public void subscription() {
    
    
		System.out.println("中欧国际基金认购成功");

	}

	@Override
	public void affrim() {
    
    
		System.out.println("中欧国际基金确认成功");

	}

	public void check() {
    
    
		System.out.println("用户信息校验成功");
	}

}

(2) Create appearance class

public class FundFacade {
    
    

	private ChinaMerchantsFund chinaMerchantsFund;
	private CentralEuropeFund centralEuropeFund;

	public final static String CHINA_MERCHANT = "chinaMerchantsFund";
	public final static String CENTRAL_EUROPE = "centralEuropeFund";

	public void buyFund(String str) {
    
    

		if (CHINA_MERCHANT.equals(str)) {
    
    
			chinaMerchantsFund = new ChinaMerchantsFund();
			chinaMerchantsFund.openAccount();
			chinaMerchantsFund.subscription();
			chinaMerchantsFund.affrim();

		} else if (CENTRAL_EUROPE.equals(str)) {
    
    
			centralEuropeFund = new CentralEuropeFund();
			centralEuropeFund.openAccount();
			centralEuropeFund.check();
			centralEuropeFund.subscription();
			centralEuropeFund.affrim();
		}
	}

}

(3) Test and test results

public class AliPay {
    
    
	public static void main(String[] args) {
    
    
		FundFacade fundFacade = new FundFacade();
		fundFacade.buyFund(FundFacade.CHINA_MERCHANT);
	}
}

Test result:
Insert picture description here
Here we observe that the realization of a simple appearance model has been completed.

2. Implementation analysis

We can simply divide the above code into three components:

  1. The first part is the subsystem responsible for the implementation of all the details . The subsystem can be an object or a system. What they have to do is deal with the specific details of the business.
  2. The second part is responsible for maintaining the appearance classes of all subsystems , from appearance classes to the implementation details of all subsystems, and for unified scheduling and maintenance of the subsystems.
  3. The third part is the business caller . Some people are also used to calling it the client. What the client has to do is to implement the business logic by calling the appearance class.

The relationship between them is as follows:
Insert picture description here

Fourth, the characteristics of the appearance model

1. Usage scenarios of appearance mode

Appearance classes are actually used in many daily applications. For example, the three-tier architecture pattern we most commonly used in JAVAEE development is a typical appearance pattern design. Of course, sometimes when we are doing some tools, it is actually a typical appearance mode application.

2. Advantages and disadvantages of appearance mode

advantage:

  1. Reduced interdependence between systems
  2. Improved system flexibility
  3. Improved system security

Disadvantages:

  1. Do not comply with the opening and closing principles, you may need to modify the code when expanding functions

Okay, this concludes today's content. If you have any questions, you can privately message me or leave a message in the comment area, and I will answer you as soon as possible. Friends who feel rewarded, please remember one-click three consecutive times, pay attention to bloggers, don’t get lost, and refuse to be a prostitute~

Guess you like

Origin blog.csdn.net/xiaoai1994/article/details/113103710