C language and design mode (adapter mode) 14

Source: Internet
Author: User

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

In our daily life, we are inseparable from various electronic tools. Any laptop, mobile phone, or MP4 cannot be charged. The charger is needed for charging. In fact, basically, the charger is a common adapter. What is an adapter? It is to program the AC voltage of 220 V and 50Hz 5 ~ 12 v dc voltage. The charger did this.
So how should we use C ++ to describe such a charging adapter?

[CPP] View plaincopyprint?
  1. ClassVoltage_12v
  2. {
  3. Public:
  4. Voltage_12v (){}
  5. Virtual~ Voltage_12v (){}
  6. Virtual VoidRequest (){}
  7. };
  8. ClassV220_to_v12
  9. {
  10. Public:
  11. V220_to_v12 (){}
  12. ~ V220_to_v12 (){}
  13. VoidVoltage_transform_process (){}
  14. };
  15. ClassAdapter:PublicVoltage_12v
  16. {
  17. V220_to_v12 * padaptee;
  18. Public:
  19. Adapter (){}
  20. ~ Adapter (){}
  21. VoidRequest ()
  22. {
  23. Padaptee-> voltage_transform_process ();
  24. }
  25. };

class voltage_12v <br/>{< br/> public: <br/> voltage_12 V () {}< br/> virtual ~ Voltage_12v () {}< br/> virtual void request () {}< br/> }; </P> <p> class v220_to_v12 <br/>{< br/> Public: <br/> v220_to_v12 () {}< br/> ~ V220_to_v12 () {}< br/> void voltage_transform_process () {}< br/>}; </P> <p> class adapter: public voltage_12v <br/>{< br/> v220_to_v12 * padaptee; </P> <p> Public: <br/> adapter () {}< br/> ~ Adapter () {}</P> <p> void request () <br/>{< br/> padaptee-> voltage_transform_process (); <br/>}< br/>};

Through the aboveCodeWe can understand it in this way. Voltage_12v indicates that our ultimate goal is to obtain a 12 v dc voltage. Of course, there are many ways to get 12 V. Using adapter conversion is only one of them. Adapter indicates the adapter, which cannot convert v to 12 V by itself. Therefore, you need to call the Conversion Function of v220_to_v12 class. Therefore, the process of getting 12 V using the adapter is actually the process of calling the v220_to_v12 function.

However, since our topic isC LanguageTo write the adapter mode, we need to achieve the initial goal. This is actually not difficult. The key step is to define the data structure of an adapter. Then all the adapter work is done by adaptee, which is so simple. I don't know if I have explained it?

[CPP] View plaincopyprint?
  1. TypdefStruct_ Adaptee
  2. {
  3. Void(* Real_process )(Struct_ Adaptee * padaptee );
  4. } Adaptee;
  5. Typedef Struct_ Adapter
  6. {
  7. Void* Padaptee;
  8. Void(* Transform_process )(Struct_ Adapter * padapter );
  9. } Adapter;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.