常用設(shè)計(jì)模式
工廠方法
定義:定義一個(gè)用于創(chuàng)建對(duì)象的接口件余,讓子類決定實(shí)例化哪一個(gè)類涨颜,工廠方法使一個(gè)類的實(shí)例化延遲到其子類年缎。
public class FactoryDemo {
interface IProduct {
public void productMethod();
}
static class Product implements IProduct {
public void productMethod() {
System.out.println("產(chǎn)品");
}
}
interface IFactory {
IProduct createProduct();
}
static class Factory implements IFactory {
public IProduct createProduct() {
return new Product();
}
}
public static void main(String[] args) {
IFactory factory = new Factory();
IProduct prodect = factory.createProduct();
prodect.productMethod();
}
}
抽象工廠
定義:為創(chuàng)建一組相關(guān)或相互依賴的對(duì)象提供一個(gè)接口,而且無(wú)需指定他們的具體類搅幅。
public class FactoryDemo {
interface IProduct1 {
void show();
}
interface IProduct2 {
void show();
}
static class Product1 implements IProduct1 {
public void show() {
System.out.println("這是1型產(chǎn)品");
}
}
static class Product2 implements IProduct2 {
public void show() {
System.out.println("這是2型產(chǎn)品");
}
}
interface IFactory {
IProduct1 createProduct1();
IProduct2 createProduct2();
}
static class Factory implements IFactory {
public IProduct1 createProduct1() {
return new Product1();
}
public IProduct2 createProduct2() {
return new Product2();
}
}
public static void main(String[] args) {
IFactory factory = new Factory();
factory.createProduct1().show();
factory.createProduct2().show();
}
}
輸出:
策略模式
定義:定義一組算法咽斧,將每個(gè)算法都封裝起來(lái)堪置,并且使他們之間可以互換。即面向接口編程张惹。
public class Client {
interface IStrategy {
void doSomething();
}
static class ConcreteStrategy1 implements IStrategy {
public void doSomething() {
System.out.println("具體策略1");
}
}
static class ConcreteStrategy2 implements IStrategy {
public void doSomething() {
System.out.println("具體策略2");
}
}
static class Context {
private IStrategy strategy;
public Context(IStrategy strategy) {
this.strategy = strategy;
}
public void execute() {
strategy.doSomething();
}
}
public static void main(String[] args) {
Context context;
System.out.println("-----執(zhí)行策略1-----");
context = new Context(new ConcreteStrategy1());
context.execute();
System.out.println("-----執(zhí)行策略2-----");
context = new Context(new ConcreteStrategy2());
context.execute();
}
}
輸出:
裝飾者
public class DecoratDemo {
interface Work {
void work();
}
static class Son implements Work { //所有裝飾類都實(shí)現(xiàn)一個(gè)統(tǒng)一的接口或者繼承同一個(gè)父類
@Override
public void work() {
System.out.println("畫畫...");
}
}
static class Mother implements Work {
Work worker; //需要被增強(qiáng)的類內(nèi)部維護(hù)一個(gè)需要“繼承”的類實(shí)例舀锨,但不使用繼承
public Mother(Work worker) {
this.worker = worker;
}
@Override
public void work() {
worker.work(); //調(diào)用的哪個(gè)work方法?
System.out.println("給畫上顏色..");
}
}
static class Father implements Work {
//需要被增強(qiáng)的類的引用
Work worker;
public Father(Work worker) {
this.worker = worker;
}
@Override
public void work() {
worker.work();
System.out.println("上畫框...");
}
}
public static void main(String[] args) {
Son s = new Son();
s.work();
Mother m = new Mother(s); //將Son對(duì)象實(shí)例作為參數(shù)傳遞進(jìn)去诵叁,從而實(shí)現(xiàn)類似“繼承”的功能
m.work();
Father f = new Father(m); //將Mother對(duì)象實(shí)例傳入雁竞,F(xiàn)ather對(duì)象實(shí)現(xiàn)三個(gè)功能
f.work(); //如果將上一行替換成 Father f = new Father(s); 將出現(xiàn)什么樣的輸出?
}
}
輸出:
代理模式
靜態(tài)代理:
public class ProxyDemo {
interface Subject {
void request();
}
private static class RealSubject implements Subject {
public void request() {
System.out.println("request");
}
}
private static class Proxy implements Subject {
private Subject subject;
public Proxy(Subject subject) {
this.subject = subject;
}
public void request() {
System.out.println("PreProcess");
subject.request();
System.out.println("PostProcess");
}
}
public static void main(String args[]) {
RealSubject subject = new RealSubject();
Proxy p = new Proxy(subject);
p.request();
}
}
動(dòng)態(tài)代理:
public class DynamicProxyDemo {
/**
* 接口
*/
interface Subject {
void request();
}
/**
* 委托類
*/
private static class RealSubject implements Subject {
public void request() {
System.out.println("request");
}
}
/**
* 代理類的調(diào)用處理器
*/
private static class ProxyHandler implements InvocationHandler {
private Subject subject;
public ProxyHandler(Subject subject) {
this.subject = subject;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("PreProcess");//定義預(yù)處理的工作拧额,當(dāng)然你也可以根據(jù) method 的不同進(jìn)行不同的預(yù)處理工作
Object result = method.invoke(subject, args);
System.out.println("PostProcess");
return result;
}
}
public static void main(String[] args) {
RealSubject realSubject = new RealSubject(); //1.創(chuàng)建委托對(duì)象
ProxyHandler handler = new ProxyHandler(realSubject); //2.創(chuàng)建調(diào)用處理器對(duì)象
Subject proxySubject = (Subject) Proxy.newProxyInstance(RealSubject.class.getClassLoader(),
RealSubject.class.getInterfaces(), handler); //3.動(dòng)態(tài)生成代理對(duì)象
proxySubject.request(); //4.通過(guò)代理對(duì)象調(diào)用方法
}
}
輸出:
模板方法
定義:定義一個(gè)操作中算法的框架碑诉,而將一些步驟延遲到子類中,使得子類可以不改變算法的結(jié)構(gòu)即可重定義該算法中的某些特定步驟侥锦。
//客戶端
public class TempleDemp {
//AbstractClass(抽象類)
static abstract class AbstractClass {
protected abstract void absMethod();
protected void hookMethod() {
//base null
}
private final void concreteMethod() {
System.out.println("Base Logic Code!");
}
public void templateMethod() {
absMethod();
hookMethod();
concreteMethod();
}
}
//ConcreteClass(具體子類)
static class ConcreteClass extends AbstractClass {
@Override
protected void absMethod() {
System.out.println("Signal Logic Code!");
}
@Override
protected void hookMethod() {
super.hookMethod();
System.out.println("hookMethod Logic Code!");
}
}
public static void main(String[] args) {
AbstractClass abstractClass = new ConcreteClass();
abstractClass.templateMethod();
}
}
輸出:
適配器模式
public class AdapterDemo {
/**
* 德標(biāo)接口
*/
public interface DBSocketInterface {
/**
* 這個(gè)方法的名字叫做:使用兩項(xiàng)圓頭的插口供電
* 本人英語(yǔ)就這個(gè)水平
*/
void powerWithTwoRound();
}
/**
* 德國(guó)插座
*/
public static class DBSocket implements DBSocketInterface {
public void powerWithTwoRound() {
System.out.println("使用兩項(xiàng)圓頭的插孔供電");
}
}
/**
* 國(guó)標(biāo)接口
*/
public interface GBSocketInterface {
/**
* 這個(gè)方法的名字叫做:使用三項(xiàng)扁頭的插口供電
* 本人英語(yǔ)就這個(gè)水平进栽,從有道詞典查得, flat意思好像是: 扁的
*/
void powerWithThreeFlat();
}
public static class GBSocket implements GBSocketInterface {
@Override
public void powerWithThreeFlat() {
System.out.println("使用三項(xiàng)扁頭插孔供電");
}
}
public static class SocketAdapter implements DBSocketInterface { //實(shí)現(xiàn)舊接口
//組合新接口
private GBSocketInterface gbSocket;
/**
* 在創(chuàng)建適配器對(duì)象時(shí)恭垦,必須傳入一個(gè)新街口的實(shí)現(xiàn)類
*
* @param gbSocket
*/
public SocketAdapter(GBSocketInterface gbSocket) {
this.gbSocket = gbSocket;
}
/**
* 將對(duì)就接口的調(diào)用適配到新接口
*/
@Override
public void powerWithTwoRound() {
gbSocket.powerWithThreeFlat();
}
}
/**
* 德國(guó)賓館
*/
public static class Hotel {
//旅館中有一個(gè)德標(biāo)的插口
private DBSocketInterface dbSocket;
public Hotel(DBSocketInterface dbSocket) {
this.dbSocket = dbSocket;
}
//旅館中有一個(gè)充電的功能
public void charge() {
//使用德標(biāo)插口充電
dbSocket.powerWithTwoRound();
}
}
public static void main(String[] args) {
GBSocketInterface gbSocket = new GBSocket();
SocketAdapter socketAdapter = new SocketAdapter(gbSocket);
Hotel hotel = new Hotel(socketAdapter);
hotel.charge();
}
}
輸出:
建造者
定義:將一個(gè)復(fù)雜對(duì)象的構(gòu)建與它的表示分離快毛,使得同樣的構(gòu)建過(guò)程可以創(chuàng)建不同的表示。
public class BuilderDemo {
private static class Product {
private String name;
private String type;
public void showProduct() {
System.out.println("名稱:" + name);
System.out.println("型號(hào):" + type);
}
static class Builder {
private String name;
private String type;
private Builder setName(String name) {
this.name = name;
return this;
}
private Builder setType(String type) {
this.type = type;
return this;
}
public Product build() {
Product product = new Product();
product.name = name;
product.type = type;
return product;
}
}
}
public static class Director {
private Product.Builder builder = new Product.Builder();
public Product getAProduct() {
builder.setName("寶馬汽車");
builder.setType("X7");
return builder.build();
}
public Product getBProduct() {
builder.setName("奧迪汽車");
builder.setType("Q5");
return builder.build();
}
}
public static void main(String[] args) {
Director director = new Director();
Product product1 = director.getAProduct();
product1.showProduct();
Product product2 = director.getBProduct();
product2.showProduct();
}
}
輸出:
觀察者
定義:定義對(duì)象間一種一對(duì)多的依賴關(guān)系番挺,使得當(dāng)每一個(gè)對(duì)象改變狀態(tài)唠帝,則所有依賴于它的對(duì)象都會(huì)得到通知并自動(dòng)更新。
public class ObserverDemo {
private static abstract class Subject {
private Vector<Observer> obs = new Vector<>();
public void addObserver(Observer obs) {
this.obs.add(obs);
}
public void delObserver(Observer obs) {
this.obs.remove(obs);
}
protected void notifyObserver() {
for (Observer o : obs) {
o.update();
}
}
public abstract void doSomething();
}
private static class ConcreteSubject extends Subject {
public void doSomething() {
System.out.println("被觀察者事件反生");
this.notifyObserver();
}
}
interface Observer {
void update();
}
private static class ConcreteObserver1 implements Observer {
public void update() {
System.out.println("觀察者1收到信息玄柏,并進(jìn)行處理襟衰。");
}
}
private static class ConcreteObserver2 implements Observer {
public void update() {
System.out.println("觀察者2收到信息,并進(jìn)行處理粪摘。");
}
}
public static void main(String[] args) {
Subject sub = new ConcreteSubject();
sub.addObserver(new ConcreteObserver1()); //添加觀察者1
sub.addObserver(new ConcreteObserver2()); //添加觀察者2
sub.doSomething();
}
}
輸出:
單例模式
定義:確保一個(gè)類只有一個(gè)實(shí)例瀑晒,而且自行實(shí)例化并向整個(gè)系統(tǒng)提供這個(gè)實(shí)例。
懶漢模式
public class Singleton {
private static Singleton instance;
private Singleton (){}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
public class Singleton {
private volatile static Singleton singleton;
private Singleton (){}
public static Singleton getSingleton() {
if (singleton == null) {
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
}
餓漢模式
public class Singleton {
private static Singleton instance = new Singleton();
private Singleton (){}
public static Singleton getInstance() {
return instance;
}
}
迭代器模式
定義:提供一種方法訪問(wèn)一個(gè)容器對(duì)象中各個(gè)元素徘意,而又不暴露該對(duì)象的內(nèi)部細(xì)節(jié)苔悦。
public class IteratorDemo {
interface Iterator<T> {
T next();
boolean hasNext();
}
private static class ConcreteIterator<T> implements Iterator<T> {
private List<T> list = null;
private int cursor = 0;
public ConcreteIterator(List<T> list) {
this.list = list;
}
public boolean hasNext() {
if (cursor == list.size()) {
return false;
}
return true;
}
public T next() {
T obj = null;
if (this.hasNext()) {
obj = this.list.get(cursor++);
}
return obj;
}
}
interface Aggregate<T> {
void add(T obj);
void remove(T obj);
Iterator<T> iterator();
}
static class ConcreteAggregate implements Aggregate<String> {
private List<String> list = new ArrayList<>();
public void add(String obj) {
list.add(obj);
}
public Iterator<String> iterator() {
return new ConcreteIterator<>(list);
}
public void remove(String obj) {
list.remove(obj);
}
}
public static void main(String[] args) {
Aggregate<String> ag = new ConcreteAggregate();
ag.add("小明");
ag.add("小紅");
ag.add("小剛");
Iterator it = ag.iterator();
while (it.hasNext()) {
String str = (String) it.next();
System.out.println(str);
}
}
}
優(yōu)秀的代碼設(shè)計(jì)
慢慢更~