【阶段总结】适配器模式&外观模式

本文介绍了适配器模式和外观模式的基本概念及其应用场景,并探讨了面向对象设计中的“最少知识原则”。通过具体代码示例说明如何实现这些模式,并讨论了它们的优势及潜在问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

If you need ,you can make a conlusion whenever you need.
-->
    An Andapter converts an 
interface
    Facade still allows low level access
    Disadvantage of 
" the Principle of Least Knowledge " : too many wrappers
    
" the Principle of Least Knowledge " : Law of Demeter
    A facade simplifiers an 
interface
    Decorator called Adapter 
this   " simple pass through "
    One advantage of Facade: decoupling
    Principle that wasn
' t as easy as it sounded: least knowledge
    A Decorator adds  new  behavior
    Example that violates the Principle of Least Knowledge: System.out.println
    Adapter client uses the target 
interface  
    An Adapter and a Decorator can be said to wrap an object

适配器模式:
    将一个类的接口,转换成客户期望的另一个接口。适配器让原本不兼容的类可以合作无间。

外观模式:
    提供了一个统一的接口,用来访问子系统中的一群接口。外观定义了一个高层接口,让子系统更容易使用。

OO原则:
    只和朋友交谈

Sutra Code as follows
-->

publi 
class  Car  {
    Engine engine;
    
//其他实例变量

    
public Car() {
        
//初始化发动机
    }


    
public void start(Key key) {
        Doors doors 
= new Doors();

        
boolean authorized = key.turns();

        
if (authorized)
        
{
            engine.start();
            updateDashboardDisplay();
            doors.lock();
        }

    }


    
public void updateDashboardDisplay() {
        
//更新显示
    }

}


public   float  getTemp()  {
    
return station.getTemperature();
}


被解耦的客户才是快乐的客户。

public   class  TurkeyAdapter  implements  Duck  {
    Turkey turkey;

    
public TurkeyAdapter(Turkey turkey) {
        
this.turkey = turkey;
    }


    
public void quack() {
        turkey.gobble();
    }


    
public void fly() {
        
for (int i=0; i<5; i++)
        
{
            turkey.fly();
        }

    }

}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值