uml類圖
**實現
python實現工廠方法public abstract class animal
public class dog : animal
}public class cat : animal
}public abstract class factory
public class dogfactory:factory
}public class catfactory : factory
}
class animal:
def print(self):
raise notimplementederror("abstract")
class cat(animal):
def print(self):
print("this is cat")
class dog(animal):
def print(self):
print("this is dog")
class animalfactory:
def getanimalfactory(self):
raise notimplementederror("abstract")
class catfactory(animalfactory):
def getanimalfactory(self):
return cat()
class dogfactory(animalfactory):
def getanimalfactory(self):
return dog()
cat=catfactory().getanimalfactory()
dog=dogfactory().getanimalfactory()
cat.print()
dog.print()
工廠方法模式 工廠方法模式
工廠方法模式是簡單工廠模式的公升級版,簡單工廠模式不符合設計模式的原則 即 單一職責,開閉原則 優點 職責明確,擴充套件方便 缺點 需要建立多個工廠 實現步驟 1.將工廠通用方法抽取介面 例如 ifactory 2.將產品抽取介面 例如 icar 3.實現各種產品 例如 baomacar,benti...
工廠方法模式 工廠方法模式 二
工廠方法模式是對簡單工廠的進一步抽象和封裝,需要新的類物件時不需要對既有工廠類進行修改,而是增加新的工廠類。工程類可以使用模版進一步封裝,由編譯器來生成 從而減少 編寫工作量。工廠方法的 c 實現01part產品抽象基類class animal virtual void show 0 02part產...
工廠方法模式(一) 簡單工廠方法模式
ps 第二篇學習部落格,堅持就是勝利。繼續設計模式的學習,記錄工廠模式,加深自己的理解。基本結構 abstractproduct 用來定義基本的商品的抽象 public abstract class abstractphoneproduct 用來實現抽象商品,生成各種商品 public class ...