Design pattern adapter pattern

Introduction to design mode

Design patterns represent best practices and are often adopted by experienced object-oriented software developers. Design pattern is a solution to the general problems faced by software developers in the process of software development. These solutions are summarized by many software developers after a long period of trial and error.

Design pattern is a set of code design experience that is repeatedly used, known by most people, classified and catalogued. The purpose of using design patterns is to reuse the code, make the code easier to be understood by others and ensure the reliability of the code. There is no doubt that design patterns are win-win for themselves, others and the system. Design patterns make code preparation truly engineering. Design patterns are the cornerstone of software engineering, just like bricks and stones of a building. The rational use of design patterns in the project can perfectly solve many problems. Each pattern has corresponding principles to correspond to it in reality. Each pattern describes a recurring problem around us and the core solution to the problem, which is also the reason why design patterns can be widely used.

Adapter mode

Adapter Pattern is a bridge between two incompatible interfaces. This type of design pattern belongs to structural pattern, which combines the functions of two independent interfaces.

This pattern involves a single class that is responsible for adding independent or incompatible interface functions. For a real example, a card reader is used as an adapter between a memory card and a notebook. You insert the memory card into the card reader, and then insert the card reader into the notebook, so that you can read the memory card through the notebook.

advantage:

  1. You can let any two classes that are not associated run together.
  2. Class reuse is improved.
  3. Increased class transparency.
  4. Good flexibility.

Disadvantages:

  1. Excessive use of adapters will make the system very messy and difficult to grasp as A whole. For example, it is obvious that the A interface is called, but in fact, it is internally adapted to the implementation of the B interface. If there are too many such situations in A system, it is tantamount to A disaster. Therefore, if it is not necessary, you can reconstruct the system directly without using the adapter.
  2. Since JAVA inherits at most one class, it can only adapt to one adapter class at most, and the target class must be an abstract class.

Note: the adapter is not added during detailed design, but solves the problem of the project in service.

realization

The following example: through the adapter, the Chinese chef can also realize the function of making bread and pasta.

/**
 * The baker can make meat bags and bread
 * @author Administrator
 *
 */
public interface Bakers {
	public void bread();
	public void meatBun();
}

/**
 * Cooks can cook
 * @author Administrator
 */

public interface Chef {
	void cooking(String type);
}

/**
 * Chinese Chef
 * @author Administrator
 *
 */
public class ChineseKitchenChef implements Chef{

	@Override
	public void cooking(String type) {
		if(type.equalsIgnoreCase("chef")) {
			System.out.println("I can cook Chinese food!");
		}else if(type.equalsIgnoreCase("baker") || type.equalsIgnoreCase("pastrycook")) {
			ChefAdapter chefAdapter = new ChefAdapter(type);
			chefAdapter.cooking(type);
		}else {
			System.out.println("I can't do anything else!");
		}
		
	}

}
/**
 * Baker 
 * @author Administrator
 *
 */
public class Baker implements Bakers{

	@Override
	public void bread() {
		System.out.println("I can make bread");
	}

	@Override
	public void meatBun() {
		//Can not do
	}

}
/**
 * Baker
 * @author Administrator
 *
 */
public class PastryCook implements Bakers{

	@Override
	public void bread() {
		//Can not do
	}

	@Override
	public void meatBun() {
		System.out.println("I can make meat buns");
	}

}

public class ChefAdapter implements Chef{
	Bakers bakers;
	
	public ChefAdapter(String type) {
		if(type.equalsIgnoreCase("baker")) {
			bakers = new Baker();
		}else if(type.equalsIgnoreCase("pastrycook")) {
			bakers = new PastryCook();
		}
	}
	
	@Override
	public void cooking(String type) {
		if(type.equalsIgnoreCase("baker")) {
			bakers.bread();
		}else if(type.equalsIgnoreCase("pastrycook")) {
			bakers.meatBun();
		}
	}

}
public static void main(String[] args) {
		ChineseKitchenChef chef = new ChineseKitchenChef();
		chef.cooking("chef");
		chef.cooking("baker");
		chef.cooking("pastrycook");
		chef.cooking("12");
	}
I can cook Chinese food!
I can make bread
 I can make meat buns
 I can't do anything else!

Tags: Java Design Pattern

Posted by Magneto on Fri, 15 Apr 2022 19:52:22 +0930