将factory类作为animal接口及其子类的容器(ioc思想),如果需要获取Animal,只需要从factory中获取即可。
具体的代码实现
接口和实现
public interface Animal {
void run();
}
public class Dog implements Animal{
@Override
public void run() {
System.out.println("狗跑步");
}
}
public class Person implements Animal{
@Override
public void run() {
System.out.println("人走路");
}
}
public class Pig implements Animal{
@Override
public void run() {
System.out.println("猪睡觉");
}
}
简单工厂方法的封装
public class AnimalFactory {
public static Animal getAnimalByType(String type) {
if ("Person".equals(type)) {
return new Person();
}
if ("Pig".equals(type)) {
return new Pig();
}
if ("Dog".equals(type)) {
return new Dog();
}
throw new UnsupportedOperationException();
}
}
封装client测试类
public class Client {
public static void main(String[] args) {
// 通过简单工厂方法获取Animal
Animal animal= AnimalFactory.getAnimalByType("Pig");
animal.run();
}
}
测试结果
猪睡觉
弊端,factory是独立的一个类,需要单独维护,并且没有抽象
AnimalDelegete类属于委托代理类
public class AnimalDelegete implements Animal {
private final Animal delegate;
public AnimalDelegete(String type) {
if ("Person".equals(type)) {
delegate = new Person();
} else if ("Pig".equals(type)) {
delegate = new Pig();
} else if ("Dog".equals(type)) {
delegate = new Dog();
} else {
throw new UnsupportedOperationException();
}
}
@Override
public void run() {
delegate.run();
}
}
测试类Client2
public class Client2 {
public static void main(String[] args) {
// 通过委托类获取Animal
Animal animal= new AnimalDelegete("Pig");
animal.run();
}
}
通过一个委托类,也是接口的子类来维护子类的统一实现,简直6的飞起
public class RoutingStatementHandler implements StatementHandler {
private final StatementHandler delegate;
public RoutingStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
switch (ms.getStatementType()) {
case STATEMENT:
delegate = new SimpleStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
break;
case PREPARED:
delegate = new PreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
break;
case CALLABLE:
delegate = new CallableStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
break;
default:
throw new ExecutorException("Unknown statement type: " + ms.getStatementType());
}
}
@Override
public Statement prepare(Connection connection, Integer transactionTimeout) throws SQLException {
return delegate.prepare(connection, transactionTimeout);
}
@Override
public void parameterize(Statement statement) throws SQLException {
delegate.parameterize(statement);
}
@Override
public void batch(Statement statement) throws SQLException {
delegate.batch(statement);
}
@Override
public int update(Statement statement) throws SQLException {
return delegate.update(statement);
}
@Override
public List query(Statement statement, ResultHandler resultHandler) throws SQLException {
return delegate.query(statement, resultHandler);
}
@Override
public Cursor queryCursor(Statement statement) throws SQLException {
return delegate.queryCursor(statement);
}
@Override
public BoundSql getBoundSql() {
return delegate.getBoundSql();
}
@Override
public ParameterHandler getParameterHandler() {
return delegate.getParameterHandler();
}
}