用内部类实现工厂模式 :优先使用类而不是接口,如果你的设计中需要某个接口,你必须了解它,否则不到迫不得已,不要将其放到你的类中

//: innerclasses/Factories.java
import static net.mindview.util.Print.*; interface Service {
void method1();
void method2();
} interface ServiceFactory {
Service getService();
} class Implementation1 implements Service {
private Implementation1() {} //构造器可以是private
public void method1() {print("Implementation1 method1");}
public void method2() {print("Implementation1 method2");}
public static ServiceFactory factory = //经常只需要单一的工厂对象,因此用了static域,这样产生的语法也更有实际意义
new ServiceFactory() {
public Service getService() {
return new Implementation1();
}
};
} class Implementation2 implements Service {
private Implementation2() {}
public void method1() {print("Implementation2 method1");}
public void method2() {print("Implementation2 method2");}
public static ServiceFactory factory =
new ServiceFactory() {
public Service getService() {
return new Implementation2();
}
};
} public class Factories {
public static void serviceConsumer(ServiceFactory fact) {
Service s = fact.getService();
s.method1();
s.method2();
}
public static void main(String[] args) {
serviceConsumer(Implementation1.factory);
// Implementations are completely interchangeable:
serviceConsumer(Implementation2.factory);
}
} /* Output:
Implementation1 method1
Implementation1 method2
Implementation2 method1
Implementation2 method2
*///:~
//: innerclasses/Games.java
// Using anonymous inner classes with the Game framework.
package object;
import static net.mindview.util.Print.*; interface Game { boolean move(); }
interface GameFactory { Game getGame(); } class Checkers implements Game {
private Checkers() {}
private int moves = 0;
private static final int MOVES = 3;
public boolean move() {
print("Checkers move " + moves);
return ++moves != MOVES;
}
public static GameFactory factory = new GameFactory() {
public Game getGame() { return new Checkers(); }
};
} class Chess implements Game {
private Chess() {}
private int moves = 0;
private static final int MOVES = 4;
public boolean move() {
print("Chess move " + moves);
return ++moves != MOVES;
}
public static GameFactory factory = new GameFactory() {
public Game getGame() { return new Chess(); }
};
} public class Games {
public static void playGame(GameFactory factory) {
Game s = factory.getGame();
while(s.move())
;
}
public static void main(String[] args) {
playGame(Checkers.factory);
playGame(Chess.factory);
}
} /* Output:
Checkers move 0
Checkers move 1
Checkers move 2
Chess move 0
Chess move 1
Chess move 2
Chess move 3
*///:~

