这个外观代码看起来是个好主意,还是设计本身有什么缺陷?更重要的是,我可能会在这段代码中遇到问题吗?任何帮助深表感谢。
我正在尝试构建它,以便我可以有一个 Payment 类作为我的门面,它接受 cc 号码等,以及一个 PayPal 类作为我的实现,以便我可以对卡收费并存储有关该等的信息。
class MyFacadeClass(models.Model):
account = models.ForeignKey('Account') # Account omitted from example for brevity.
implementation = CharField(max_length=255, choices=IMPL_CHOICES) # IMPL_CHOICES omitted for brevity
some_field = models.CharField(max_length=255)
def __init__(self, *args, **kwargs):
super(MyFacadeClass, self).__init__(*args, **kwargs)
if self.implementation == 'PAYPAL':
from somewhere import MyPayPalImplementationModelClass
self.impl = MyPayPalImplementationModelClass(my_facade_instance=self, some_field=self.some_field, account=self.account)
# Then MyPayPalImplementationModelClass does stuff with PayPal and has its own attributes such as ack, and datetime and fee_amount behind the scenes.
def save(self, force_insert=False, force_update=False)
if self.impl.is_valid():
self.impl.save()
super(MyFacadeClass, self).save(force_insert, force_update)