SpecialistOff.NET / Вопросы / Статьи / Фрагменты кода / Резюме / Метки / Помощь / Файлы
НазадМетки: python
A general principle that I apply when I’m casting about trying to mold requirements into a first-cut object is “If something is ugly, hide it inside an object.” This is basically what Façade accomplishes. If you have a rather confusing collection of classes and interactions that the client programmer doesn’t really need to see, then you can create an interface that is useful for the client programmer and that only presents what’s necessary.
Façade is often implemented as singleton abstract factory. Of course, you can easily get this effect by creating a class containing static factory methods:
# ChangeInterface/Facade.py class A: def __init__(self, x): pass class B: def __init__(self, x): pass class C: def __init__(self, x): pass # Other classes that aren't exposed by the # facade go here ... class Facade: def makeA(x): return A(x) makeA = staticmethod(makeA) def makeB(x): return B(x) makeB = staticmethod(makeB) def makeC(x): return C(x) makeC = staticmethod(makeC) # The client programmer gets the objects # by calling the static methods: a = Facade.makeA(1); b = Facade.makeB(1); c = Facade.makeC(1.0);
[rewrite this section using research from Larman’s book]
Example for Facade (?): my “nicer” version of the XML library.