当前位置:网站首页>Adapter mode -- can you talk well?

Adapter mode -- can you talk well?

2022-06-11 11:13:00 zhanyd

Generation gap

Students from small to large , I must be used to listening to my parents' nagging , Our ears are all cocooned , But they still keep talking in their ears every day .

Like a word , Say it ten thousand times , Will become the truth .

Maybe this is the generation gap , Let's see what our parents say first .

public interface Communication {
    

    /** *  About work  */
    public void aboutWork();

    /** *  About eating  */
    public void aboutEate();

    /** *  About sleeping  */
    public void aboutSleep();
}
public class NormalCommunication implements Communication{
    

    /** *  About work  */
    @Override
    public void aboutWork() {
    
        System.out.println(" About work : Work hard , Don't spend money , Save more money .");
    }

    /** *  About eating  */
    @Override
    public void aboutEate() {
    
        System.out.println(" About junk food : Don't eat takeout every day , Food taken late at night , Take care of your body , Pay attention to healthy diet .");
    }

    /** *  About sleeping  */
    @Override
    public void aboutSleep() {
    
        System.out.println(" About sleeping : Don't stay up late , Go to bed early .");
    }
}
public class Test {
    

    public static void main(String[] args) {
    
        System.out.println(" Paternalistic nagging ******");
        NormalCommunication normalCommunication = new NormalCommunication();
        normalCommunication.aboutEate();
        normalCommunication.aboutWork();
        normalCommunication.aboutSleep();
    }
}
 Paternalistic nagging ******
 About junk food : Don't eat takeout every day , Food taken late at night , Take care of your body , Pay attention to healthy diet .
 About work : Work hard , Don't spend money , Save more money .
 About sleeping : Don't stay up late , Go to bed early .

A new way

Time passed quickly , Time flies like an arrow , Twinkling of an eye , We have become parents ourselves , You have to treat your own children ” To nag “, No , communicate .

We must speak better than our own parents did , Be efficient , Have momentum .

I thought about it for a long time , I think I should say so :

public class EffectiveCommunication {
    

    /** *  Work better  */
    public void betterWork() {
    
        System.out.println(" About work : No money , Also want to have a girlfriend / Boy friend ? I want to be a single dog all my life ?");
    }

    /** *  Eat better  */
    public void betterEate() {
    
        System.out.println(" About junk food : All fat into a pig , And eat ?");
    }

    /** *  Better sleep  */
    public void betterSleep() {
    
        System.out.println(" About sleeping : Early risers are more handsome / More beautiful .");
    }
}

Although this set of words is perfect , It fully reflects the wisdom of life and communication skills .

however ,EffectiveCommunication Methods in classes and Communication Interface does not match , Traditional virtues cannot be lost , Our new way of communication should be compatible with the previous way of communication .

Adapter pattern

The most common adapter in our life is the power conversion plug , When the socket and plug do not match , You need to add a conversion plug , This adapter plug is the adapter .
 Insert picture description here

At this point, we will ask for the adapter mode .

Adapter pattern (Adapter Pattern) : Convert one interface to another that the customer wants , The adapter pattern enables classes that are not compatible with the interface to work together , It is alias wrapper (Wrapper).

 picture source :https://design-patterns.readthedocs.io/zh_CN/latest/structural_patterns/adapter.html
Let's find a communication adapter , Make the new communication mode match the old communication interface .

public class CommunicationAdpter implements Communication{
    

    private EffectiveCommunication effectiveCommunication;

    public CommunicationAdpter(EffectiveCommunication effectiveCommunication) {
    
        this.effectiveCommunication = effectiveCommunication;
    }

    /** *  About work  */
    @Override
    public void aboutWork() {
    
        effectiveCommunication.betterWork();
    }

    /** *  About eating  */
    @Override
    public void aboutEate() {
    
        effectiveCommunication.betterEate();
    }

    /** *  About sleeping  */
    @Override
    public void aboutSleep() {
    
        effectiveCommunication.betterSleep();
    }
}
public class Test {
    

    public static void main(String[] args) {
    
        System.out.println(" Communication with young people ******");
        CommunicationAdpter communicationAdpter = new CommunicationAdpter(new EffectiveCommunication());
        communicationAdpter.aboutEate();
        communicationAdpter.aboutWork();
        communicationAdpter.aboutSleep();
    }
}

 Communication with young people ******
 About junk food : All fat into a pig , And eat ?
 About work : No money , Also want to have a girlfriend / Boy friend ? I want to be a single dog all my life ?
 About sleeping : Early risers are more handsome / More beautiful .

Although still according to the previous routine , But through the adapter , Said something completely different , It really conforms to the trend of the times .

summary

Communication The interface corresponds to... In the class diagram Traget class ,CommunicationAdpter In the class diagram Adpter,EffectiveCommunication In the class diagram Adptee.

The client and Adptee Are not compatible , Can't call directly , But with adapters Adpter, The client just calls Adpter,Adpter By calling Adptee Object to complete the task , In this way, the two incompatible systems can cooperate .

Advantages of adapter mode :

  • Decouple the target class from the adapter class , Reuse existing adapter classes by introducing an adapter class , Without modifying the original code .
  • Increased class transparency and reusability , Encapsulate the concrete implementation in the adapter class , It's transparent to client classes , And it improves the reusability of the adapter .

shortcoming :

  • Excessive use of adapters , It will make the system chaotic , such as , Obviously, what we call is A Interface , In fact, the interior is adapted into B Interface implementation , Sometimes it can cause confusion .

Application :

  • The system needs to use existing classes , The interfaces of these classes do not meet our requirements , It is not convenient for us to modify the code .

Generally speaking , The adapter pattern can be seen as a kind of “ Compensation mode ”, To remedy design defects , Or we can't change the system code , Applying this model is “ Helpless move ”.

So we can't rely too much on the adapter pattern .

To adapt to the changes of the times , Still have to rely on their own continuous learning and hard work .

Code link






 Insert picture description here

原网站

版权声明
本文为[zhanyd]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111054046024.html