Java设计模式:工厂方法模式


public interface Factory {
	Product makeProduct();
}

 


public class MyFactory1 implements Factory {

	@Override
	public Product makeProduct() {
		Product product = new MyProduct1();
		return product;
	}
}

public interface Product {
	void method();
}

public class MyProduct1 implements Product{

	@Override
	public void method() {
		System.out.println("MyProduct1 method");
	}
}

 

public class Test {
	public static void main(String[] args) {
		try {
			Test test = new Test();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Test() {
		Factory factory = new MyFactory1();
		Product product = factory.makeProduct();
		product.method();
	}
}

 

输出:

MyProduct1 method

 

和本文类似:https://blog.csdn.net/zhangphil/article/details/88713685

 

 

你可能感兴趣的:(Java,设计模式,设计模式与架构设计)