代理甘改,適配器,裝飾器灭抑,享元十艾,組合,門面腾节,橋接
代理模式
(這里只是簡單說下靜態(tài)代理忘嫉,JDK動態(tài)代理和CGLIB動態(tài)代理這里略)
定義:由于某些原因需要給某對象提供一個(gè)代理以控制對該對象的訪問
//主題接口
public interface Subject {
void request();
}
//真實(shí)主題實(shí)現(xiàn)類
public class RealSubject implements Subject{
public void request() {
System.out.println("real subject");
}
}
//代理類
public class Proxy implements Subject{
private RealSubject realSubject;
public void request() {
if (realSubject == null) {
realSubject = new RealSubject();
}
preRequest();
realSubject.request();
postRequest();
}
private void postRequest() {
System.out.println("before subject ...");
}
private void preRequest() {
System.out.println("after subject ...");
}
}
Main方法及輸出結(jié)果:
public static void main(String[] args) {
Proxy proxy = new Proxy();
proxy.request();
}
after subject ...
real subject
before subject ...
適配器(Adaptee)模式
定義:將一個(gè)類的接口轉(zhuǎn)換成客戶希望的另外一個(gè)接口,使得原本由于接口不兼容而不能一起工作的那些類能一起工作
優(yōu)點(diǎn):
1.客戶端通過適配器可以透明地調(diào)用目標(biāo)接口案腺。
2.復(fù)用了現(xiàn)存的類庆冕,程序員不需要修改原有代碼而重用現(xiàn)有的適配者類。
3.將目標(biāo)類和適配者類解耦劈榨,解決了目標(biāo)類和適配者類接口不一致的問題愧杯。
缺點(diǎn):
1.對類適配器來說,更換適配器的實(shí)現(xiàn)過程比較復(fù)雜鞋既。
例子:各種adapter都是
//組合方式實(shí)現(xiàn)適配器模式:
//即對象適配器
//三相接口
public interface Three {
void chargedByThree();
}
//二相充電器
public class Two {
public void chargedByTwo(){
System.out.println("使用二相插座供電");
}
}
//二相充電器轉(zhuǎn)三相
public class Adapter implements Three{
private Two two;
public Adapter(Two two) {
this.two = two;
}
@Override
public void chargedByThree() {
System.out.println("二相轉(zhuǎn)三相 適配器處理中...");
two.chargedByTwo();
}
}
//筆記本主類充電
public class Client {
private Three three;
public Client(Three three){
this.three = three;
}
public void charged(){
three.chargedByThree();
}
}
Main函數(shù)及輸出結(jié)果:
public static void main(String[] args) {
Two two = new Two();
Three three = new Adapter(two);
Client client = new Client(three);
client.charged();
}
二相轉(zhuǎn)三相 適配器處理中...
使用二項(xiàng)插座供電
//繼承方式實(shí)現(xiàn)適配器模式:
//及類適配器
//繼承類并實(shí)現(xiàn)接口
public class AdapterExtends extends Two implements Three{
@Override
public void chargedByThree() {
System.out.println("二相轉(zhuǎn)三相 繼承適配器處理中...");
this.chargedByTwo();
}
}
Main函數(shù)及輸出結(jié)果:
public static void main(String[] args) {
Two two = new Two();
Three three = new AdapterExtends();
Client client = new Client(three);
client.charged();
}
二相轉(zhuǎn)三相 繼承適配器處理中...
使用二項(xiàng)插座供電
橋接模式
定義:將抽象與實(shí)現(xiàn)分離力九,使它們可以獨(dú)立變化
使用場景:JDBC就是使用的該模式。
mysql的driver和oracle的driver邑闺,都實(shí)現(xiàn)了Driver接口跌前,生成了DriverInfo類,通過DriverManager能獲取對應(yīng)的connection陡舅,springboot默認(rèn)的連接池就是HikariCP 使用threadlocal+CopyOnWriteArrayList抵乓,HikariCP 是一個(gè)“零開銷”的生產(chǎn)就緒 JDBC 連接池“醒埽快速灾炭、簡單、可靠颅眶。大約 130Kb 的庫非常輕巧蜈出。
//基礎(chǔ)接口
public interface Base {
void operate();
}
//基礎(chǔ)接口實(shí)現(xiàn)
public class BaseImpl implements Base{
public void operate() {
System.out.println("Base operate ...");
}
}
//擴(kuò)展抽象類
public abstract class AbstractExtra {
protected Base base;
public AbstractExtra(Base base) {
this.base = base;
}
public abstract void extraOperate();
}
//擴(kuò)展實(shí)現(xiàn)類
public class Extra extends AbstractExtra{
public Extra(Base base) {
super(base);
}
public void extraOperate(){
System.out.println("Extra extraOperate ...");
base.operate();
}
}
Main方法及測試類
public static void main(String[] args) {
Base base = new BaseImpl();
AbstractExtra extra = new Extra(base);
extra.extraOperate();
}
Extra extraOperate ...
Base operate ...
裝飾(decorator)模式
定義:指在不改變現(xiàn)有對象結(jié)構(gòu)的情況下,動態(tài)地給該對象增加一些職責(zé)(即增加其額外功能)
優(yōu)點(diǎn):
1.采用裝飾模式擴(kuò)展對象的功能比采用繼承方式更加靈活涛酗。
2.可以設(shè)計(jì)出多個(gè)不同的具體裝飾類铡原,創(chuàng)造出多個(gè)不同行為的組合。
缺點(diǎn):
1.裝飾模式增加了許多子類商叹,如果過度使用會使程序變得很復(fù)雜
//component通用接口
public interface Component {
void operation();
}
//接口的實(shí)現(xiàn)類
public class CurrentComponent implements Component{
public CurrentComponent() {
System.out.println("創(chuàng)建組件");
}
public void operation() {
System.out.println("組件的通用操作...");
}
}
//裝飾模式的抽象類
public abstract class Decorator implements Component{
private Component component;
public Decorator(Component component) {
this.component = component;
}
abstract void incrementFunction();
public void operation() {
component.operation();
}
}
//裝飾模式的具體實(shí)現(xiàn)
public class IncrementAComponent extends Decorator{
public IncrementAComponent(Component component) {
super(component);
}
void incrementFunction() {
System.out.println("裝飾器A的附加操作...");
}
}
Main函數(shù)及輸出結(jié)果:
public static void main(String[] args) {
Component current = new CurrentComponent();
current.operation();
System.out.println("----裝飾過后------");
Component incrementA = new IncrementAComponent(current);
incrementA.operation();
((IncrementAComponent) incrementA).incrementFunction();
}
創(chuàng)建組件
組件的通用操作...
----裝飾過后------
組件的通用操作...
裝飾器A的附加操作...
外觀模式
外觀(Facade)模式的結(jié)構(gòu)比較簡單燕刻,主要是定義了一個(gè)高層接口。它包含了對各個(gè)子系統(tǒng)的引用剖笙,客戶端可以通過它訪問各個(gè)子系統(tǒng)的功能÷严矗現(xiàn)在來分析其基本結(jié)構(gòu)和實(shí)現(xiàn)方法。
//外觀類
public class Facade {
private SubSystem1 system1 = new SubSystem1();
private SubSystem2 system2 = new SubSystem2();
public void method(){
system1.method();
system2.method();
}
}
//子系統(tǒng)1
public class SubSystem1 {
public void method(){
System.out.println("SubSystem1 method ...");
}
}
//子系統(tǒng)2
public class SubSystem1 {
public void method(){
System.out.println("SubSystem2 method ...");
}
}
Main方法及輸出結(jié)果:
public static void main(String[] args) {
Facade facade = new Facade();
facade.method();
}
SubSystem1 method ...
SubSystem2 method ...
享元模式
運(yùn)用共享技術(shù)來有効地支持大量細(xì)粒度對象的復(fù)用弥咪。它通過共享已經(jīng)存在的又橡來大幅度減少需要?jiǎng)?chuàng)建的對象數(shù)量过蹂、避免大量相似類的開銷,從而提高系統(tǒng)資源的利用率酪夷。
字符串常量池
自動裝箱 -128-127
//非享元對象
public class UnFlyweight {
private String info;
public UnFlyweight(String info) {
this.info = info;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
//享元接口
public interface Flyweight {
void operation(UnFlyweight unFlyweight);
}
//享元接口的具體實(shí)現(xiàn)
public class ConcreteFlyweight implements Flyweight{
private String key;
public ConcreteFlyweight(String key) {
this.key = key;
System.out.println("ConcreteFlyweight create " + key);
}
@Override
public void operation(UnFlyweight unFlyweight) {
System.out.print("ConcreteFlyweight key is "+key);
System.out.println(". ConcreteFlyweight operation with unFlyweight["+unFlyweight.getInfo()+"]");
}
}
//享元工廠
public class FlyweightFactory {
private HashMap<String, Flyweight> flyweights=new HashMap<>();
public Flyweight getFlyweight(String key) {
Flyweight flyweight = flyweights.get(key);
if(flyweight!=null) {
System.out.println("Flyweight key "+key+" is already exist.");
} else {
flyweight=new ConcreteFlyweight(key);
flyweights.put(key, flyweight);
}
return flyweight;
}
}
Main方法及輸出結(jié)果:
public static void main(String[] args) {
FlyweightFactory factory = new FlyweightFactory();
Flyweight f01=factory.getFlyweight("a");
Flyweight f02=factory.getFlyweight("a");
f01.operation(new UnFlyweight("1"));
f02.operation(new UnFlyweight("2"));
}
ConcreteFlyweight create a
Flyweight key a is already exist.
ConcreteFlyweight key is a. ConcreteFlyweight operation with unFlyweight[1]
ConcreteFlyweight key is a. ConcreteFlyweight operation with unFlyweight[2]
組合模式
有時(shí)又叫作部分-整體模式榴啸,它是一種將對象組合成樹狀的層次結(jié)構(gòu)的模式,用來表示“部分-整體”的關(guān)系晚岭,使用戶對單個(gè)對象和組合對象具有一致的訪問性鸥印。
//組件接口
public interface MyComponent {
void add(MyComponent c);
void remove(MyComponent c);
MyComponent getChild(int i);
void operation();
}
//葉子實(shí)現(xiàn)接口
public class Leaf implements MyComponent{
private String name;
public Leaf(String name)
{
this.name=name;
}
public void add(MyComponent c){ }
public void remove(MyComponent c){ }
public MyComponent getChild(int i) {
return null;
}
public void operation() {
System.out.println("Leaf "+name+" is here");
}
}
//樹枝實(shí)現(xiàn)接口
public class Composite implements MyComponent{
private ArrayList<MyComponent> children=new ArrayList<MyComponent>();
public void add(MyComponent c){
children.add(c);
}
public void remove(MyComponent c){
children.remove(c);
}
public MyComponent getChild(int i) {
return children.get(i);
}
public void operation() {
for (MyComponent c : children) {
c.operation();
}
}
}
Main方法及輸出結(jié)果:
public static void main(String[] args) {
MyComponent c0=new Composite();
MyComponent c1=new Composite();
MyComponent leaf1=new Leaf("1");
MyComponent leaf2=new Leaf("2");
MyComponent leaf3=new Leaf("3");
c0.add(leaf1);
c0.add(c1);
c1.add(leaf2);
c1.add(leaf3);
c0.operation();
}
Leaf 1 is here
Leaf 2 is here
Leaf 3 is here