Java接口———工厂方法设计模式

接口是实现多重继承的途径,而生成遵循某个接口的对象的典型方法就是工厂方法设计模式。工厂方法与直接调用构造器不同,直接调用构造器,会导致对象的生成与对象的使用耦合性太强,使得代码不够灵活,而工厂方法则能够很好的使两者分离。而且工厂方法将完全和接口实现分离,这样也使得我们可以透明的将某个实现替换为另一个实行。

下面是工厂方法的结构:

package Test_1;


interface Service{
    void method1();
    void method2();
}

interface ServiceFactory{
    Service getService();
}

class Implements1 implements Service{
    public void method1(){ System.out.println("Implements1.method1");}
    public void method2(){ System.out.println("Implements1.method2");}
}

class Implements1Factory implements ServiceFactory{
    public Service getService(){
        return new Implements1();
    }
}

class Implements2 implements Service{
    public void method1() { System.out.println("Implements2.method1");}
    public void method2() { System.out.println("Implements2.method2");}
}

class Implements2Factory implements ServiceFactory{
    public Service getService(){
        return new Implements2();
    }
}

public class Factories {
    public static void serviceConsumer(ServiceFactory fact){
        Service s = fact.getService();
        s.method1();
        s.method2();
    }

    public static void main(String[] args){
        serviceConsumer(new Implements1Factory());
        serviceConsumer(new Implements2Factory());

    }
}

下面是输出结果:

Implements1.method1
Implements1.method2
Implements2.method1
Implements2.method2

根据不同接口可以提供不同的实现方法,而具体的实现方法也与工厂方法分离。不明白的朋友多敲敲代码就能够理解了我觉得。

通过匿名类实现的简化工厂方法设计模式如下:


interface Service{
    void method1();
    void method2();
}

interface ServiceFactory{
    Service getService();
}

class Implements1 implements Service{
    public void method1(){ System.out.println("Implements1.method1");}
    public void method2(){ System.out.println("Implements1.method2");}
    public static ServiceFactory Implements1Factory = new ServiceFactory(){
        public Service getService(){
            return new Implements1();
        }
    };
}


class Implements2 implements Service{
    public void method1() { System.out.println("Implements2.method1");}
    public void method2() { System.out.println("Implements2.method2");}
    public static ServiceFactory Implements2Factory = new ServiceFactory(){
        public Service getService(){
            return new Implements2();
        }
    };
}

public class Factories {
    public static void serviceConsumer(ServiceFactory fact){
        Service s = fact.getService();
        s.method1();
        s.method2();
    }

    public static void main(String[] args){
        serviceConsumer(Implements1.Implements1Factory);
        serviceConsumer(Implements2.Implements2Factory);

    }
}

通过匿名类使得代码简单优雅了许多,而且也不再需要具体的ServiceFactory类了。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值