A fascinating story from Python (ii)

Source: Internet
Author: User

The book is connected to the back. After showing the top-level code of the app, let's look at how the modules are programmed.

In order to understand the code of each module, we need to lay the groundwork for the basic concepts and design specifications of the softchip architecture.

1, any module does not hold instances of other modules, naturally no longer explicitly use the handle to invoke any method.

This means that there is no coupling between the encoding period and the compile period between the modules.

2, each module in the form only with the machine interface, the interface in the API and Event two forms exist.

The API is mapped directly to the function entry address, which can be called directly by the user and has high performance during runtime. It is mainly used for utility properties and needs high-performance occasions.

The Event is also a synchronous call, but runs less than the API. Its key role is to logically cut the lengthy call chain. in traditional routines, a module in the completion of its own processing, often also hard to trigger other modules related to follow-up action, which is logically equivalent to thinking about "other people's things." The introduction of an Event is equivalent to a phase state in which the original long call chain is cut into one stage in time. Each module only needs to tell machine "x thing Y is finished",machinewill automatically find who need to "x things y finished" after the continuation. Machine is looking for a follow-up operation based on the target module in its own interface to do the event monitoring statement.

3, the module interface by self-declaration, including:

3-1) Outapi: The list of API names that you need to invoke.

3-2) InAPI: You can provide a list of API names for other module calls ( with corresponding function entry addresses ).

3-3) Inevent: A list of event names that you want to listen to ( with the corresponding event handler entry address ).

3-4) Outevent: Whether to send the event yourself.

Based on the declaration of each module, machine automatically "inserts" each module to complete the binding of the runtime API and the distribution of the event.

4, generally speaking, each module basically only need to care about " own thing ":

4-1) What API do I provide?

4-2) What API do I need to invoke?

4-3) What events do I monitor and handle?

4-4) What events do I have?

In turn, modules do not need to care about " other people's Things ":

4-5) who uses my API?

4-6) who provides the API for me to use?

4-7) who generated the event I was listening to? ( who was doing it before I worked?) )

4-8) The events I have generated who deal with it? (Who's going to carry on when I'm done? )

To give a simple example, in the Textchat design, Loginwindow login successful, do not need to explicitly notify Chatwindow, because that is Chatwindow thing. Loginwindow only need to tell machine "Loginwindow login succeeded". Machine will automatically find Chatwindow come out to work, because Chatwindow already told machine, if "Liginwindow login succeeded", please notify me.

OK, with the above cushion, the following code is easier to read, the basic routines are:

1. Each module inherits the Chip class.

2. Implement the _defineinterface() method to complete the interface declaration.

3. Implement the _poweron() method to receive "power-up" notifications from machine.

In addition, each module is completely free, love how to play on how to play it.

The new version of the module relationship reference.



