1.静态工厂模式
package creational_pattern.static_factory_pattern;
public class StaticFactoryPattern {
public static void main(String[] args) {
Product concreteProduct = Factory.getProduct("ConcreteProduct");
concreteProduct.commonMethod();
concreteProduct.diffMethod();
}
}
abstract class Product{
public void commonMethod(){
}
public abstract void diffMethod();
}
class ConcreteProduct extends Product{
@Override
public void diffMethod() {
}
}
class OtherProduct extends Product{
@Override
public void diffMethod() {
}
}
class Factory{
public static Product getProduct(String type){
if(type.equals("ConcreteProduct"))
return new ConcreteProduct();
else{
return new OtherProduct();
}
}
}
2.工厂方法模式
package creational_pattern.factory_method_pattern;
public class FactoryMethodPattern {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
Class<?> cl = Class.forName("creational_pattern.factory_method_pattern.FileLoggerFactory");
LoggerFactory loggerFactory = (LoggerFactory) cl.newInstance();
Logger logger = loggerFactory.createLogger();
logger.writeLog();
}
}
interface Logger{
void writeLog();
}
class FileLogger implements Logger{
@Override
public void writeLog() {
System.out.println("file logging");
}
}
class DatabaseLogger implements Logger{
@Override
public void writeLog() {
System.out.println("database logging");
}
}
interface LoggerFactory{
public abstract Logger createLogger();
}
class FileLoggerFactory implements LoggerFactory{
@Override
public Logger createLogger() {
return new FileLogger();
}
}
class DatabaseLoggerFactory implements LoggerFactory{
@Override
public Logger createLogger() {
return new DatabaseLogger();
}
}
3.抽象工厂模式
package creational_pattern.abstract_factory_pattern;
public class AbstractFactoryPattern {
public static void main(String[] args) {
Factory factory1 = new AlibabaFctory();
Factory factory2 = new BaiduFctory();
ProductA productA = factory1.createProductA();
productA.display();
System.out.println("----------");
ProductA preparing_workA = factory1.createProductA("preparing_workA");
preparing_workA.display();
System.out.println("----------");
ProductB preparing_workB = factory1.createProductB("preparing_workB");
preparing_workB.display();
System.out.println("----------");
ProductB productB = factory1.createProductB("productB");
productB.display();
System.out.println("----------");
ProductA productA1 = factory2.createProductA();
productA1.display();
System.out.println("----------");
ProductB productB1 = factory2.createProductB();
productB1.display();
}
}
abstract class ProductA{
abstract void display();
}
class AlibabaProductA1 extends ProductA{
@Override
void display() {
System.out.println("AlibabaProductA1");
}
}
class BaiduProductA2 extends ProductA{
@Override
void display() {
System.out.println("BaiduProductA2");
}
}
abstract class ProductB{
abstract void display();
}
class AlibabaProductB1 extends ProductB{
@Override
void display() {
System.out.println("AlibabaProductB1");
}
}
class BaiduProductB2 extends ProductB{
@Override
void display() {
System.out.println("BaiduProductB2");
}
}
interface Factory{
ProductA createProductA();
ProductA createProductA(String args);
ProductB createProductB();
ProductB createProductB(String args);
}
class AlibabaFctory implements Factory{
@Override
public ProductA createProductA() {
return new AlibabaProductA1();
}
@Override
public ProductA createProductA(String args) {
System.out.println(args);
return new AlibabaProductA1();
}
@Override
public ProductB createProductB() {
return new AlibabaProductB1();
}
@Override
public ProductB createProductB(String args) {
System.out.println(args);
return new AlibabaProductB1();
}
}
class BaiduFctory implements Factory{
@Override
public ProductA createProductA() {
return new BaiduProductA2();
}
@Override
public ProductA createProductA(String args) {
System.out.println(args);
return new BaiduProductA2();
}
@Override
public ProductB createProductB() {
return new BaiduProductB2();
}
@Override
public ProductB createProductB(String args) {
System.out.println(args);
return new BaiduProductB2();
}
}