参考文档:
1.设计模式书籍;
在学习Facade模式的时候,已知应用中,说到了:
Choices操作系统[CIRM93]使用facade模式将多个框架组合到一起。Choices中的关键抽象是进程( process )、存储( storage )和地址空间(address space)。每个抽象有一个相应的子系统,用框架实现,支持Choices系统在不同的硬件平台之间移植。其中的两个子系统有“代表”(也就是facade),这两个代表分别是存储( FileSystemInterface )和地址空间( Domain )。
MemoryObjectCache将MemoryObject数据缓存在物理存储器中。MemoryObjectCache实际上是一个Strategy(5.9)模式,由它定位缓存策略。
于是,根据上图,实现这个Facade + Strategy模式的案例
代码实现,(对于具体的业务逻辑没有深入研究,欢迎拍砖):
Process.java:
package com.rick.designpattern.facade2strategy;
/**
* Created by MyPC on 2017/6/6.
*/
public class Process {
public static void main(String[] args) {
Domain domain = new Domain();
Address address = new Address((byte) 0x0001);
Memory memory = new Memory(address);
domain.add(memory, address);
domain.remove(memory);
}
}
Domain.java:
package com.rick.designpattern.facade2strategy; /** * Created by MyPC on 2017/6/6. */ public class Domain { private AddressTranslation mAddressTranslation; private MemoryObject mMemoryObject; private MemoryObjectCache mMemoryObjectCache; public Domain() { this.mAddressTranslation = new TwoLevelPageTable(); this.mMemoryObject = new PersistentStore(); this.mMemoryObjectCache = new PagedMemoryObjectCache(); } public void add(Memory memory, Address address) { mAddressTranslation.findMemory(address); mMemoryObject.buildCache(address); } public void remove(Memory memory) { mAddressTranslation.findMemory(memory.getAddress()); mMemoryObject.remove(memory.getAddress()); } public void protect(Memory memory, Protection protection) { } public void RepairFault() { } }
AddressTranslation.java:
package com.rick.designpattern.facade2strategy; /** * Created by MyPC on 2017/6/6. */ public abstract class AddressTranslation { public abstract Memory findMemory(Address address); }
TwoLevelPageTable.java:
package com.rick.designpattern.facade2strategy; /** * Created by MyPC on 2017/6/6. */ public class TwoLevelPageTable extends AddressTranslation { @Override public Memory findMemory(Address address) { return new Memory(address); } }
MemoryObject.java:
package com.rick.designpattern.facade2strategy; /** * Created by MyPC on 2017/6/6. */ public abstract class MemoryObject { public abstract void buildCache(Address address); public abstract void remove(Address address); }
PersistentStore.java:
package com.rick.designpattern.facade2strategy; /** * Created by MyPC on 2017/6/6. */ public class PersistentStore extends MemoryObject { @Override public void buildCache(Address address) { MemoryObjectCache memoryObjectCache = new PagedMemoryObjectCache(); memoryObjectCache.cache(address); } @Override public void remove(Address address){ } }
Disk.java:
package com.rick.designpattern.facade2strategy; /** * Created by MyPC on 2017/6/6. */ public class Disk extends PersistentStore { @Override public void buildCache(Address address) { super.buildCache(address); } @Override public void remove(Address address) { } }
File.java:
package com.rick.designpattern.facade2strategy; /** * Created by MyPC on 2017/6/6. */ public class File extends PersistentStore { @Override public void buildCache(Address address) { super.buildCache(address); } @Override public void remove(Address address) { } }
MemoryObjectCache.java:
package com.rick.designpattern.facade2strategy; /** * Created by MyPC on 2017/6/6. */ public abstract class MemoryObjectCache { public abstract void cache(Address address); }
PagedMemoryObjectCache.java:
package com.rick.designpattern.facade2strategy; /** * Created by MyPC on 2017/6/6. */ public class PagedMemoryObjectCache extends MemoryObjectCache { @Override public void cache(Address address) { } }
Address.java:
package com.rick.designpattern.facade2strategy; /** * Created by MyPC on 2017/6/6. */ public class Address { private byte mByte = 0x00010; public Address(byte mByte) { this.mByte = mByte; } public byte getmByte() { return mByte; } public void setmByte(byte mByte) { this.mByte = mByte; } }
Memory.java:
package com.rick.designpattern.facade2strategy; /** * Created by MyPC on 2017/6/6. */ public class Memory { private Address address; public Memory(Address address) { this.address = address; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } }
Process.java:
package com.rick.designpattern.facade2strategy; /** * Created by MyPC on 2017/6/6. */ public class Process { public static void main(String[] args) { Domain domain = new Domain(); Address address = new Address((byte) 0x0001); Memory memory = new Memory(address); domain.add(memory, address); domain.remove(memory); } }
Protection.java:
package com.rick.designpattern.facade2strategy; /** * Created by MyPC on 2017/6/6. */ public class Protection { }