Java设计模式-2-工厂方法模式

来源:互联网 发布:数控车凹圆弧编程实例 编辑:程序博客网 时间:2024/06/05 19:40

一、介绍:

首先说一下工厂模式,工厂模式根据抽象程度的不同分为三种:简单工厂模式(也叫静态工厂模式)、本文所讲述的工厂方法模式抽象工厂模式

二、UML类图:


三、工厂方法模式代码

interface IProduct {public void productMethod();}class Product implements IProduct {public void productMethod() {System.out.println("产品");}}interface IFactory {public IProduct createProduct();}class Factory implements IFactory {public IProduct createProduct() {return new Product();}}public class Client {public static void main(String[] args) {IFactory factory = new Factory();IProduct prodect = factory.createProduct();prodect.productMethod();}}


0 0