This article describes the implementation of the food world recipe function based on the appearance mode of Java. Share it for your reference, as follows:
1. Pattern definition
Appearance mode is a software design mode commonly used by software engineers. It provides a unified high-level interface for a set of interfaces in the subsystem, making the subsystem easier to use. Appearance mode reads/writes data resources of each interface in the subsystem through an appearance interface, and customers can read the internal resource library through the appearance interface without interacting with the subsystem.
2. Examples of the model
1. Pattern analysis
We borrowed the recipe of making sweet and sour pork ribs from the Food World recipe to illustrate this pattern.
2. Appearance mode static class diagram
3. Code example
3.1 Create sweet and sour pork ribs interface-ISpareribs
package com.demo.common;/** * Sweet and Sour Ribs Interface* * @author * */public interface ISpareribs { // Prepare ingredients public void prepair(); // pickled pork ribs public void preserve(); // Fried pork ribs public void fry(); // Sauce-adjusted public void juice();}3.2 Sweet and Sour Ribs Realization Class-Spareribs
package com.demo.common;/** * * Sweet and sour pork ribs implementations * * @author * */public class Spareribs implements ISpareribs { // Prepare ingredients @Override public void prepair() { System.out.println("1. Prepare 500 grams of pork ribs, chopped onion, minced ginger, soy sauce, peanut oil, sugar, vinegar, cooking wine, and salt..."); } // Pickled pork ribs @Override public void preserve() { System.out.println("2. Wash the ribs and chop them into 3 cm long sections, blanch them in boiling water, take them out and put them in a basin, add salt and soy sauce to marinate them for flavor..."); } // Fried pork ribs @Override public void fry() { System.out.println("3. Add oil to fry until 60% heat, fry until light yellow, remove from the ribs; heat to 80% heat, then fry until golden brown, remove from the pot..."); } // Fry the juice @Override public void juice() { System.out .println("4. Leave a little oil in the wok, heat in the frying pan, add chopped green onion, stir-fry ginger and stir-fry, add appropriate amount of water, soy sauce, vinegar, sugar, cooking wine, pour in the ribs, boil and simmer over low heat until the soup is thick. The ribs are cooked, drizzle with cooked oil, and then release it!"); }}3.3 Create an appearance interface - ICookFacade
package com.demo.facade;/** * Make sweet and sour pork ribs in external mode* * @author * */public interface ICookFacade { // Make sweet and sour pork ribs public void cookSpareribs();}3.4 Appearance to realize CookFacade
package com.demo.facade;import com.demo.common.ISpareribs;import com.demo.common.Spareribs;/** * Make sweet and sour ribs in external mode* * @author * */public class CookFacade implements ICookFacade { // Sweet and sour ribs interface private final ISpareribs spareribs = new Spareribs(); // Make sweet and sour ribs public void cookSpareribs() { // Prepare ingredients this.spareribs.prepair(); // Pickled ribs this.spareribs.preserve(); // Fried pork ribs this.spareribs.fry(); // Sauce this.spareribs.juice(); }}3.5 Client Testing
package com.demo;import com.demo.facade.CookFacade;import com.demo.facade.ICookFacade;/** * Client application* * @author * */public class Client { public static void main(String[] args) { // Start making sweet and sour pork ribs! System.out.println("===== Start making sweet and sour pork ribs..."); // // Create sweet and sour pork ribs object instance// ISpareribs spareribs = new Spareribs(); // // Prepare ingredients// spareribs.prepair(); // // pickled pork ribs// spareribs.preserve(); // // Fried pork ribs// spareribs.fry(); // // Sauce// spareribs.juice(); ICookFacade cookFacade = new CookFacade(); cookFacade.cookSpareribs(); System.out.println("==== Sweet and sour pork ribs are finished! "); }}4. Operation results
==== Start making sweet and sour pork ribs...
1. Prepare 500 grams of pork ribs, appropriate amounts of chopped green onion, minced ginger, soy sauce, peanut oil, sugar, vinegar, cooking wine, and salt...
2. Wash the ribs and chop them into 3 cm long sections, blanch them in boiling water, remove them and put them in a basin, add salt and soy sauce to marinate them to taste...
3. Add oil to the wok and cook until 60% hot, fry the ribs until light yellow, remove them; heat them to 80% hot, then put them in the wok and fry until golden brown and remove them...
4. Leave a little oil in the wok and heat it, add chopped green onion, stir-fry the aroma of ginger, add appropriate amount of water, soy sauce, vinegar, sugar, cooking wine, and pour in the ribs. After boiling, simmer over low heat until the soup is thick. The ribs are cooked, drizzle with cooked oil and then set it off!
==== The sweet and sour pork ribs are finished!
3. The design principles of this model
1 Dimitte’s Law – the Minimum Knowledge Principle
2 Package Change Part
This figure further shows the relationship between client applications, appearance patterns and complex applications within subsystems.
4. Use occasions
1. A software system is relatively complex and requires a higher level of simple interface to simplify the operation of subsystems.
2. When there are too many dependencies between the user end and the implementation class, it is necessary to reduce the coupling between the user end and the subsystem or subsystem and increase the independence of the subsystem.
3. When subsystems are interdependent and need to hierarchical subsystems to simplify dependencies between subsystems, appearance mode can be used.
5. Appearance mode static class diagram
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.