工厂模式
简单工厂模式
属于创建型工厂模式,由一个工厂类决定创建出哪种类的实例,这个类封装了实例化对象的行为,是工厂模式中最简单使用的模式。
package SimpleFactory;
public class SimpleFactory {
public static Product getProduct(String className){
Product product = null;
if("A".equals(className)){
product = new Son1Product();
}
if("B".equals(className)){
product = new Son2Product();
}
return product;
}
}
abstract class Product{
public abstract void getProduct();
}
class Son1Product extends Product{
@Override
public void getProduct() {
System.out.println("son1调用getProduct方法");
}
}
class Son2Product extends Product{
@Override
public void getProduct() {
System.out.println("son2调用getProduct方法");
}
}
class MainClass{
public static void main(String[] args) {
Product product = SimpleFactory.getProduct("A");
product.getProduct();
}
}
工厂方法模式
定义一个创建对象的抽象方法,由子类决定要实例化的类。将对象的实例化推迟到子类中。
package SimpleFactory;
public abstract class FactoryMethod {
abstract Pizza getPizza(Integer data);
}
class SingleSonFactory extends FactoryMethod{
@Override
Pizza getPizza(Integer data) {
Pizza pizza = null;
if(data ==1){
pizza = new Son1Pizza();
}
if(data ==3){
pizza = new Son3Pizza();
}
return pizza;
}
}
class DoubleSonFactory extends FactoryMethod{
@Override
Pizza getPizza(Integer data) {
Pizza pizza = null;
if(data == 2){
pizza = new Son2Pizza();
}
if(data == 4){
pizza = new Son4Pizza();
}
return pizza;
}
}
abstract class Pizza{
public abstract void getPizza();
}
class Son1Pizza extends Pizza{
@Override
public void getPizza() {
System.out.println("son1调用getSon方法");
}
}
class Son2Pizza extends Pizza{
@Override
public void getPizza() {
System.out.println("son2调用getSon方法");
}
}
class Son3Pizza extends Pizza{
@Override
public void getPizza() {
System.out.println("son3调用getSon方法");
}
}
class Son4Pizza extends Pizza{
@Override
public void getPizza() {
System.out.println("son4调用getSon方法");
}
}
class MainFactoryMethodClass{
public static void main(String[] args) {
Pizza pizza = new SingleSonFactory().getPizza(3);
pizza.getPizza();
}
}
抽象工厂模式
定义一个接口用于创建相关或有依赖关系的对象簇,而无需指定具体的类,可以看成是将简单工厂模式和工厂方法模式的整合。
package SimpleFactory;
public interface AbstractFactory {
Pizza getPizza(Integer data);
}
class SingleSonFactory implements AbstractFactory{
@Override
public Pizza getPizza(Integer data) {
Pizza pizza = null;
if(data ==1){
pizza = new Son1Pizza();
}
if(data ==3){
pizza = new Son3Pizza();
}
return pizza;
}
}
class DoubleSonFactory implements AbstractFactory{
@Override
public Pizza getPizza(Integer data) {
Pizza pizza = null;
if(data == 2){
pizza = new Son2Pizza();
}
if(data == 4){
pizza = new Son4Pizza();
}
return pizza;
}
}
abstract class Pizza{
public abstract void getPizza();
}
class Son1Pizza extends Pizza{
@Override
public void getPizza() {
System.out.println("son1调用getSon方法");
}
}
class Son2Pizza extends Pizza{
@Override
public void getPizza() {
System.out.println("son2调用getSon方法");
}
}
class Son3Pizza extends Pizza{
@Override
public void getPizza() {
System.out.println("son3调用getSon方法");
}
}
class Son4Pizza extends Pizza{
@Override
public void getPizza() {
System.out.println("son4调用getSon方法");
}
}
class MainAbstractFactory{
public static void main(String[] args) {
getInstance(new SingleSonFactory());
}
public static void getInstance(AbstractFactory abstractFactory){
abstractFactory.getPizza(1);
}
}
JDK中Calendar类就使用了简单工厂模式
|