php网站开发工程师笔试,服务专业的网络建站公司,做网站后台的叫什么,苏州做网站要多少钱工厂方法模式又叫虚拟构造函数#xff08;Virtual Constructor#xff09;模式或者多态性工厂#xff08;Polymorphic Factory#xff09;模式。工厂方法模式的用意是定义一个创建产品对象的工厂接口#xff0c;将实际创建性工作推迟到子类中。
工厂模式可以分为简单工厂…工厂方法模式又叫虚拟构造函数Virtual Constructor模式或者多态性工厂Polymorphic Factory模式。工厂方法模式的用意是定义一个创建产品对象的工厂接口将实际创建性工作推迟到子类中。
工厂模式可以分为简单工厂、工厂方法和抽象工厂模式。
简单工厂模式需要注意的是简单工厂并不包含在《GoF》一书中定义的23种设计模式因为它过于简单更像是一种编程习惯并且它违反了开闭原则增加工厂需要修改类的实现。工厂方法模式是简单工厂模式的进一步抽象和推广将具体创建工作交给子类去做避免违反开闭原则。抽象工厂模式如果说工厂方法模式针对的是一个产品等级结构那么抽象工厂模式面对的就是多个产品等级结构。 由于简单工厂模式严格来说并不算是一种设计模式就不再画UML图了大家通过一个例子感受一下它的用法
public interface Shape {void draw();
}public class Circle implements Shape {Overridepublic void draw() {System.out.println(draw a circle);}
}public class Square implements Shape {Overridepublic void draw() {System.out.println(draw a square);}
}public class SimpleFactory {public static Shape createShape(String shapeType) {if (shapeType null) {throw new IllegalArgumentException(Shape type cannot be null.);}switch (shapeType.toLowerCase()) {case circle:return new Circle();case square:return new Square();default:throw new IllegalArgumentException(Unsupported shape type: shapeType);}}
}public class Demo {public static void main(String[] args) {Shape circle SimpleFactory.createShape(circle);circle.draw();Shape square SimpleFactory.createShape(square);square.draw();Shape triangle SimpleFactory.createShape(triangle);triangle.draw();}
}工厂方法模式的UML图如下 下面还是以一个例子来说明工厂方法模式的用法。假设有一个果农接口相当于图中的Creator有一个葡萄果农和一个苹果农分别实现果农接口这两个类相当于ConcreteCreator类。然后有一个水果接口 相当于Product接口一个苹果类和一个葡萄类分别实现水果接口。这时候注意体会工厂方法模式和简单工厂模式的区别工厂方法模式把创建对象的工作分别放到葡萄果农和苹果农的类中去实现也就是在具体类中实现。
public interface GuoNong {Fruit createFruit();
}public class GrapeNong implements GuoNong {Overridepublic Fruit createFruit() {return new Grape();}
}public class AppleNong implements GuoNong {Overridepublic Fruit createFruit() {return new Apple();}
}public interface Fruit {void plant();void grow();void harvest();
}public class Grape implements Fruit {Overridepublic void plant() {System.out.println(种葡萄);}Overridepublic void grow() {System.out.println(葡萄生长);}Overridepublic void harvest() {System.out.println(收葡萄);}
}public class Apple implements Fruit{Overridepublic void plant() {System.out.println(种苹果);}Overridepublic void grow() {System.out.println(苹果生长);}Overridepublic void harvest() {System.out.println(收苹果);}
}public class Demo {public static void main(String[] args) {GrapeNong grapeNong new GrapeNong();Fruit grape grapeNong.createFruit();grape.plant();grape.grow();grape.harvest();System.out.println(**************分割线**********************);AppleNong appleNong new AppleNong();Fruit apple appleNong.createFruit();apple.plant();apple.grow();apple.harvest();}
}大家如果需要视频版本的讲解可以关注下我的B站 三、设计模式之工厂方法模式精讲