Adapter mode lua implementation

Source: Internet
Author: User

-- [Convert an interface of a class to another interface that the customer wants. The Adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together. -- Gang of Four Application: for example, when a meeting is held in the United Nations, the leaders of each country speak their own language, but receive people from different countries. Therefore, the leaders of different countries need to translate the information, this translation is the adapter. For example, if we play games, especially games on different platforms, we need a simulator, which is an adapter. ] -- The following assumes that the two are in a meeting.

Target = {} function Target: new (o) o = o or {} setretriable (o, self) self. _ index = self return o; end function Target: Listen (content) print (content) end Adaptee = {} function Adaptee: new (o) o = o or {} setretriable (o, self) self. _ index = self return o; end function Adaptee: Translate (content) -- Here return "Are you happy? "End Adapter = Target: new () function Adapter: new (o) o = o or {} setretriable (o, self) self. _ index = self o. adaptee = Adaptee: new () return o; end function Adapter: Listen (content) print (self. adaptee: Translate (content) end

 

-- Xi zhiping said c = "are you happy? "Aobama = Adapter: new () -- what my parents actually hear is aobama: Listen (c). Output: Are you happy? Chat group: 315249378 if any of them is incorrect, please contact us for discussion!

Related Article

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.