-- [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!