标签:分离 基础 uil this 匹配 dem image 主题 根据
public class Chain { public abstract class Handler { private Handler nextHandler; // 当前领导能审批通过的最多天数 public int maxDay; protected Handler(int maxDay) { this.maxDay = maxDay; } //设置责任链中下一个处理请求的对象 public void setNextHandler(Handler handler) { nextHandler = handler; } protected void handleRequest(int day) { if (day <= maxDay) { reply(day); } else { if (nextHandler != null) { //审批权限不够,继续上报 nextHandler.handleRequest(day); } else { System.out.println("没有更高的领导审批了"); } } } protected abstract void reply(int day); } class ProjectManager extends Handler { public ProjectManager(int day) { super(day); } @Override protected void reply(int day) { System.out.println(day + "天请假,项目经理直接审批通过"); } } class DepartmentManager extends Handler { public DepartmentManager(int day) { super(day); } @Override protected void reply(int day) { System.out.println(day + "天请假,部门经理审批通过"); } } class GeneralManager extends Handler { public GeneralManager(int day) { super(day); } @Override protected void reply(int day) { System.out.println(day + "天请假,总经理直接审批通过"); } } public static void main(String[] strings) { Chain chain = new Chain(); Handler projectManager = chain.new ProjectManager(3); Handler departmentManager = chain.new DepartmentManager(5); Handler generalManager = chain.new GeneralManager(15); //创建职责链 projectManager.setNextHandler(departmentManager); departmentManager.setNextHandler(generalManager); //发起请假请求 projectManager.handleRequest(4); } }
public class AlerDialog { private String title; private String message; public AlerDialog(Builder builder) { // View.inflate() this.title = builder.title; this.message = builder.message; } public static class Builder { private String title; private String message; public Builder setTitle(String title) { this.title = title; return this; } public Builder setMessage(String message) { this.message = message; return this; } public AlerDialog build() { return new AlerDialog(this); } } } new AlerDialog.Builder().setTitle("").setMessage("").build();
public interface Target { void sampleOperation1(); void sampleOperation2(); } public class Adaptee { public void sampleOperation1() { System.out.println("sampleOperation1"); } } public class Adapter extends Adaptee implements Target { @Override public void sampleOperation2() { System.out.println("sampleOperation2"); } } public class MyClass { public static void main(String[] args) { Adapter adapter = new Adapter(); adapter.sampleOperation1(); adapter.sampleOperation2(); } }
public interface Target { void sampleOperation1(); void sampleOperation2(); } public class Adaptee { public void sampleOperation1() { System.out.println("sampleOperation1"); } } public class Adapter implements Target { private Adaptee mAdaptee; public Adapter(Adaptee adaptee) { mAdaptee = adaptee; } @Override public void sampleOperation1() { mAdaptee.sampleOperation1(); } @Override public void sampleOperation2() { System.out.println("sampleOperation2"); } } public class MyClass { public static void main(String[] args) { Adapter adapter =new Adapter(new Adaptee()); adapter.sampleOperation1(); adapter.sampleOperation2(); } }
/** * subject(抽象主题角色): * 真实主题与代理主题的共同接口。 */ interface Subject { void sellBook(); } /** * ReaISubject(真实主题角色): * 定义了代理角色所代表的真实对象。 */ public class RealSubject implements Subject { @Override public void sellBook() { System.out.println("出版社卖书"); } } /** * Proxy(代理主题角色): * 含有对真实主题角色的引用,代理角色通常在将客 * 户端调用传递给真实主题对象之前或者之后执行某些 * 操作,而不是单纯返回真实的对象。 */ public class ProxySubject implements Subject { private RealSubject realSubject; @Override public void sellBook() { if (realSubject == null) { realSubject = new RealSubject(); } sale(); realSubject.sellBook(); give(); } public void sale() { System.out.println("打折"); } public void give() { System.out.println("送优惠券"); } } public class Main { public static void main(String[] args) { //静态代理(我们自己静态定义的代理类) ProxySubject proxySubject = new ProxySubject(); proxySubject.sellBook(); //动态代理(通过程序动态生成代理类,该代理类不是我们自己定义的。而是由程序自动生成) RealSubject realSubject = new RealSubject(); MyHandler myHandler = new MyHandler(); myHandler.setProxySubject(realSubject); Subject subject = (Subject) Proxy.newProxyInstance(realSubject.getClass().getClassLoader(), realSubject.getClass().getInterfaces(), myHandler); subject.sellBook(); } } public class MyHandler implements InvocationHandler { private RealSubject realSubject; public void setProxySubject(RealSubject realSubject) { this.realSubject = realSubject; } /** * @param proxy 指代我们所代理的那个真实对象 * @param method 指代的是我们所要调用真实对象的某个方法的Method对象 * @param args 指代的是调用真实对象某个方法时接受的参数 * @return * @throws Throwable */ @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { sale(); proxy = method.invoke(realSubject, args); give(); return proxy; } public void sale() { System.out.println("打折"); } public void give() { System.out.println("送优惠券"); } }
public class FlyWeight { static class MyString { private String myChar; public MyString(String myChar) { this.myChar = myChar; } public void display() { System.out.println(myChar); } } static class MyCharacterFactory { private Map<String, MyString> pool; public MyCharacterFactory() { pool = new HashMap<>(); } public MyString getMyCharacte(String strig) { MyString myString = pool.get(strig); if (myString == null) { myString = new MyString(strig); pool.put(strig, myString); } return myString; } } public static void main(String[] args) { MyCharacterFactory myCharacterFactory = new MyCharacterFactory(); MyString a = myCharacterFactory.getMyCharacte("a"); MyString b = myCharacterFactory.getMyCharacte("b"); MyString a1 = myCharacterFactory.getMyCharacte("a"); MyString d = myCharacterFactory.getMyCharacte("d"); if (a == a1) { System.out.println("true"); } } }
用最简单的例子说明设计模式(三)之责任链、建造者、适配器、代理模式、享元模式
标签:分离 基础 uil this 匹配 dem image 主题 根据
原文地址:https://www.cnblogs.com/liuyu0529/p/9021641.html