工厂方法模式的结构

工厂方法模式:就是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类当中。核心工厂类不在负责产品的创建,这样核心类成为一个抽象工厂角色,仅负责具体工厂子类必须实现的接口,这样进一步抽象化的好处是 使得工厂方法模式可以使系统在不修改具体工厂角色的情况下引进新的产品。

interface Service{
void methodOne();
void methodTwo();
}

interface ServiceFactory{
Service getService();
}

class ImplementationOne implements Service{
ImplementationOne(){};

public void methodOne(){
    System.out.println("ImplementationOne's methodOne()");
}

public void methodTwo() {
    System.out.println("ImplementationOne's methodTwo()");
}

}

class ImplementationTwo implements Service{
ImplementationTwo(){}

public void methodOne() {
    System.out.println("ImplementationTwo's methodOne()");
}
public void methodTwo() {
    System.out.println("ImplementationTwo's methodTwo()");
}

}

class ImplementationOneFactory implements ServiceFactory{
@Override
public Service getService() {
return new ImplementationOne();
}
}

class ImplementationTwoFactory implements ServiceFactory{
@Override
public Service getService() {
return new ImplementationTwo();
}
}
public class FactoryMethodDemo {
public static void serviceConsumer(ServiceFactory sFact){
Service svc = sFact.getService();
svc.methodOne();
svc.methodTwo();
}
public static void main(String[] args){
serviceConsumer(new ImplementationOneFactory());
serviceConsumer(new ImplementationTwoFactory());
}
}
/output/
ImplementationOne’s methodOne()
ImplementationOne’s methodTwo()
ImplementationTwo’s methodOne()
ImplementationTwo’s methodTwo()

这个代码的结构,也可以使用匿名内部类来实现:

interface Service{
void methodOne();
void methodTwo();
}

interface ServiceFactory{
Service getService();
}

class ImplementationOne implements Service{

@Override
public void methodOne() {
    System.out.println("ImplementationOne methodOne()");
}

@Override
public void methodTwo() {
    System.out.println("ImplementationOne methodTwo()");
}

public static ServiceFactory factory=
        new ServiceFactory(){
        public Service getService(){
            return new ImplementationOne();
        }
};

}

class ImplementationTwo implements Service{

@Override
public void methodOne() {
    System.out.println("ImplementationTwo methodOne()");
}

@Override
public void methodTwo() {
    System.out.println("ImplementationTwo methodTwo()");
}

public static ServiceFactory factory=
        new ServiceFactory(){
        public Service getService(){
            return new ImplementationTwo();
        }
};

}

public class AnonumousInnerClassDemo {
public static void serviceConsumer(ServiceFactory fact){
Service service=fact.getService();
service.methodOne();
service.methodTwo();
}

public static void main(String[] args){
    serviceConsumer(ImplementationOne.factory);
    serviceConsumer(ImplementationTwo.factory);
}

}
/output/
ImplementationOne methodOne()
ImplementationOne methodTwo()
ImplementationTwo methodOne()
ImplementationTwo methodTwo()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值