package com.java.ntt.designPatterns.FactoryMethed;
public interface Product {
void getName();
}
package com.java.ntt.designPatterns.FactoryMethed;
/**
* @ClassName ConcreteProduct
* @Description
* @Author niutongtong
* @Date 2019-06-02 19:04
* @Version V1.0
**/
public class ConcreteProduct implements Product {
@Override
public void getName() {
System.out.println("ConcreteProduct");
}
}
package com.java.ntt.designPatterns.FactoryMethed;
/**
* @ClassName ConcreteProduct1
* @Description
* @Author niutongtong
* @Date 2019-06-02 19:06
* @Version V1.0
**/
public class ConcreteProduct1 implements Product {
@Override
public void getName() {
System.out.println("ConcreteProduct1");
}
}
package com.java.ntt.designPatterns.FactoryMethed;
/**
* @ClassName ConcreteProduct2
* @Description
* @Author niutongtong
* @Date 2019-06-02 19:07
* @Version V1.0
**/
public class ConcreteProduct2 implements Product {
@Override
public void getName() {
System.out.println("ConcreteProduct2");
}
}
package com.java.ntt.designPatterns.FactoryMethed;
/**
* @ClassName AbstractFactory
* @Description 定义了一个创建对象的接口,但由子类决定要实例化哪个类。工厂方法把实例化操作推迟到子类。
* @Author niutongtong
* @Date 2019-06-02 21:13
* @Version V1.0
**/
public abstract class Factory {
abstract public Product factoryMethod();
public void dosomething(){
Product product=factoryMethod();
//
}
}
package com.java.ntt.designPatterns.FactoryMethed;
/**
* @ClassName ConcreteFactory
* @Description
* @Author niutongtong
* @Date 2019-06-02 21:19
* @Version V1.0
**/
public class ConcreteFactory extends Factory {
@Override
public Product factoryMethod() {
return new ConcreteProduct();
}
}
package com.java.ntt.designPatterns.FactoryMethed;
/**
* @ClassName ConcreteFactory1
* @Description
* @Author niutongtong
* @Date 2019-06-02 21:20
* @Version V1.0
**/
public class ConcreteFactory1 extends Factory{
@Override
public Product factoryMethod() {
return new ConcreteProduct1();
}
}
package com.java.ntt.designPatterns.FactoryMethed;
/**
* @ClassName ConcreteFactory2
* @Description
* @Author niutongtong
* @Date 2019-06-02 21:21
* @Version V1.0
**/
public class ConcreteFactory2 extends Factory {
@Override
public Product factoryMethod() {
return new ConcreteProduct2();
}
}
学习参考: 传送门