单例设计模式
单例设计模式:保证程序在内存中只有一个对象(实例),这个实例被整个程序共享.
单例设计模式的两种实现方式:
- 懒汉式:随着类的加载在内存中对象为null,当调用 getInstance 方法时才创建对象(延迟加载)
- 饿汉式:随着类的加载直接创建对象(推荐开发中使用)
单例模式的实现步骤:
保证一个类只有一个实例:实现方式:构造方法私有化
必须要自己创建这个实例:实现方式:在本类中维护一个本类对象(私有,静态)
必须向整个程序提供这个实例:实现方法:对外提供公共的访问方式(getInstance方法,静态)
饿汉式
class Single1{
private static Single1 s = new Single1();
private Single1(){
}
public static Single1 getInstance(){
return s;
}
}
懒汉式:
class Single2{
private Single2(){}
private static Single2 s2 = null;
public static Single2 getInstance(){
if (s2 == null){
s2 = new Single2();
}
return s2;
}
}
工厂模式
简单工厂模式定义:通过工厂降低类与类的耦合,提高灵活性,利于变化.
看下面的示例代码:
interface Fruit{
void eat();//吃水果
}
class Apple implements Fruit{
public void eat(){
System.out.println("吃苹果......");
}
}
class Orange implements Fruit{
public void eat(){
System.out.println("吃橘子......");
}
}
//低耦合,高内聚
//面向抽象编程.面向接口编程
class FactoryDemo {
public static void main(String[] args) {
Fruit apple = new Apple();
apple.eat();
Fruit orange = new Orange();
orange.eat();
}
}
以上代码可能会有问题,如果Apple类或者Orange类出现了变化.可能会影响FactoryDemo类中的使用代码,为了让我们的程序更利于变化,使用工厂类解决这个问题.
interface Fruit{
void eat();//吃水果
}
class Apple implements Fruit{
public void eat(){
System.out.println("吃苹果......");
}
}
class Orange implements Fruit{
public void eat(){
System.out.println("吃橘子......");
}
}
class FruitFactory{
public static Fruit getFruit(String name){
if ("apple".equals(name)){
return new Apple();
}
if ("orange".equals(name)){
return new Orange();
}
if ("banana".equals(name)){
//return new Banana();
}
return new Orange();
}
}
//低耦合,高内聚
//面向抽象编程.面向接口编程
class FactoryDemo {
public static void main(String[] args) {
//Fruit apple = new Apple();
Fruit apple = FruitFactory.getFruit("liulian");
apple.eat();
//Fruit orange = new Orange();
//orange.eat();
}
}
代理模式:
定义:为其他对象提供一种代理,以控制对这个对象的访问.
//代理的主题
interface Subject{
void giveMoney();//要钱的方法(功能)
}
//代理对象
class ProxySubject implements Subject{
private Subject sub;
public ProxySubject(Subject sub){
this.sub = sub;
}
public void before(){
System.out.println("要钱之前的准备工作......");
}
//代理的追账公司的要钱方法
public void giveMoney(){
this.before();
this.sub.giveMoney();
this.after();
}
public void after(){
System.out.println("要钱之后的善后工作.......");
}
}
//被代理对象
class Person implements Subject{
public void giveMoney(){
System.out.println("浪哥,速速还钱!!!");
}
}
class ProxyDemo {
public static void main(String[] args) {
Subject p = new Person();
ProxySubject ps = new ProxySubject(p);
ps.giveMoney();
//Person p1 = new Person();
//p1.giveMoney();
}
}
免费奉送第二个案例,仅供大家参考
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();
}
}
适配器模式(了解)
定义:让原本不能在一起工作的两个接口可以一起工作
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();
}
}
策略模式
定义:把可变的行为抽取出来,形成一个算法簇,这样可以让我们的应用更利于变化
- 在使用抽象类和接口时,我们优先选择接口
//对修改关闭,对扩展开放
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();
}
}
版权声明:本文为weixin_42359442原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。