From softchip import Chip
Class Controller (Chip): Def __init__ (self, Chipid): chip.__init__ (Self, chipid) def _defineinterf Ace (self): outapilist = [u ' connect_server ', U ' disconnect_server ',] inapilist = [(U ' api_start_work ', self.__apistartwork),] ineventlist = [(U ' evt_logout_done ', s Elf.__evtonlogoutdone),] Self.__interface = {Chip.TYPE_INT_API:inAPIList, chip.ty Pe_out_api:outapilist, Chip.TYPE_OUT_EVENT:None,} return self.__interface def _poweron (SE LF): Self._ehandler = self.__interface[chip. Type_out_event] Apimap = self._defineinterface[chip. TYPE_OUT_API] Self.__apiconnect = Apimap[u ' connect_server '] self.__apidisconnect = Apimap[u ' Disconnect_se        RVer '] def __apistartwork (self, ServerHost, serverport): succeeded = Self.__apiconnect (ServerHost, ServerPort) If not succeeded:sElf._ehandler (U ' evt_connect_failed ') return Self._ehandler (U ' evt_connect_succeeded ') def __evtonlogoutdon E (self): Self.__apidisconnect () class Communicationmanager (Chip): Def __init__ (self, chipid): Chip.__init_ _ (Self, chipid) def _defineinterface (self): inapilist = [(U ' connect_server ', self.__connect)            , (U ' disconnect_server ', self.__disconnect), (U ' log_in ', Self.__login), (U ' log_out ', self.__logout), (U ' invite_friend ', self.__invitefriend), (U ' send            _message ', self.__sendmessage),] Self.__interface = {Chip.TYPE_IN_API:inAPIList, Chip.TYPE_OUT_EVENT:None,} return self.__interface def _poweron (self): Self._ehandler = S  Elf.__interface[chip.type_out_event] def __connect (ServerHost, ServerPort): Pass def __disconnect (ServerHost, ServerPort): pasS def __login (username, password): Pass def __logout (): Pass def __invitefriend (friendname): ... self.__messagereceivethread () def __sendmessage (message): Pass Def __MESSAGERECEIVETHR EAD (): while (True): ... message = xxx self._ehandler (u ' evt_message_arrived ', message). ..        Class Loginwindow (Chip): Def __init__ (self, Chipid): chip.__init__ (Self, chipid) def _defineinterface (self): Outapilist = [u ' log_in ', U ' log_out ',] ineventlist = [(U ' Evt_conne        Ct_succeeded ', self.__evtonconnectsucceeded), (U ' evt_chat_closed ', self.__evtonchatclosed),            ] Self.__interface = {Chip.TYPE_OUT_API:outAPIList, Chip.TYPE_INT_EVENT:inEventList, Chip.TYPE_OUT_EVENT:None,} return self.__interface def _poweron (self): Self._ehandler = Self.__interface[chip.Type_out_event] Apimap = self._defineinterface[chip. TYPE_OUT_API] Self.__apilogin = Apimap[u ' log_in '] self.__apilogout = Apimap[u ' log_out '] def __evtonconne Ctsucceeded (self): self. Show () def __evtonchatclosed (self): Self.__apilogout () Self._ehandler (U ' evt_logout_done ') def __onlogi Nbuttonpressed (self): username = xxx. GetValue () password = xxx. GetValue () succeeded = Self.__apilogin (username, password) if not Succeeded:self._ehandler (U ' evt _login_failed ') Return Self._ehandler (U ' evt_login_succeeded ') self. Hide () class Chatwindow (Chip): Def __init__ (self, Chipid): chip.__init__ (Self, chipid) def _defineinterface (SE LF): Outapilist = [u ' invite_friend ', U ' send_message ', U ' log_out ', U ' di            Sconnect_server ',] ineventlist = [(U ' evt_login_succeeded ', self.__evtonloginsucceeded), (U ' EVt_message_arrived ', self.__evtonmessagereceived),] self.__interface = {Chip.TYPE_OUT_API:ou Tapilist, Chip.TYPE_IN_API:inEventList, Chip.TYPE_OUT_EVENT:None,} return self._ _interface def _poweron (self): Self._ehandler = self.__interface[chip. Type_out_event] Apimap = self._defineinterface[chip.         TYPE_OUT_API] Self.__apiinvite = Apimap[u ' invite_friend '] self.__apisendmsg = Apimap[u ' Send_message '] Self.__apilogout = Apimap[u ' log_out '] self.__apidisconnect = Apimap[u ' Disconnect_server '] def __evtO Nloginsucceeded (self): self. Show () def __evtonmessagereceived (self, message): XXX. Showtext (message) def __oninvitebuttonpressed (self): Friendname = xxx. GetValue () succeeded = Self.__apiinvite (Friendname, self.__onmessagereceived) if not succeeded:s Elf._ehandler (U ' evt_invite_failed ') return Self._ehandLer (U ' evt_invite_succeeded ') def __onsendbuttonpressed (self): message = XXX. GetValue () self.__apisendmsg (message) def __onwindowclosing (self): self._ehandle (U ' evt_chat_closed ')




In the next chapter of this series, we will briefly analyze the meaning of the softchip framework.

A fascinating story from Python (ii)

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.