/定义不同的产品之间的一定具备的标准,用interface实现 //其中的method()方法可看作提取出不同产品的共性,如手机都有类似的功能 interface IProductA{ public void method(); } interface IProductB{ public void method(); } // 实现了产品标准实现的一系列具体产品 class ProductA implements IProductA { public void method(){ } } class ProductB implements IProductB { public void method(){ } } // 每一种牌子的产品生产工厂,即不同的厂商负责自己牌子产品的生产 abstract class Factory { abstract IProductA getProductA1(); abstract IProductB getProductB1(); } // 具体的工厂用来生产相关的产品 class ConcreteFactory extends Factory{ public IProductA getProductA(){ return new ProductA(); } public IProuctB getProductB(){ return new ProductB(); } } // 测试类 public class AbstractFactoryModel{ public static void test(){ Factory factory = new ConcreteFactory(); IProductA productA = factory.getProductA(); IProductB productB = factory.getProductB(); productA.method(); productB.method(); } }
public interface Cook { public void moreSalt(); public void moreOil(); public void moreWater(); } public class Director(Cook cook){ Cook cook; public Director(Cook cook){ this.cook = cook; } public void makeMeal(){ cook.moreSalt(); cook.moreOil(); cook.moreWater(); } } public class John implements Cook { public void moreSalt(){ //put more salt } public void moreOil(){ //put more oil } public void moreWater(){ //put more water } } //生成器模式 public class BuilderMode { public static void test(){ Cook cook = new John(); Director d = new Director(cook); d.makeMeal(); //让指挥者做菜,其实指挥者叫John做菜 } }
public interface IProduct { public void productMethod(); } public class FactoryMethodModel implements IProduct{ public void productMethod(){ System.out.println("产品"); } } public class Tree implements IProduct { @Override public void productMethod() { System.out.println("树产品"); } }
public abstract class AbstractMonkey implements Cloneable{ public Object clone(){ Object o = null; try{ o = super.clone(); }catch(CloneNotSupportedException e){ e.printStackTrace(); } return o; } public abstract void dothing(); } public class RealMonkey extends AbstractMonkey{ public void dothing(){ } } public class CloneMode{ public static void test(){ AbstractMonkey a1 = new RealMonkey(); AbstractMonkey a2 = (AbstractMonkey) a1.clone(); a1.dothing(); a2.dothing(); } }
public class SingleMode { private static SingleMode mThisMode; // 已经自行实例化 // private static final SingleMode mThisMode2 = new SingleMode(); public static SingleMode getInstance() {// 懒汉式单例类 if (mThisMode == null) mThisMode = new SingleMode(); return mThisMode; } // public static SingleMode getInstance2() {// 饿汉式单例类 // return mThisMode2; // } }
public class Adapter { private Adaptee adaptee; public Adapter(Adaptee adaptee){ this.adaptee = adaptee; } /*源类Adaptee有方法sampleOperation1 因此适配器类直接委派即可*/ public void sampleOperation1(){ this.adaptee.sampleOperation1(); } /*源类Adaptee没有方法sampleOperation2 因此由适配器类需要补充此方法*/ public void sampleOperation2(){ //some code to execute } } public class Adaptee { public void sampleOperation1() { } } public interface Target { /** * 这是源类Adaptee也有的方法 */ public void sampleOperation1(); /** * 这是源类Adapteee没有的方法 */ public void sampleOperation2(); }
//桥梁模式 public class BridgeMode { public static void test(){ // 创建具体的实现对象 MessageImplmentor impl = new MessageSMS(); // 创建普通消息对象 AbstractMessage message = new CommonMessage(impl); message.sendMessage("加班申请速批","李总"); // 将实现方式切换成邮件,再次发送 impl = new MessageEmail(); // 创建加急消息对象 message = new Urgencymessage(impl); message.sendMessage("加班申请速批","李总"); } } public interface MessageImplementor{ public void send(String message, String toUser); } public class MessageSMS implements MessageImplementor{ @Override public void send(String message, String toUser){ System.out.println("使用系统内短消息的方法,发送消息'" + message + "'给" + toUser); } } public class MessageEmail implements MessageImplementor{ @Override public void send(String message, String toUser){ System.out.println("使用邮件短消息的方法,发送消息'" + message + "'给" + toUser); } } public abstract class AbstractMessage{ //持有一个实现部分的对象 MessageImplementor impl; //构造方法,传入实现部分的对象 public AbstractMessage(MessageImplementor impl){ this.impl = impl; } //发送消息,委派给实现部分的方法 public void sendMessage(String message, String toUser){ this.impl.send(message, toUser); } } public class CommonMessage extends AbstractMessage{ public CommonMessage(MessageImplementor impl){ super(impl); } @Override public void sendMessage(String message, String toUser){ // 对于普通消息,直接调用父类方法,发送消息即可 super.sendMessage(message, toUser); } } public class UrgencyMessage extends AbstractMessage(){ public UrgencyMessage(ImessageImplementor impl){ super(impl); } @Override public void sendMessage(String message, String toUser){ message = "加急:" +message; super.send(message, toUser); } //扩展自己的新功能,监控某消息的处理状态 public Object watch(String messageId){ // 根据消息id获取消息的状态,组织成监控的数据对象,然后返回 return null; } }
interface Node{ // 定义统一的接口:复制 public void copy(); } class File implements Node{ private String fileName; public File(String fileName){ this.fileName = fileName; } public void copy(){ System.out.println("复制文件:" + fileName); } } class Folder implements Node{ private String folderName; // 用于存储文件夹下的文件夹或文件的信息 private ArrayList nodeList = new ArrayList(); public Folder(String folderName){ this.folderName = folderName; } public void add(Node node){ // 增加文件或文件夹 nodeList.add(node); } public void copy(){ // 文件夹复制操作实现递归 for (int i = 0; i < nodeList.size(); i++) { Node node = (Node) nodeList.get(i); node.copy(); } } } public class CombinationMode{ public static void test(){ Folder document = new Folder("我的资料"); File book = new File("Java编程思想.pdf"); Folder music = new Folder("我的音乐"); File music1 = new File("Timber.mp3"); File music2 = new File("Let it go.mp3"); // 确定树形结构关系 document.add(book); document.add(music); music.add(music1); music.add(music2); // 复制“我的资料”文件夹,递归地复制了其下所有文件夹和文件。 document.copy(); } }
//装饰器模式 public interface Component { public void operation(); } public class ConcreteComponent implements Component { public ConcreteComponent(){ } public void operation(){ System.out.println("开车"); } } public class Decorator implements Component { private Component component; public Decorator(){ } public Decorator(Component component){ this.component = component; } public void operation(){ component.operation(); } } public class ConcreteDecorator extends Decorator { public ConcreteDecorator(){ } public ConcreteDecorator(Component component){ super(component); } public void operation(){ this.addedOperation(); super.operation(); } public void addedOperation(){ System.out.println("晚上"); } } public class DecorativeMode { public static void test(){ Component component = new ConcreteComponent(); Decorator decorator = new ConcreteDecorator(component); //客户端不变,但已增加了责任 decorator.operation(); } }
//外观模式 public class ModuleA{ //提供给子系统外部使用的方法 public void a1(){ } //子系统内部模块之间相互调用时使用的方法 public void a2(){ } public void a3(){ } } public class ModuleB { /** * 提供给子系统外部使用的方法 */ public void b1() { }; /** * 子系统内部模块之间相互调用时使用的方法 */ public void b2() { }; public void b3() { }; } public class ModuleC { /** * 提供给子系统外部使用的方法 */ public void c1() { }; /** * 子系统内部模块之间相互调用时使用的方法 */ public void c2() { }; public void c3() { }; } public class ModuleFacade { Module a = new Module(); ModuleB b = new ModuleB(); ModuleC c = new ModuleC(); /** * 下面这些是A、B、C模块对子系统外部提供的方法 */ public void a1() { a.a1(); } public void b1() { b.b1(); } public void c1() { c.c1(); } } public class WindowMode { public static void test() { ModuleFacade facade = new ModuleFacade(); facade.a1(); facade.b1(); facade.c1(); } }
//亨元模式 public interface Flyweight { public void operation(String state); } public class ConcreteFlyweight implements Flyweight{ private Character intrinsicState = null; /** * 构造函数,内蕴状态作为参数传入 */ public ConcreteFlyweight(Character state){ this.intrinsicState = state; } /** * 外蕴状态作为参数传入方法中,改变方法的行为, 但是并不改变对象的内蕴状态。 */ @Override public void operation(String state){ System.out.println("Intrinsic State = " + this.intrinsicState); System.out.println("Extrinsic State = " + state); } } public class FlyweightFactory{ private Map<Character, Flyweight> files = new HashMap<Character, Flyweight>(); public Flyweight factory(Character state) { // 先从缓存中查找对象 Flyweight fly = files.get(state); if (fly == null) { // 如果对象不存在则创建一个新的Flyweight对象 fly = new ConcreteFlyweight(state); // 把这个新的Flyweight对象添加到缓存中 files.put(state, fly); } return fly; } } public class FlyweightMode { public static void test(){ FlyweightFactory factory = new FlyweightFactory(); Flyweight fly = factory.factory(new Character('a')); fly.operation("First Call"); fly = factory.factory(new Character('b')); fly.operation("Second Call"); fly = factory.factory(new Character('a')); fly.operation("Third Call"); } }
//代理模式 public interface Italk { public void talk(String msg); } public class People implements Italk { public String username; public String age; public String getName() { return username; } public void setName(String name) { this.username = name; } public String getAge(){ return age; } public void setAge(String age) { this.age = age; } public People(String name1, String age1){ this.username = name1; this.age = age1; } public void talk(String msg) { System.out.println(msg + "! Hello,I am "+ username + ", my age is "+ age); } } public class TalkProxy implements Italk { Italk talker; public TalkProxy(Italk talker) { this.talker = talker; } public void talk(String msg) { talker.talk(msg); } public void talk(String msg, String singname){ talker.talk(msg); sing(singname); } private void sing(String singname) { System.out.println("唱歌:" + singname); } } public class ProxyMode { public static void test() { // 不需要执行额外方法的 Italk people1 = new People("湖海散人", "18"); people1.talk("No ProXY Test"); // 需要执行额外方法的 TalkProxy talker = new TalkProxy(people1); talker.talk("ProXY Test", "七里香"); } }