Python implements the Adapter mode instance code, pythonadapter

Source: Internet
Author: User

Python implements the Adapter mode instance code, pythonadapter

This article focuses on the implementation of the Adapter mode in python. The specific implementation code is as follows.

The Adapter mode can be implemented in two ways: class mode.

# Understanding # The principle of the power adapter. The incompatible interface class can work # This is the class implementation method # example # If an outlet class output foot is 3 feet, the lamp requires a two-pin socket, and now an Adapter is needed to implement the Adapter socket # Adaptee class socket (object): def Trigle (self ): print 'power supply' # target class tableLamp (object): def needTwo (self): pass # adapter class Adapter (tableLamp, socket): def needTwo (self): self. trigle () # client if _ name __= = '_ main _': lamp = Adapter () lamp. needTwo ()

Run

The second is the object method.

# This is the object implementation method class socket (object): def Trigle (self): print 'power supply over' # target class tableLamp (object): def needTwo (self ): pass # adapter class Adapter (tableLamp): def _ init _ (self, Socket): self. socket = Socket def needTwo (self): self. socket. trigle () # client if _ name __= = '_ main _': plug = socket () lamp = Adapter (plug) lamp. needTwo ()

Run

Summary

The above is all about the python Implementation of the Adapter mode instance code, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.