Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Abstract Factory pattern of python Design pattern

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to implement the abstract factory pattern of python design pattern". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to implement the abstract factory pattern of python design pattern.

The code for the implementation is as follows:

Class FruitClass: # breed factory def get_name (self Name_index): if name_index = = 0: name_object = OrangeClass () elif name_index = = 1: name_object = Hami_MelonClass () elif name_index = = 2: name_object = GrapeClass () else: name_object = None return name_objectclass OrangeClass: # Orange def _ _ init__ (self): self.name = "orange" def print_name (self): print ("the fruit you buy is:% s"% self.name) class Hami_MelonClass: # cantaloupe def _ init__ (self): self.name = "cantaloupe" def print_name (self): print ("the fruit you buy is:% s"% self. Name) class GrapeClass: # grape def _ _ init__ (self): self.name = "grape" def print_name (self): print ("the fruit you bought is:% s"% self.name) class FruitWeight: # weighing factory def _ _ init__ (self Weight): self.weight = float (weight) def print_weight (self): print ("weight of the fruit:% .2F kg"% self.weight) class FruitPrice: # Price Factory def get_price (self, name_index Variety): if name_index = = 0: price_object = OrangePrice (variety) elif name_index = = 1: price_object = Hami_MelonPrice () elif name_index = = 2: price_object = GrapePrice () else: price_object = None return price_objectclass OrangePrice: # Orange price def _ init__ (self Variety): self.variety = variety if self.variety = = 1: self.price = 8.5else: self.price = 11.0def print_price (self): print ("the unit price of this fruit is:% .2f yuan / kg"% self.price) class Hami_MelonPrice: # Hami melon price def _ _ init__ (self): Self.price = 24.3def print_price (self): print ("the price of the fruit is:% .2f / kg"% self.price) class GrapePrice: # grape price category def _ _ init__ (self): self.price = 16.2 def print_price (self): print ("the price of the fruit is:% .2f / kg"% self.price) Return self.priceclass FruitPack: # Packaging Factory def _ _ init__ (self Pack): if pack = = 1: self.pack = "scattered" else: self.pack = "boxed" def print_pack (self): print ("the fruit is packaged in% s"% self.pack) class FruitFactory: def _ init__ (self, name_index, weight, variety, pack): # assignment of tasks Variety, weight, price, packing method self.name_object = FruitClass (). Get_name (name_index) self.weight_object = FruitWeight (weight) self.price_object = FruitPrice (). Get_price (name_index Variety) self.pack_object = FruitPack (pack) def print_purchase (self): # calculate the purchase amount money = self.price_object.price * self.weight_object.weight print ("the total amount to be paid is:% .2f yuan"% money) def show_info (self): # show the final purchase information self.name_object .print _ name () self.weight_object.print_weight () self.price_object.print_price () self.pack_object.print_pack () self.print_purchase () print ("- * -" * 20) class Consumer: # def _ init__ (self): print ("- * -" * 20) # Enter the original "purchase demand" information self.name = input ("Please enter the name of the fruit you want to buy: 0. Orange 1. Cantaloupe 2. Grape ") self.weight = input (" Please enter the weight of the fruit you want to buy (kg: ") self.variety = input (" if you buy oranges, we have two kinds of oranges: 0. Don't buy oranges. Tangerine 2. Sugar orange ") self.pack = input (" Please choose the packing method of the fruit: 1. Loose weighting 2. Boxed ") print ("-*-"* 20) def request (self): # returns relevant purchase information return self.name, self.weight, self.variety, self.packif _ _ name__ = =" _ _ main__ ": # create customer buyer = Consumer () # get customer purchase information buy_info = buyer.request () # use fruit factory Convey instructions to its sub-factories and execute purchase operations buy_res = FruitFactory (int (buy_info [0]), int (buy_info [1]), int (buy_info [2]), int (buy_ info [3]) # display of purchase information buy_res.show_info (). I believe you have a better understanding of "how to implement the abstract factory pattern of python design pattern". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report