Command Mode and Facade Mode and Iterator Mode

Command mode:

1. Normalize the interface, use the same method to call different objects and perform different tasks

   For example: open this operation, open the air conditioner, open the refrigerator, open the door, each operation has its own different place, we can do this

   **Define a Command interface

     public interface Command()

{

    public void open();

}

  

    **Concrete objects, implement the Command interface, and then execute specific methods

     public class Light implements Command

{

        public void open()

        { 

             light.on();

        }

}

 

   **Create a command sender

   public class SendCommand()

{

        public void executeOpen(Command command)

        {  

             command.open() //Pass in the light, execute the open method of the light, pass in the refrigerator, execute the open method of the refrigerator

        }

}

 

2. In the SendCommand() method, command is for interface programming, so any object that implements the Command interface can execute the open() method

 

Appearance Mode (Facade Mode)

1. Encapsulate complex behaviors in a method to simplify operations

   For example, when the TV is turned on, the user calls the openTV() method to complete

    In reality, there's a lot going on inside the TV

 

2. Design Principles: The Principle of Least Knowledge

   Don't let too many classes couple together, design interfaces to reduce coupling

 

iterator pattern

1. Encapsulate the traversal operation as an interface, providing a way to access the inside of the collection without exposing the details

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326146031&siteId=291194637