
jasper的技术小窝
关注DevOps、运维监控、Python、Golang、开源、大数据、web开发、互联网
工厂模式
下面要开始说一说工厂模式呢,自我感觉,工厂模式好像会更多地运用在架构层面。一般讲工厂模式分为三种:简单工厂模式、工厂方法模式、抽象工厂模式;下面就重点说一下他们在java和python中的运用。
简单的说工厂模式,能让你轻松方便地构造对象实例,而不必关心构造对象实例的细节和复杂过程。在软件设计要求"开放-封闭"原则,其要求系统对扩展开放,对修改封闭。而工厂模式很好地契合了这一原则。
废话不说,直接上java的例子:
一、简单工厂模式:
抽象类,提供eat方法:
public interface Animal { //创建动物接口,提供吃的方法 public abstract void eat(); }
用两个类来实现:
public class Dog implements Animal { @Override public void eat() { System.out.println("狗吃骨头");//根据实际情况,具体实现接口中的方法 } } public class Cat implements Animal { @Override public void eat() { System.out.println("猫吃小鱼");//根据实际情况,具体实现接口中的方法 } }
创建工厂
public class Factory { public static Animal createAnimal(String name){ //在工厂中提供静态方法,根据传递的参数,动态的选择实例化哪个类 if("cat".equalsIgnoreCase(name)){ return new Cat(); }else if("dog".equalsIgnoreCase(name)){ return new Dog(); }else{ return null ; } } }
二、工厂方法模式:
分别创建dog和cat的工厂,将一中的Factory分为了一下三个:
public abstract class FactoryAnimal { public void eat(){ Animal a = createAnimal(); a.eat(); } abstract Animal createAnimal(); } public class FactoryDog extends FactoryAnimal{ @Override public Dog createAnimal() { return new Dog(); } } public class FactoryCat extends FactoryAnimal { @Override public Cat createAnimal() { return new Cat(); } }
三、抽象工厂模式:
实现黑/白 狗/猫吃东西这样一个过程
public interface AnimalFactory { public Dog newDog(); public Cat newCat(); } public class BlackAnimalFactory implements AnimalFactory { @Override public Dog newDog() { // TODO Auto-generated method stub return new BlackDog(); } @Override public Cat newCat() { // TODO Auto-generated method stub return new BlackCat(); } } public class BlackCat implements Cat { @Override public void eat() { System.out.println("黑猫吃东西"); } } public class BlackDog implements Dog { @Override public void eat() { System.out.println("黑狗吃东西"); } } public interface Cat { public void eat(); } public interface Dog { public void eat(); } public class WhiteAnimalFactory implements AnimalFactory { @Override public Dog newDog() { // TODO Auto-generated method stub return new WhiteDog(); } @Override public Cat newCat() { // TODO Auto-generated method stub return new WhiteCat(); } } public class WhiteCat implements Cat { @Override public void eat() { System.out.println("白猫吃东西"); } } public class WhiteDog implements Dog { @Override public void eat() { System.out.println("白狗吃东西"); } }
和简单工厂模式相比,工厂方法和抽象工厂其实非常相似,他们都是通过工厂类来创建产品类。但是他们也有不同,一个具体的工厂类只能创建一个具体的产品类。而在抽象工厂中,他把具体的特征都抽象出来了,一个工厂类可以实现创建多个产品。由此,我们可以得到以下结论:
工厂方法模式:
一个抽象产品类,可以派生出多个具体产品类。
一个抽象工厂类,可以派生出多个具体工厂类。
每个具体工厂类只能创建一个具体产品类的实例。
抽象工厂模式:
多个抽象产品类,每个抽象产品类可以派生出多个具体产品类。
一个抽象工厂类,可以派生出多个具体工厂类。
每个具体工厂类可以创建多个具体产品类的实例。
ok,说完了java,下面要来简单讲一讲在python中是怎么实现工厂模式的吧。
一、简单工厂模式:
from abc import ABCMeta # 可以认为dog和cat都是动物的一种,可以有个基类 class Animal(object): # ABCMeta会让这个类在注册后添加很多基础抽象基类 __metaclass__ = ABCMeta def eat(self): pass class Dog(Animal): def eat(self): return 'Dog food...' class Cat(Animal): def eat(self): return 'Cat food...'
在工厂里面:
class Factory(object): def createAnimal(type): if type=="dog": return Dog() elif type=="cat": return Cat() else: return null
二、工厂方法模式:
class DogFactory(object): def get_animal(self): return Dog(); class CatFactory(object): # 注意这个方法和上面的名字一样,但是返回的类不同,这就是工厂的作用 def get_animal(self): return Cat();
三、抽象工厂模式:
class Dog(): def eat(self): pass class WhiteDog(Dog): def eat(self): print "白狗吃东西" class BlackDog(Dog): def eat(self): print "黑狗吃东西" class Cat(): def eat(self): pass class WhiteCat(Cat): def eat(self): print "白猫吃东西" class BlackCat(Cat): def eat(self): print "黑猫吃东西" class Factory(): def CreateDog(self): pass def CreateCat(self): pass class ConcreateWhite(Factory): def CreateDog(self): dog = WhiteDog() return dog def CreateCat(self): cat = Cat() return cat class ConcreateBlack(Factory): def CreateDog(self): dog = BlackDog() return dog def CreateCat(self): cat = BlackCat() return cat
上面本人仅仅是写出了实现方式,具体怎么用,我不写,因为看过之后就会懂哒。
好吧,虽然用python实现了所谓的工厂模式,但是由于python中没有所谓的上下转型,所以例子中的基类也没太大的用处。这样的工厂模式也不像java中那样的饱满。
其他分类: