设计模式:
- 适配器模式:让原本不能在一起工作的两个接口可以一起工作(比如A和B是两种不同的排插,C和D是两种不同的插座分别对应A和B,一般当排查和插座不对应是排插是无法工作的,而适配器的作用是给无法工作的排插接上可以插入插座的的接口,使之可以正常工作)
interface PowerSourceA{
void insert();
}
class PowerSourceAImpl implements PowerSourceA{
public void insert(){
System.out.println("电源A开始工作......");
}
}
interface PowerSourceB{
void set();
}
class PowerSourceBImpl implements PowerSourceB{
public void set(){
System.out.println("电源B开始工作......");
}
}
//适配器
class Adapter implements PowerSourceA,PowerSourceB{
private PowerSourceA a;//
private PowerSourceB b;//b = null
public Adapter(PowerSourceA a){
this.a = a;
}
public Adapter(PowerSourceB b){
this.b = b;
}
public void insert(){
b.set();
}
public void set(){
a.insert();
}
}
class AdapterDemo {
public static void main(String[] args) {
PowerSourceA a = new PowerSourceAImpl();
//这里买个转换头
Adapter adapterA = new Adapter(a);
fun(adapterA);
PowerSourceB b = new PowerSourceBImpl();
Adapter adapterB = new Adapter(b);
method(adapterB);
/*
PowerSourceA a = new PowerSourceAImpl();
method(a);
PowerSourceB b = new PowerSourceBImpl();
fun(b);
*/
}
public static void method(PowerSourceA a){//这个是中国的插排(插座)
a.insert();
}
public static void fun(PowerSourceB b){//PowerSourceB b = new Adapter(a);
b.set();
}
}
- 代理模式:为其他对象提供一种代理,以控制对这个对象的访问(做代理最典型的是律师,律师接受雇主的代理解决法律纠纷,把雇主的违法行为过滤淡化)
interface IWomanable{
void makeEyesWithMan();//抛媚眼..(泛指某一小部分人)
void happyWithMan();//和男人..........
}
//被代理对象
class PanJinLian implements IWomanable{
public void makeEyesWithMan(){
System.out.println("潘金莲抛媚眼......");
}
public void happyWithMan(){
System.out.println("潘金莲和 man happy......");
}
}
class WangPo implements IWomanable{
private IWomanable iw;
public WangPo(IWomanable iw){
this.iw = iw;
}
public void makeEyesWithMan(){
this.iw.makeEyesWithMan();
}
public void happyWithMan(){
this.iw.happyWithMan();
}
}
class ProxyDemo2 {
public static void main(String[] args) {
IWomanable pjl = new PanJinLian();
WangPo wp = new WangPo(pjl);
wp.makeEyesWithMan();
wp.happyWithMan();
}
}
- 策略模式:把可变的行为抽取出来,形成一个算法簇,这样可以让我们的应用更利于变化,在使用抽象类和接口时,我们优先选择接口
abstract class Cat{
private String name;
public Cat(String name){
this.name = name;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void catchMouse(){
System.out.println("抓老鼠......");
}
}
class BaiCat extends Cat{
public BaiCat(String name){
super(name);
}
public void catchMouse(){
System.out.println("我是" + getName() + "我能抓老鼠......");
}
}
class HeiCat extends Cat{
public HeiCat(String name){
super(name);
}
public void catchMouse(){
System.out.println("我是" + getName() + "我只吃鱼,不能抓老鼠......");
}
}
class CeLueDemo {
public static void main(String[] args) {
BaiCat bc = new BaiCat("白猫");
bc.catchMouse();
HeiCat hc = new HeiCat("黑猫");
hc.catchMouse();
}
}
```
上面的代码中,catchMouse()方法的实现是可能会发生变化的,为了让我们的代码更利于变化,使用策略模式重新设计代码结构.
- 模板方法模式
定义一个功能的骨架(框架),一部分功能是确定的,一部分功能是不确定的.把确定的功能实现,把不确定的功能抽取出来,延迟到子类中实现.
abstract class DaBaoJian{
public void baoJian(){
System.out.println("大哥!您来啦!");
System.out.println("哥!您几位呀!");
System.out.println("哥!来,这边!这个房间!");
System.out.println("哥!要个法器吗?");
if (work()){
System.out.println("那就来个法器吧!");
System.out.println("床啊!床!床啊!床!");
System.out.println(".............");
}else{
System.out.println("我就是来放松的!别整那没用的!!!");
}
}
public abstract boolean work();
}
class Man extends DaBaoJian{
String name;
public Man(String name){
this.name = name;
}
public boolean work(){
System.out.println(name + "说:");
java.util.Random r = new java.util.Random();
return r.nextBoolean();
}
}
class AbstractDemo {
public static void main(String[] args) {
Man m = new Man("白哥");
m.baoJian();
}
}