1. 装饰者模式
装饰模式,动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更加灵活,当频繁的继承使我们的代码成一条线的时候,或许就该考虑,怎么样在运行的时候扩展类的属性,或者说是装饰类,而不仅仅是在编译前期直接构思好继承逻辑。
- 如果只有一个
ConcreteComponent
类而没有抽象的Component
类,那么Decorator
类可以是ConcreteComponent
类的子类 - 如果只有一个
ConcreteDecorator
,那么也不需要Decorator
类了
public class Person {
public Person() {
}
private String name;
private int age;
public int getAge() {
return age;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void show() {
System.out.println("this is " + this.name);
}
}
public class Clothes extends Person {
protected Person component;
public void Decorate(Person component) {
this.component = component;
}
@Override
public void show() {
super.show();
if(component != null) {
System.out.println(component.toString());
}
}
}
public class TShirt extends Clothes {
@Override
public void Decorate(Person component) {
super.Decorate(component);
}
@Override
public void show() {
super.show();
System.out.println("这是T恤");
}
}
public class Swearter extends Clothes {
@Override
public void show() {
super.show();
System.out.println("毛衣");
}
}
public class DecoratorClient {
public static void main(String[] args) {
Person guanzihan = new Person();
guanzihan.setName("guanzihan");
guanzihan.setAge(20);
Swearter swearter = new Swearter();
TShirt tShirt = new TShirt();
swearter.Decorate(guanzihan);
tShirt.Decorate(swearter);
tShirt.show();
}
}
2. 代理模式
代理模式为其他对象提供一种代理以控制对这个对象的访问
public interface SendGift {
public void sendCake();
public void sendFruit();
}
public class Pursuit implements SendGift {
Girl mGirl;
public Pursuit(Girl girl) {
mGirl = girl;
}
@Override
public void sendCake() {
System.out.println("追求者送了蛋糕");
}
@Override
public void sendFruit() {
System.out.println("追求者送了水果");
}
}
public class MyProxy implements SendGift {
Pursuit mPursuit;
public MyProxy(Girl girl) {
mPursuit = new Pursuit(girl);
}
@Override
public void sendCake() {
System.out.println("代理送了蛋糕");
}
@Override
public void sendFruit() {
System.out.println("代理送了水果");
}
}
public class Girl {
String name;
public Girl(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class ProxtClient {
public static void main(String[] args) {
Girl girl = new Girl("小美");
MyProxy proxy = new MyProxy(girl);
proxy.sendCake();
proxy.sendFruit();
}
}
3. 工厂方法模式
工厂方法模式定义了一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其他子类
- 简单工厂模式的最大优点在于工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,但是当新需求来的时候违背了开放-封闭原则
- 工厂方法模式把简单的内部逻辑判断移动到了客户端代码中进行。想要加功能,只需要修改客户端
public abstract class LeiFeng {
public abstract void Sweep();
public abstract void Wash();
public abstract void BuyRice();
}
public class Undergraduate extends LeiFeng {
@Override
public void Sweep() {
System.out.println("I'm sweeping the floor");
}
@Override
public void Wash() {
System.out.println("I'm washing the dishes");
}
@Override
public void BuyRice() {
System.out.println("I'm buying rice");
}
}
public interface LeiFengFactory {
LeiFeng createLeiFeng();
}
public class UndergraduateFactory implements LeiFengFactory {
@Override
public LeiFeng createLeiFeng() {
return new Undergraduate();
}
}
public class FactoryMethodClient {
public static void main(String[] args) {
LeiFengFactory leiFengFactory = new UndergraduateFactory();
LeiFeng leiFeng = leiFengFactory.createLeiFeng();
leiFeng.BuyRice();
leiFeng.Sweep();
leiFeng.Wash();
}
}
4. 原型模式
原型模式,用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象
- 原型模式其实就是从一个对象再创建另一个可定制的对象,而且不需要知道任何创建的细节
public class Resume implements Cloneable {
private String name;
private int age;
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void Display() {
System.out.println("My name is " + name);
System.out.println("My age is " + age);
}
public Object Clone() {
try {
return this.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
return null;
}
}
}
public class ResumeClient {
public static void main(String[] args) {
Resume resume = new Resume();
resume.setAge(20);
resume.setName("官子寒");
resume.Display();
Resume resumeClone = (Resume) resume.Clone();
resumeClone.Display();
resumeClone.setName("胡梦萱");
resumeClone.Display();
resume.Display();
}
}