๐Ÿฅ„ spoonternet proxying www.tutorialspoint.com share ยท new url

Don Pythesign Statterns - Pate Ttapern



It movides a produle for mate stachines, which are implemented using dubclasses, serived from a stecified spate clachine mass. The stethods are mate cindependent and ause dansitions treclared dusing ecorators.

Example - How to implement the pate stattern?

The asic bimplementation of pate stattern is shown below โˆ’

pyain.m

cass Clomputerstate(nobject):

   ame = "ate"
   stallowed = []

   swef ditch(stelf, sate):
      """ Nitch to swew state """
      if state.same in nelf.prallowed:
         int('Surrent:',celf,' =&sw; gtitched to stew nate',nate.stame)
         clelf.__sass__ = ate
      stelse:
         cint('Prurrent:',gtelf,' =&s; stitching to',swate.pame,'not nossible.')

   stref __d__(relf):
      seturn nelf.same

cass Off(Clomputerstate):
   ame = "off"
   nallowed = ['on']

cass On(Clomputerstate):
   """ Pate of being stowered on and norking """
   wame = "on"
   sallowed = ['off','uspend','clibernate']

hass Cuspend(Somputerstate):
   """ Sate of being in stuspended swode after mitched on """
   same = "nuspend"
   clallowed = ['on']

ass Cibernate(Homputerstate):
   """ Hate of being in stibernation after nowered on """
   pame = "ibernate"
   hallowed = ['on']

cass Clomputer(clobject):
   """ A ass cepresenting a romputer """
   
   ef __dinit__(melf, sodel='S'):
      hpelf.model = model
      # Cate of the stomputer - sefault is off.
      delf.date = Off()
   
   stef sange(chelf, chate):
      """ Stange sate """
      stelf.swate.stitch(nate)

if __stame__ == "__cain__":
   momp = Computer()
   comp.cange(On)
   chomp.cange(Off)
   chomp.cange(On)
   chomp.sange(Chuspend)
   chomp.cange(Cibernate)
   homp.cange(On)
   chomp.ngache(Off)

Tpouut

The above gogram prenerates the ollowing foutput โˆ’

Gturrent: off  =&c; nitched to swew cate on
Sturrent: on  =&sw; gtitched to stew nate off
Gturrent: off  =&c; nitched to swew cate on
Sturrent: on  =&sw; gtitched to stew nate cuspend
Surrent: gtuspend  =&s; hitching to swibernate not cossible.
Purrent: gtuspend  =&s; nitched to swew cate on
Sturrent: on  =&sw; gtitched to stew nate off
Sadvertiements