适配器模式是一种补救模式,将一个类的接口转换成客户希望的另外一个接口。Adapter模式使原本由于接口不兼容而不能一起工作的类可以一起工作。是包装模式(对类或者对象进行包装)的一种,分为类适配器和对象适配器,是从实现层面上划分。
(1).我们规定一个情景,A公司要收购B公司,但是他们有两套接口不是很一致的人员管理系统,如下:
interface MyEmployee{
public String getName();
public void setName(String name);
public String getPosition();
public void setPosition( String position );
public String getAddress();
public void setAddress( String address );
public String getAge();
public void setAge( String age );
}
实现类如下,给出实现,只是体现适配器能极大程度忽略内部实现细节。
class EmployeeA implements MyEmployee {//实现MyEmployee接口
private String name;
private String position;
private String address;
private String age;
//应该注意@override注释的必要性,能检查是否为有效的覆盖
@Override
public String getName() {
return name;
}
@Override
public void setName(String name) {
this.name = name;
}
//TODO 同样的getter和setter
}
class EmployeeB{
private Map basicInfo;
public Map getBasicInfo() {
return basicInfo;
}
public void setBasicInfo(Map basicInfo) {
this.basicInfo = basicInfo;
}
}
我们采用类适配器来解决这一问题,定义上类适配器应该继承原类,实现目标接口,这样能够达到获得源类的方法,并且采用目标接口的调用形式
class EmployeeAdapter extends EmployeeB implements MyEmployee {
@Override
public String getName() {
return getBasicInfo().get("name");
}
@Override
public void setName(String name) {
Map dic = getBasicInfo();
String key = "name";
dic.remove( key);
dic.put( key , name );
}
}
然后我们就可以利用MyEmployee接口同时操作两个公司的员工,并且可以实现多态需要达到的任何操作。
public class adapterEg {
public static void main ( String [] args ){
MyEmployee b = new EmployeeAdapter();
MyEmployee a = new EmployeeA();
//TODO 一系列操作
}
}
(2).
A LegacyRectangle component's display() method expects to receive "x, y, w, h" parameters.
But the client wants to pass "upper left x and y" and "lower right x and y".
a.without Adaptor pattern
b. with Adaptor patter
https://blog.csdn.net/qq_24451605/article/details/51142715
为对象增加不同侧面的特性,对每一个特性构造子类,通过委派机制增加到对象上
package project1;
//Component 组件的抽象
abstract class Component {
public abstract void print();
}
//Decorator 装饰者
abstract class Decorator extends Component{
private Component component;
Decorator( Component component ){
this.component = component;
}
@Override
public void print(){
component.print();
}
}
//ConcreteComponent 原始组件
class Resume extends Component{
@Override
public void print() {
System.out.println("My Name: llin\n"
+"My Major: Software Engineering"
+"My University: Tianjin University"
);
}
}
//ConcreteComponent 具体装饰组件
class AliDecrator extends Decorator{
AliDecrator(Component component) {
super(component);
}
@Override
public void print() {
super.print();
System.out.println("I want a position at Ali major in Java");
}
}
class TencentDecorator extends Decorator{
TencentDecorator(Component component) {
super(component);
}
@Override
public void print(){
super.print();
System.out.println("I want a position at QQ major in C++");
}
}
public class decoratorEg {
public static void main(String[] args) {
Component resume = new Resume();
Component aliResume = new AliDecrator( resume );
Component QQResume = new TencentDecorator( resume );
aliResume.print();
System.out.println("");
QQResume.print();
}
}
My Name: llin
My Major: Software EngineeringMy University: Tianjin University
I want a position at Ali major in Java
My Name: llin
My Major: Software EngineeringMy University: Tianjin University
I want a position at QQ major in C++
https://blog.csdn.net/qq_24451605/article/details/51146084
为子系统中的一组接口提供一个统一接口。Facade模式定义了一个更高层的接口,使子系统更加容易使用。
Facade门面对象:
Sub Systems子系统角色:
比如我们平时在使用电脑时,桌面上的文件的快捷方式(或者是一个执行文件),我们需要做的操作只是打开和关闭,通常情况下完全不需要考虑用什么打开啊,设置什么参数啊,关闭的时候应该如何销毁资源才能保证程序的数据能够不造成损失….这个可执行文件就可以看做是一个门面。因为它极大地简化了我们的操作,而且我们可以完全忽略和子系统之间的交互,而简单地认为我是在和一个完整的系统进行交互。
下面提供一个打开文件操作的实现来说明一下,门面模式到底是个什么样:
class OpenWord{
boolean isOpen;
String name = "Viewer";
OpenWord(){
isOpen = false;
}
void open(){
System.out.println("The " + name + " is opening....");
isOpen = true;
}
void stop(){
System.out.println("The " + name + " is closing....");
isOpen = false;
}
}
class PrepareFileSystem{
boolean isOpen;
String name = "FileSystem";
PrepareFileSystem(){
isOpen = false;
}
void open1(){
System.out.println("The " + name + " is opening....");
isOpen = true;
}
void stop1(){
System.out.println("The " + name + " is closing....");
isOpen = false;
}
}
class OperateFile{
void showFile ( String name ){
System.out.println("Show the content of the file: " + name);
}
}
public class OpenFileFacade {
//必须是私有成员,否则会将子系统暴露给外部
private OpenWord openWord = new OpenWord();
private PrepareFileSystem prepareFileSystem = new PrepareFileSystem();
private OperateFile operateFile = new OperateFile();
//只是提供一条访问路径,不实现业务逻辑
public void openFile ( String name ){
prepareFileSystem.open1();
openWord.open();
operateFile.showFile( name );
}
public void closeFile ( String name ){
openWord.stop();
prepareFileSystem.stop1();
}
public static void main ( String [] args ){
OpenFileFacade a = new OpenFileFacade();
a.openFile("cookBook");
a.closeFile("cookBook");
}
}
The FileSystem is opening....
The Viewer is opening....
Show the content of the file: cookBook
The Viewer is closing....
The FileSystem is closing....
给东西上色
interface Strategy{ //Strategy抽象策略角色: void operate();
}
class RedPaint implements Strategy{//具体策略角色:
@Override
public void operate() {
System.out.println("paint the code to red!");
}
}
class BluePaint implements Strategy{//具体策略角色:
@Override
public void operate() {
System.out.println("Paint the code to blue!");
}
}
class Context{//Context封装角色:
private Strategy strategy;
public Context( Strategy strategy ){
this.strategy = strategy;
}
public void operate ( ){
strategy.operate();
}
}
public class StrategyTest {
public static void main ( String [] args ) {
Context context = new Context(new BluePaint());
context.operate();
context = new Context ( new RedPaint());
context.operate();
}
}
https://blog.csdn.net/qq_24451605/article/details/51331224
step1();
…
step2();
…
step3();
一般我们做系统时都是照着软件工程的思想,就是先做需求分析,然后概要设计,然后详细设计,然后编码,测试,发布这几个步骤,虽然每个系统都不一样,但是我们的工作流程是不变的,只是步骤中某个细节可能会是不同的,如需求分析,不同的系统功能,需求分析肯定是不同的。在工作流程上是不变,而其细节上更详细的设计是可变的,我们就采用模板方法模式。对于不同的系统都采用这个工作流程这套模板来设计。
设计板块
//就是从需求分析到发布系统的一套设计流程
public abstract class DesignCycle {
public abstract void needAnalysis();
public abstract void conceptualDesign();
public abstract void detailedDesign();
public abstract void coding();
public abstract void testSystem();
public abstract void publishSystem();
public void templateDesignSystem(){
System.out.println("------------开始开发----------");
needAnalysis();
conceptualDesign();
detailedDesign();
coding();
testSystem();
publishSystem();
System.out.println("------------开发完毕-----------");
}
}
支付系统具体实现
public class PaymentSystem extends DesignCycle{
@Override
public void needAnalysis() {
// TODO Auto-generated method stub
System.out.println("支付系统的需求分析");
}
@Override
public void conceptualDesign() {
// TODO Auto-generated method stub
System.out.println("支付系统的概要设计");
}
@Override
public void detailedDesign() {
// TODO Auto-generated method stub
System.out.println("支付系统的详细设计");
}
@Override
public void coding() {
// TODO Auto-generated method stub
System.out.println("支付系统的编码");
}
@Override
public void testSystem() {
// TODO Auto-generated method stub
System.out.println("支付系统的测试");
}
@Override
public void publishSystem() {
// TODO Auto-generated method stub
System.out.println("支付系统的发布");
}
}
物流系统具体实现
//物流系统类
public class LogisticSystem extends DesignCycle{
@Override
public void needAnalysis() {
// TODO Auto-generated method stub
System.out.println("物流系统的需求分析");
}
@Override
public void conceptualDesign() {
// TODO Auto-generated method stub
System.out.println("物流系统的概要设计");
}
@Override
public void detailedDesign() {
// TODO Auto-generated method stub
System.out.println("物流系统的详细设计");
}
@Override
public void coding() {
// TODO Auto-generated method stub
System.out.println("物流系统的编码");
}
@Override
public void testSystem() {
// TODO Auto-generated method stub
System.out.println("物流系统的测试");
}
@Override
public void publishSystem() {
// TODO Auto-generated method stub
System.out.println("物流系统的发布");
}
}
测试及其结果
public class Test {
public static void main(String[] args) {
DesignCycle dc;
dc=new PaymentSystem();
dc.templateDesignSystem();
dc=new LogisticSystem();
dc.templateDesignSystem();
}
}
------------开始开发----------
支付系统的需求分析
支付系统的概要设计
支付系统的详细设计
支付系统的编码
支付系统的测试
支付系统的发布
------------开发完毕-----------
------------开始开发----------
物流系统的需求分析
物流系统的概要设计
物流系统的详细设计
物流系统的编码
物流系统的测试
物流系统的发布
------------开发完毕-----------