行为型模式-模板方法模式

2020-03-18  本文已影响0人  格林哈

0 行为型模式

1 模板方法模式

package com.mg.springjiemi.templatemethod;
public abstract class Vehicle {
    //模板方法
    public final void  drive(){
        //点火启动汽车
        startTheEngine();
        //挂挡 可能有手动挡,自动挡
        putIntoGear();
        //跑路
        go();
    }
    //开车的步骤
    protected  abstract void putIntoGear();
    //开车的步骤
    public void go(){
        //...
        System.out.println("跑路");
    }
    public void startTheEngine(){
        //...
        System.out.println("开火");
    }

}

package com.mg.springjiemi.templatemethod;
//自动挡汽车
public class VehicleZiDong extends Vehicle {
    @Override
    protected void putIntoGear() {
        System.out.println("挂自动挡");
    }
}

package com.mg.springjiemi.templatemethod;
public class VeicleShouDong extends Vehicle {
    @Override
    protected void putIntoGear() {
        System.out.println("手动挡");
    }
}


2 模板方法与Callback结合使用

package com.gerp.service.common;
import com.gerp.common.faceRecognition.FaceRecognitionPrx;
public interface Callback<T> {
    public T callback(FaceRecognitionPrx helloWorld);
}


package com.gerp.service.common;
public class FaceServiceTemplate {
    private final Logger LOGGER = Logger.getLogger(FaceServiceTemplate.class);

    //公共模板
    private <T> T execute(Callback<T> fun) {
        FaceRecognitionPrx helloWorld = null;
        Ice.Communicator ic = null;
        try {
            ic = Ice.Util.initialize();
            Ice.ObjectPrx base = ic.stringToProxy(BaseData.getBaseData().getIce_url());
            helloWorld = FaceRecognitionPrxHelper.checkedCast(base);
            if (helloWorld == null) {
                Error error = new Error("Invalid proxy");
                LOGGER.error(error.getMessage());
                throw error;
            }
            return fun.callback(helloWorld);
        } catch (Ice.LocalException e) {
            LOGGER.error(e.getMessage());
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
        } finally {
            if (ic != null) {
                ic.destroy();
            }

        }
        return null;


    }
    /**
     * 2.   人脸检测接口
     *
     * @param staticDetect
     * @return
     */
    public StaticDetectResult staticDetect(final StaticDetect staticDetect) {
        try {
            return this.execute(new Callback<StaticDetectResult>() {
                @Override
                public StaticDetectResult callback(FaceRecognitionPrx helloWorld) {
                    String data = Base.beanToXml(staticDetect, StaticDetect.class);
                    String value = helloWorld.send(data);
                    StaticDetectResult result = (StaticDetectResult) Base.xmlToBean(value, StaticDetectResult.class);
                    return result;
                }

            });
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 3 人脸验证接口 判断是否同一个人
     *
     * @param compare
     * @return
     */
    public CompareResult compare(final Compare compare) {
        return this.execute(new Callback<CompareResult>() {
            @Override
            public CompareResult callback(FaceRecognitionPrx helloWorld) {
                String data = Base.beanToXml(compare, Compare.class);
                String value = helloWorld.send(data);
                CompareResult result = (CompareResult) Base.xmlToBean(value, CompareResult.class);
                return result;
            }

        });
    }



}


上一篇下一篇

猜你喜欢

热点阅读