java 内部类 工厂方法的更多相关文章

  1. 比较 Java 静态工厂方法与构造函数

    1 什么是静态工厂方法 Java 静态工厂方法是在方法前加上 public static,让这个方法变为公开.静态的方法.该方法返回该类的一个实例,就好像一个工厂生产出一个产品.所以称之为静态工厂方法 ...

  2. Java之工厂方法

    普通工厂模式: 第一步:定义接口,坚持面向接口编程, package dp; public interface Sender {    public void send();} 第二步:实现接口: p ...

  3. JAVA设计模式--工厂方法模式

    工厂方法设计模式 抽象工厂角色: 这是工厂方法模式的核心,它与应用程序无关.是具体工厂角色必须实现的接口或者必须继承的父类.在java中它由抽象类或者接口来实现.具体工厂角色:它含有和具体业务逻辑有关 ...

  4. 我的Java设计模式-工厂方法模式

    女朋友dodo闹脾气,气势汹汹的说"我要吃雪糕".笔者心里啊乐滋滋的,一支雪糕就能哄回来,不亦乐乎?! 但是,雪糕买回来了,她竟然说"不想吃雪糕了,突然想吃披萨" ...

  5. Java设计模式-工厂方法模式(Virtual Constructor/Polymorphic Factory)

    工厂方法模式(Virtual Constructor/Polymorphic Factory) 工厂方法模式是类的创建模式,又叫做虚拟构造子模式(Virtual Constructor)或者多态性工厂 ...

  6. java之工厂方法设计模式

    工厂方法模式是设计模式中应用最广泛的模式.在面向对象的编程中,对象的创建工作非常简单,对象的创建时机却很重要.工厂方法模式就是解决这个问题,它通过面向对象的手法,将所要创建的具体对象创建工作延迟到了子 ...

  7. Java设计模式-工厂方法模式(Factory Method)

    工厂方法模式(Factory Method) 工厂模式适合:凡是出现了大量的产品需要创建,并且具有共同的接口时,可以通过工厂方法模式进行创建.在以下的三种模式中,第一种如果传入的字符串有误,不能正确创 ...

  8. Java设计模式---工厂方法模式(Factory-Method)

    一.普通工厂模式 建立一个工厂类,对实现了同一接口的一些类进行实例的创建 实例代码: 发送短信和邮件的例子,首先创建接口: public interface Sender { public void ...

  9. Java设计模式—工厂方法模式&抽象工厂模式

    工厂方法模式与抽象工厂模式都是设计模式中重要而且常见的模式.       工厂方法模式:定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法使一个类的实例化延迟到其子类. 通用类图如下: 在 ...

随机推荐

  1. Redis 通信协议简单研究

    1.Redis网络通信协议 Redis底层网络通信协议其实是通过TCP来完成的. 2.Redis通信协议 Redis的通信协议首先是以行来划分,每行以\r\n行结束.每一行都有一个消息头,消息头共分为 ...

  2. TRichEdit怎样新增的内容到最后一行?

    Delphi里使用TRichEdit,使用SetSelTextBuf时可以设置显示的字体格式,但是显示位置是在当前的插入光标后,如果人为改变插入光标的位置,比如在其他位置单,以后再插入的内容位置就没办 ...

  3. 51nod 1476 括号序列的最小代价(贪心+优先队列)

    题意 我们这有一种仅由"(",")"和"?"组成的括号序列,你必须将"?"替换成括号,从而得到一个合法的括号序列. 对于 ...

  4. 51nod 1480 打广告(二分+线段树)

    题意 给出n个区间和m个区间,从这n个区间里选一个区间a,这m个区间选一个区间b,使得a&b的长度*c最大. 思路 如果这n个区间里有一个区间包含另一个区间,那另外一个区间就可以忽略掉,进行\ ...

  5. poj2082 Terrible Sets(单调栈)

    Description Let N be the set of all natural numbers {0 , 1 , 2 , . . . }, and R be the set of all re ...

  6. VMXNET3 vs E1000E and E1000

    VMXNET3 vs E1000E and E1000 用户为什么要从E1000调整为VMXNET3,理由如下: E1000是千兆网路卡,而VMXNET3是万兆网路卡: E1000的性能相对较低,而V ...

  7. Python(3)---从迭代器到异步IO

    whenif 关注 2017.02.13 23:48* 字数 1750 阅读 250评论 0喜欢 8 目录 1. 迭代(iteration)与迭代器(iterator) 1.1 构建简单迭代器 1.2 ...

  8. OpenStack中RabbitMQ高可用性配置

    采用镜像队列的方案进行配置 1. 网络拓扑 node1:10.10.11.1 node2:10.10.11.2 2. 配置hosts node1+node2: vim /etc/hosts >1 ...

  9. Codeforces Round #481 (Div. 3) D. Almost Arithmetic Progression

    http://codeforces.com/contest/978/problem/D 题目大意: 给你一个长度为n的b(i)数组,你有如下操作: 对数组中的某个元素+1,+0,-1.并且这个元素只能 ...

  10. centos 7 两台机器搭建三主三从 redis 集群

    参考自:https://linux.cn/article-6719-1.htmlhttp://blog.csdn.net/xu470438000/article/details/42971091 ## ...