Java设计模式

来源:互联网 发布:发型知乎女生短发 编辑:程序博客网 时间:2024/05/24 03:01
Java设计模式之抽象类与适配器模式


抽象:抽象是处理对象复杂性的一种技术,它只关注对象的主要细节而忽略了对象的次要细节。


抽象类:
(1)类和方法都要被关键字abstract去修饰;
(2)抽象的方法不用去实现而只要声明一下就可以了;
(3)抽象类不可以去实例化,也就是不能去new;
(4)一个类中如果含有抽象方法,那么这个类就必须被声明为抽象类,继承它的子类必须去覆写这个抽象方法才可以被实例化。


定义的格式:
abstract class 类名 {
//声明数据成员
//抽象方法
abstract 返回值类型 方法名(参数1, 参数2.....); //此方法为抽象方法,所以没有实现
//一般方法
}
在抽象类中,既可以有抽象方法也可以有一般方法,不同的是抽象方法必须被子类去覆写。


<Example>


package org.alan.mode;


abstract class Person {


private String name;


private int age;


public Person(String name, int age) {
this.setName(name);
this.setAge(age);
}


public int getAge() {
return age;
}


public void setAge(int age) {
this.age = age;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public abstract String getSay();


public void say() {
System.out.println(this.getSay());
}
}


class Student extends Person {


private float score;


public Student(String name, int age, float score) {
super(name, age);
this.setScore(score);
}


public float getScore() {
return score;
}


public void setScore(float score) {
this.score = score;
}


public String getSay() {
return "I am a Student. My name is " + super.getName() + ", age is "
+ super.getAge() + ", score is " + this.getScore();
}
}


public class Demo {
public static void main(String[] args) {
Person person = new Student("Alan", 24, 95);
person.say();
}
}


  
接口:
在java中只允许单继承,但是在实际问题中往往都需要多继承,java引入了接口这一概念(一个类可以实现多个接口)。
由于接口中都是抽象方法,那么我们在实现接口的时候就必须全部覆写这些方法。假如有一个类,这个类只想覆写一部份方法怎么办?可在接口与这个类中间可以加一个抽象类,抽象类去覆写接口中的全部方法,而那个类去继承这个抽象类,根据需要覆写抽象类中的方法。(简单的适配器模式)


<Example>


package org.alan.mode;


interface Eat {


public void eatBread();


public void eatApple();


public void eatBanana();


}


abstract class PersonEat implements Eat {


public void eatBread() {


}


public void eatApple() {
}


public void eatBanana() {
}


}


class Person extends PersonEat {


public void eatBanana() {
System.out.println("I am eating Banana.");
}


public void eatApple() {
System.out.println("I am eating apple.");
}


}


public class Demo {


public static void main(String[] args) {
Person person = new Person();
person.eatBanana();
person.eatApple();
}


}






Java设计模式之单态模式与简单工厂模式


单态模式(Singleton模式):Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在。在项目的很多地方都会用到它,比如说数据库的链接。使用Singleton的好处还在于可以节省内存,因为它限制了实例的个数,有利于Java垃圾回收(garbage collection)。


<Example>


package org.alan.mode;


class Single {


private Single() {
}


private static final Single single = new Single();


public static Single getSingleInstance() {
return single;
}


public void Say() {
System.out.println("I am saying!");
}


}


public class Demo {


public static void main(String[] args) {
Single single = Single.getSingleInstance();
single.Say();
}


}






简单工厂模式:简单工厂模式又叫静态工厂模式,顾名思义,它是用来实例化目标类的静态类。


<Example>


package org.alan.mode;


interface Car {


public void run();


public void stop();


}


class Benz implements Car {


public void run() {
System.out.println("Benz is running.....");
}


public void stop() {
System.out.println("Benz has been stopped!");
}


}


class Ford implements Car {


public void run() {
System.out.println("Ford is running.....");
}


public void stop() {
System.out.println("Ford has been stopped!");
}


}


class Factory {


public static Car getCarInstance(String type) {
Car car = null;
try {
car = (Car) Class.forName("org.alan.mode." + type).newInstance(); // 反射机制
} catch (Exception e) {
e.printStackTrace();
}
return car;
}


}


public class Demo {


public static void main(String[] args) {
Car car = Factory.getCarInstance("Ford");
if (car != null) {
car.run();
car.stop();
} else {
System.out.println("造不了这种汽车!!!");
}
}


}




Java设计模式之工厂方法模式
工厂方法模式:工厂方法模式和简单工厂模式在结构上的不同是很明显的。工厂方法模式的核心是一个抽象工厂类,而简单工厂模式把核心放在一个具体类上。工厂方法模式可以允许很多具体工厂类从抽象工厂类中将创建行为继承下来,从而可以成为多个简单工厂模式的综合,进而推广了简单工厂模式。此模式涉及到抽象工厂角色(汽车厂),具体工厂角色(具体的汽车车间),抽象产品角色(汽车)以及具体产品角色(私车、公交巴士)。


<Example>


package org.alan.mode;


interface Car {


public void run();


public void stop();


}


class Benz implements Car {


public void run() {
System.out.println("Benz is running.....");
}


public void stop() {
System.out.println("Benz has been stopped!");
}


}


class Ford implements Car {


public void run() {
System.out.println("Ford is running.....");
}


public void stop() {
System.out.println("Ford has been stopped!");
}


}


class BigBus implements Car {


public void run() {
System.out.println("BigBus is running.....");
}


public void stop() {
System.out.println("BigBus has been stopped!");
}


}


class MiniBus implements Car {


public void run() {
System.out.println("MiniBus is running.....");
}


public void stop() {
System.out.println("MiniBus has been stopped!");
}


}


interface AbstractFactory {


}


class CarFactory implements AbstractFactory {


public Car getCarInstance(String type) {
Car car = null;
try {
car = (Car) Class.forName("org.alan.mode." + type).newInstance(); // 反射机制
} catch (Exception e) {
e.printStackTrace();
}
return car;
}


}


class BusFactory implements AbstractFactory {


public Car getBusInstance(String type) {
Car car = null;
try {
car = (Car) Class.forName("org.alan.mode." + type).newInstance(); // 反射机制
} catch (Exception e) {
e.printStackTrace();
}
return car;
}


}


public class Demo {


public static void main(String[] args) {
CarFactory carFactory = new CarFactory();
Car car = null;
car = carFactory.getCarInstance("Ford");
if (car != null) {
car.run();
car.stop();
} else {
System.out.println("造不了这种汽车!!!");
}
}


}




Java设计模式之抽象工厂模式


抽象工厂模式:当每个抽象产品都有多于一个的具体子类的时候,工厂角色怎么知道实例化哪一个子类呢?比如每个抽象产品角色都有两个具体产品。抽象工厂模式提供两个具体工厂角色,分别对应于这两个具体产品角色,每一个具体工厂角色只负责某一个产品角色的实例化。每一个模式都是针对一定问题的解决方案,工厂方法模式针对的是一个产品等级结构,而抽象工厂模式针对的是多个产品等级结构。


<Example>


package org.alan.mode;


interface Person {


public void eat();


public void talk();


}


class Man implements Person {


public void eat() {
System.out.println("Man is eating.....");
}


public void talk() {
System.out.println("Man is talking.....");
}


}


class Woman implements Person {


public void eat() {
System.out.println("Woman is eating.....");
}


public void talk() {
System.out.println("Woman is talking.....");
}


}


interface Animal {


public void eat();


public void sleep();


}


class Bull implements Animal {


public void eat() {
System.out.println("Bull is eating.....");
}


public void sleep() {
System.out.println("Bull is sleeping.....");
}


}


class Cow implements Animal {


public void eat() {
System.out.println("Cow is eating.....");
}


public void sleep() {
System.out.println("Cow is sleeping.....");
}


}


// NvwaFactory--->女娲
interface NvwaFactory {


public Person getPerson(String type);


public Animal getAnimal(String type);


}


// 阳绳--->用来造男人和雄性动物(Bull)
class YangSheng implements NvwaFactory {


Man man = null;


Bull bull = null;


public Man getPerson(String type) {
try {
man = (Man) Class.forName("org.alan.mode." + type).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return man;
}


public Bull getAnimal(String type) {
try {
bull = (Bull) Class.forName("org.alan.mode." + type).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return bull;
}


}


// 阴绳--->用来造女人和雌性动物(Cow)
class YinSheng implements NvwaFactory {


Woman woman = null;


Cow cow = null;


public Woman getPerson(String type) {
try {
woman = (Woman) Class.forName("org.alan.mode." + type)
.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return woman;
}


public Cow getAnimal(String type) {
try {
cow = (Cow) Class.forName("org.alan.mode." + type).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return cow;
}
}


public class Demo {


public static void main(String[] args) {
YangSheng yangSheng = new YangSheng();
YinSheng yinSheng = new YinSheng();
Person man = yangSheng.getPerson("Man");
Person woman = yinSheng.getPerson("Woman");
Animal bull = yangSheng.getAnimal("Bull");
Animal cow = yinSheng.getAnimal("Cow");
bull.eat();
bull.sleep();
cow.eat();
cow.sleep();
man.eat();
man.talk();
woman.talk();
woman.eat();
}


}




Java设计模式之代理模式
代理(Proxy)模式:所谓的代理就是一个人或者一个机构代替另一个人或者另一个机构去做一些事情(类似于中介或者代理商)。
代理模式的结构
    代理模式所涉及的角色有:
        抽象主题角色:声明了真实主题与代理主题的共同接口(共有的特性);
代理主题角色:含有对真实主题角色的引用(操作真实主体对象),代理角色通常在将客户端调用传递给真实主题对象的之前或者之后都会执行某些操作(方法),而不是只单纯的返回真实的对象。
真实主题角色:定义了代理角色所代表的真实对象。


<Example>


package org.alan.mode;


//Subject抽象类 (真实主题与代理主题共有的特性)
abstract class Subject {


public abstract void request();


}


// RealSubject类 ->真实主题角色(被代理的类)
class RealSubject extends Subject {


public void request() {
System.out.println("实现请求操作!!!");
}


}


// ProxySubject类->代理主题角色
class ProxySubject extends Subject {


private RealSubject realSubject = null;


public void request() {


preRequest();


if (realSubject == null) {
realSubject = new RealSubject();
}
realSubject.request();


postRequest();
}


public void preRequest() {
System.out.println("请求前的操作!!!");
}


public void postRequest() {
System.out.println("请求后的操作!!!");
}


}


public class Demo {


public static void main(String[] args) {
Subject subject = new ProxySubject();
subject.request();
}


}




Java设计模式之动态代理
原创粉丝点击