设计模式
来源:互联网 发布:剑三成女捏脸数据 编辑:程序博客网 时间:2024/06/13 05:02
/** * 内部类单列模式 */public class A { private A(){}; private static class B{ public static A a = new A(); } private static A getInstante(){ return B.a; }}
//------------------------------------------------------工厂模式------------------------------------------------------------------------------
package com.aim.factorytest;/** * 类名称:com.aim.factorytest * 类描述: 多方法工厂(常用) * 创建人: 张国胜 * 创建时间:2016/12/19 16:32 * 修改人: * 修改时间 : * * @version V1.0 */public class FactoryJava { public static void main(String[] args){ ICpu iCpu = new CpuFactory().ICpuIcpucreatOther(); iCpu.decs(); }}class Inter implements ICpu{ @Override public void decs() { System.out.printf("我是英特尔cup"); }}class Aple implements ICpu{ @Override public void decs() { System.out.printf("我是苹果cpu"); }}class Other implements ICpu{ @Override public void decs() { System.out.printf("我是乱七八糟杂牌cpu"); }}interface ICpu{ void decs();}class CpuFactory{ public static ICpu IcpuCreatInter(){ return new Inter(); } public static ICpu IcpucreatApple(){ return new Aple(); } public static ICpu ICpuIcpucreatOther(){ return new Other(); }}//--------------------------------------------------观察者模式---------------------------------------------
/** * 类名称:com.aim.factorytest.com.aim.observer * 类描述: 观察者模式,妖艳贱货是被观察者,老王跟老李是观察者 * 被观察者发出一条消息然后观察者做出相应的处理 * 创建人: 张国胜 * 创建时间:2016/12/19 17:02 * 修改人: * 修改时间 : * * @version V1.0 */public class Obsercer{ public static void main(String[] args){ LaoWan laoWan = new LaoWan(); LaoLi laoLi = new LaoLi(); YaoyanJianHuo yaoyanJianHuo = new YaoyanJianHuo(); //老王,老李都是妖艳贱货的爱慕者,都到她那留了联系方式 yaoyanJianHuo.addPerson(laoWan); yaoyanJianHuo.addPerson(laoLi); yaoyanJianHuo.infoMsg(); }}class LaoWan implements Person{ private String name = "老王"; public LaoWan(){ } @Override public void getMessage(String msg) { System.out.printf(name+"接到妖艳贱货的消息:"+msg); }}class YaoyanJianHuo{ private List<Person> list = new ArrayList<>(); public void addPerson(Person person){ list.add(person); } public void infoMsg(){ for(Person person:list){ person.getMessage("我老公今晚不在家,谁先到就会很嗨哦\n"); } }}class LaoLi implements Person{ private String name = "老李"; public LaoLi() { } @Override public void getMessage(String msg) { System.out.printf(name+"接到妖艳贱货的消息:"+msg); }}interface Person{ void getMessage(String msg);}
//--------------------------------------------------------------适配器模式-----------------------------------------------
public class Test { public static void main(String[] args) { Phone phone = new Phone(); VoltageAdapter adapter = new VoltageAdapter(); phone.setAdapter(adapter); phone.charge(); }}// 手机类class Phone { public static final int V = 220;// 正常电压220v,是一个常量 private VoltageAdapter adapter; // 充电 public void charge() { adapter.changeVoltage(); } public void setAdapter(VoltageAdapter adapter) { this.adapter = adapter; }}// 变压器class VoltageAdapter { // 改变电压的功能 public void changeVoltage() { System.out.println("正在充电..."); System.out.println("原始电压:" + Phone.V + "V"); System.out.println("经过变压器转换之后的电压:" + (Phone.V - 200) + "V"); }}
//-------------------------------------------------------------------代理模式-----------------------------------------------------------------------------public class AdapterModel {
public static void main(String[] args) {
//有人让老李吃苹果
System.out.println("老李吃个苹果吧");
DaiLiPersonLaoWan daiLiPersonLaoWan = new DaiLiPersonLaoWan(new WeiTuoPersonLaoli());
daiLiPersonLaoWan.eat();
}
}
//代理事件
interface DailiEvent{
void eat();//吃东西
}
//代理人老王
class DaiLiPersonLaoWan implements DailiEvent{
private DailiEvent dailiEvent;
public DaiLiPersonLaoWan(DailiEvent dailiEvent){
this.dailiEvent = dailiEvent;
}
public void eat() {
// TODO Auto-generated method stub
dailiEvent.eat();
System.out.println("代理人老王:吃完了");
}
}
//委托人老李
class WeiTuoPersonLaoli implements DailiEvent{
public void eat() {
// TODO Auto-generated method stub
System.out.println("委托人老李:不想吃,老王你来代我吃了它");
}
}
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式~~~~~~~~~~
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- 设计模式
- python交换两个变量的值
- POJ 3109 Inner Vertices 已翻译
- 线程和进程
- 161216
- [嵌入式]ARM Cortex-A8体系结构
- 设计模式
- 如何成为一名JAVA(高级)工程师
- PAT---B1012 数字分类
- 【33.10%】【codeforces 604C】Alternative Thinking
- mysql-5.6.17-win32免安装版配置
- 从本地服务器到腾讯云服务器的部署
- 在IDEA中使用ANTLR4教程
- 网络爬虫之Beautifulsoup入门(二)
- C++ Primer 课后练习10.11,10.12,10.13