java 来自http://stackoverflow.com/questions/23720140/using-factory-pattern-with-multiple-arguments-in-

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 来自http://stackoverflow.com/questions/23720140/using-factory-pattern-with-multiple-arguments-in-相关的知识,希望对你有一定的参考价值。

public class Main {

    public static interface IMessage{
        public String getName();
    }

    public static class GoodMessage implements IMessage{
        @Override
        public String getName() {
            return "Good";
        }
    }

    public static class BadMessage implements IMessage{
        @Override
        public String getName() {
            return "Bad";
        }
    }

    public static interface IMsgGen{
        public IMessage create();
    }

    public static enum Messages implements IMsgGen {

        GOOD_MSG(GoodMessage.class),

        BAD_MSG(BadMessage.class),

        CUSTOM_MSG(null){
            @Override
            public IMessage create() {
                return new IMessage() {
                    @Override
                    public String getName() {
                        return "Custom";
                    }
                };
            }
        };

        private final Class<? extends IMessage> mClass;
        private Messages(Class<? extends IMessage> aClass) {
            mClass = aClass;
        }

        @Override
        public IMessage create() {
            try {
                return mClass.newInstance();
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage(),e);
            }
        }
    }

    public static void main(String[] args){
        IMessage msg = Messages.GOOD_MSG.create();
        System.out.println(msg.getName());

        msg = Messages.BAD_MSG.create();
        System.out.println(msg.getName());

        msg = Messages.CUSTOM_MSG.create();
        System.out.println(msg.getName());
    }
}

以上是关于java 来自http://stackoverflow.com/questions/23720140/using-factory-pattern-with-multiple-arguments-in-的主要内容,如果未能解决你的问题,请参考以下文章

java #Drawable来自FileName.java

来自java的流插入:templateSuffix

来自客户端的 Java (Tomcat) websocket 参数

Java:没有来自外部类的 ArrayList 修改

java 来自http://javabeat.net/java-getresourcestream/

Java:字体不加载(来自外部文件)