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"); } } }