黑马程序员技术交流社区
标题: 内部类 小结二 [打印本页]
作者: 321哈哈哈 时间: 2017-10-12 01:41
标题: 内部类 小结二
“迭代器”设计模式:
内部类实现了迭代接口,按它们自己的方式实现这个接口(每个迭代器按照自己的方法迭代外部类对象包含的元素,例中外部类包装了一个数组)
同一个外部类可以用多个内部类同时实现该接口,但具体实现不同,就拥有了不同的迭代功能
别的方法能以此接口为参数,来生成更加通用的代码(利用多态)
interface Selector {
boolean end();
Object current();
void next();
}
public class Sequence {
private Object[] items;
private int next = 0;
public Sequence(int size) {
items = new Object[size];
}
public void add(Object x) {
if (next < items.length)
items[next++] = x;
}
private class SequenceSelector implements Selector {
private int i = 0;
public boolean end() {
return i == items.length;
}
public Object current() {
return items;
}
public void next() {
if (i < items.length)
i++;
}
}
public Selector selector() {
return new SequenceSelector();
}
public static void main(String[] args) {
Sequence sequence = new Sequence(10);
for (int i = 0; i < 10; i++)
sequence.add(Integer.toString(i));
Selector selector = sequence.selector();
while (!selector.end()) {
System.out.print(selector.current() + " ");
selector.next();
}
}
} /*
* Output: 0 1 2 3 4 5 6 7 8 9
*/// :~
使用匿名内部类的工厂方法设计模式
没有单独创建工厂类
工厂被写为Service实现中的一个static域(字段),使用匿名内部类,实现了接口的getService方法
Service实现类的构造方法设为私有,Service对象只能通过工厂字段的getSerice()获得
import static net.mindview.util.Print.*;
interface Service {
void method1();
void method2();
}
interface ServiceFactory {
Service getService();
}
class Implementation1 implements Service {
private Implementation1() {}
public void method1() {print("Implementation1 method1");}
public void method2() {print("Implementation1 method2");}
//工厂在这
public static ServiceFactory factory =
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
*///:~
模板方法设计模式
模板方法包含算法的基本结构,并且会调用一个或者多个可覆盖的方法
(骨架是确定的,细节是可扩展的)
(设计模式总是将变化的事物与保持不变的事物分离开,这里模板方法是不变的,可覆盖方法是变化的)
模板方法的应用:
应用程序框架(application framework),是被设计用来解决某类特定问题的一个类或一组类
要应用某个应用程序框架,通常是继承这个(组)类并重写方法,以解决特定问题
控制框架(control framework)是一类特殊的应用程序框架,需求是响应事件
主要用来响应事件的系统被称作事件驱动系统(GUI几乎完全是事件驱动的系统,java Swing库就是一个控制框架)
关键词:控制 事件(分别是接口或者父类,控制框架类要实现控制接口,让自己的内部事件类分别实现事件接口)
下面例子中分别是事件抽象类、控制类、控制框架类、测试类
// The common methods for any control event.事件抽象类
public abstract class Event {//本例中的事件全是简单的时间触发的,实际的事件类型会更多样
private long eventTime;
protected final long delayTime;
public Event(long delayTime) {
this.delayTime = delayTime;
start();
}
public void start() { // Allows restarting
eventTime = System.nanoTime() + delayTime;
}
public boolean ready() {
return System.nanoTime() >= eventTime;
}
public abstract void action();
} ///:~
// The reusable framework for control systems.控制类
public class Controller {
// A class from java.util to hold Event objects:
private List<Event> eventList = new ArrayList<Event>();
public void addEvent(Event c) { eventList.add(c); }
public void run() {
while(eventList.size() > 0)
// Make a copy so you're not modifying the list
// while you're selecting the elements in it:
for(Event e : new ArrayList<Event>(eventList))
if(e.ready()) {
System.out.println(e);
e.action();
eventList.remove(e);
}
}
} ///:~
// This produces a specific application of the控制框架类
// control system, all in a single class. Inner
// classes allow you to encapsulate different
// functionality for each type of event.
public class GreenhouseControls extends Controller {
private boolean light = false;
public class LightOn extends Event {//各种内部事件类在控制框架中的应用
public LightOn(long delayTime) { super(delayTime); }
public void action() {
// Put hardware control code here to
// physically turn on the light.
light = true;
}
public String toString() { return "Light is on"; }
}
public class LightOff extends Event {
public LightOff(long delayTime) { super(delayTime); }
public void action() {
// Put hardware control code here to
// physically turn off the light.
light = false;
}
public String toString() { return "Light is off"; }
}
private boolean water = false;
public class WaterOn extends Event {
public WaterOn(long delayTime) { super(delayTime); }
public void action() {
// Put hardware control code here.
water = true;
}
public String toString() {
return "Greenhouse water is on";
}
}
public class WaterOff extends Event {
public WaterOff(long delayTime) { super(delayTime); }
public void action() {
// Put hardware control code here.
water = false;
}
public String toString() {
return "Greenhouse water is off";
}
}
private String thermostat = "Day";
public class ThermostatNight extends Event {
public ThermostatNight(long delayTime) {
super(delayTime);
}
public void action() {
// Put hardware control code here.
thermostat = "Night";
}
public String toString() {
return "Thermostat on night setting";
}
}
public class ThermostatDay extends Event {
public ThermostatDay(long delayTime) {
super(delayTime);
}
public void action() {
// Put hardware control code here.
thermostat = "Day";
}
public String toString() {
return "Thermostat on day setting";
}
}
// An example of an action() that inserts a
// new one of itself into the event list:
public class Bell extends Event {
public Bell(long delayTime) { super(delayTime); }
public void action() {
addEvent(new Bell(delayTime));
}
public String toString() { return "Bing!"; }
}
public class Restart extends Event {
private Event[] eventList;
public Restart(long delayTime, Event[] eventList) {
super(delayTime);
this.eventList = eventList;
for(Event e : eventList)
addEvent(e);
}
public void action() {// 这个事件执行时会重置指定事件和它自己
for(Event e : eventList) {
e.start(); // Rerun each event
addEvent(e);
}
start(); // Rerun this Event
addEvent(this);
}
public String toString() {
return "Restarting system";
}
}
public static class Terminate extends Event {
public Terminate(long delayTime) { super(delayTime); }
public void action() { System.exit(0); }
public String toString() { return "Terminating"; }
}
} ///:~
// Configure and execute the greenhouse system.测试类
// {Args: 5000}
public class GreenhouseController {
public static void main(String[] args) {
GreenhouseControls gc = new GreenhouseControls();
// Instead of hard-wiring, you could parse
// configuration information from a text file here:
gc.addEvent(gc.new Bell(900));//这里往里面加事件是硬编码
Event[] eventList = {//更灵活的方式是从文件中读取需要的事件(方便修改)
gc.new ThermostatNight(0),
gc.new LightOn(200),
gc.new LightOff(400),
gc.new WaterOn(600),
gc.new WaterOff(800),
gc.new ThermostatDay(1400)
};
gc.addEvent(gc.new Restart(2000, eventList));
if(args.length == 1)
gc.addEvent(
new GreenhouseControls.Terminate(
new Integer(args[0])));
gc.run();
}
} /* Output:
Bing!
Thermostat on night setting
Light is on
Light is off
Greenhouse water is on
Greenhouse water is off
Thermostat on day setting
Restarting system
Terminating
*///:~
作者: 李龙菲 时间: 2017-10-12 16:06



作者: 恋爱不如敲代码 时间: 2017-10-12 16:34
还没有学 不过感觉很难的样子
作者: wheat 时间: 2017-10-12 18:39
不错哦
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |