在需要不同的条件产生具有多态性质的一组类时,可以考虑使用工厂类来生成.
优点就是职责简单实现单一,实现具体代码和实现解耦,工厂类则是关键里面包含了具体的逻辑判断,缺点也很明显就是如果产品类多的话一堆逻辑判断还是比较恶心的。看示例代码
package com.demo.stragetry.
public class FactoryTest {
static interface Operation{
public void calculate();
}
public static class AddOperation implements Operation{
@Override
public void calculate() {
// TODO Auto-generated method stub
System.out.println("add operation");
}
}
public static class SubOperation implements Operation{
@Override
public void calculate() {
// TODO Auto-generated method stub
System.out.println("sub operation");
}
}
public static class MulOperation implements Operation{
@Override
public void calculate() {
// TODO Auto-generated method stub
System.out.println("mul operation");
}
}
public static class DivOperation implements Operation{
@Override
public void calculate() {
// TODO Auto-generated method stub
System.out.println("div operation");
}
}
public static class FaltOperation implements Operation{
@Override
public void calculate() {
// TODO Auto-generated method stub
System.out.println("Falt operation");
}
}
public static class OperationFactory{
public Operation calculate(String operation){
if("add".equals(operation)){
return new AddOperation();
}else if("sub".equals(operation)){
return new SubOperation();
}else if("mul".equals(operation)){
return new MulOperation();
}else if("div".equals(operation)){
return new DivOperation();
}else{
return new FaltOperation();
}
}
}
public static void main(String[] args) {
OperationFactory factory = new OperationFactory();
Operation operation= factory.calculate("add");
operation.calculate();
}
}
设计模式之多工厂模式
该模式是对普通工厂模式的改进,在普通工厂模式中如果字符串参数出错,则不能正确的创建对象,而多工厂模式则是针对不同对象分别创建对象,废话不多说了看代码吧。。。
package com.demo.stragetry;
public class FactoryTest {
static interface Operation{
public void calculate();
}
public static class AddOperation implements Operation{
@Override
public void calculate() {
// TODO Auto-generated method stub
System.out.println("add operation");
}
}
public static class SubOperation implements Operation{
@Override
public void calculate() {
// TODO Auto-generated method stub
System.out.println("sub operation");
}
}
public static class MulOperation implements Operation{
@Override
public void calculate() {
// TODO Auto-generated method stub
System.out.println("mul operation");
}
}
public static class DivOperation implements Operation{
@Override
public void calculate() {
// TODO Auto-generated method stub
System.out.println("div operation");
}
}
public static class FaltOperation implements Operation{
@Override
public void calculate() {
// TODO Auto-generated method stub
System.out.println("Falt operation");
}
}
public static class OperationFactory{
public Operation addOperation(){
return new AddOperation();
}
public Operation subOperation(){
return new SubOperation();
}
public Operation mulOperation(){
return new MulOperation();
}
public Operation divOperation(){
return new DivOperation();
}
}
public static void main(String[] args) {
OperationFactory factory = new OperationFactory();
// Operation operation= factory.calculate("add");
Operation operation = factory.addOperation();
operation.calculate();
}
}
设计模式之静态工厂模式
该模式是针对多工厂模式而言,若多个对象则创建比较麻烦,将类和方法都改为static 直接调用而不需要新建代码就不放了。。。。