在软件开发的世界中,有一个众所周知的词汇——设计模式。设计模式是一种解决特定问题的优雅方案,代码让人看起来就有种自然的感觉。现在,我们就来轻松、幽默地了解一下这些神秘的设计模式吧!
创建型(Creational):生孩子还是领养?这里有五种方法!
1、单例模式(Singleton):只要一个宝贝!
想象一下,你的家里只能有一台电视机。这台电视机就像单例模式中的唯一实例。Java代码示例:
public class Television {
private static Television instance;
private Television() {
}
public static Television getInstance() {
if (instance == null) {
instance = new Television();
}
return instance;
}
}
2、工厂方法模式(Factory Method):专业的孩子制造工厂!
每个孩子都是独一无二的,工厂方法模式就像一个专业的孩子制造工厂,可以根据需求生产出不同类型的孩子。Java代码示例:
public interface Child {
void play();
}
public class NaughtyChild implements Child {
public void play() {
System.out.println("I'm a naughty child!");
}
}
public class QuietChild implements Child {
public void play() {
System.out.println("I'm a quiet child.");
}
}
public abstract class ChildFactory {
public abstract Child createChild();
}
3、抽象工厂模式(Abstract Factory):万能孩子制造厂!
抽象工厂模式就像一个万能孩子制造厂,不仅可以生产孩子,还能生产他们的玩具、衣服等。Java代码示例:
public interface Toy {
void playWith();
}
public class CarToy implements Toy {
public void playWith() {
System.out.println("Vroom, vroom!");
}
}
public class DollToy implements Toy {
public void playWith() {
System.out.println("Let's have a tea party!");
}
}
public abstract class AbstractChildFactory {
public abstract Child createChild();
public abstract Toy createToy();
}
4、建造者模式(Builder):DIY你的孩子!
建造者模式就像用积木搭建玩具,你可以自由搭配各种属性,组成不同的孩子。Java代码示例:
public class ChildBuilder {
private String name;
private int age;
private String hobby;
public ChildBuilder setName(String name) {
this.name = name;
return this;
}
public ChildBuilder setAge(int age) {
this.age = age;
return this;
}
public ChildBuilder setHobby(String hobby) {
this.hobby = hobby;
return this;
}
public Child build() {
return new Child(name, age, hobby);
}
}
5、原型模式(Prototype):克隆孩子,简单快捷!
想要快速复制一个孩子?原型模式就像克隆技术,让你轻松复制孩子。Java代码示例:
public class Child implements Cloneable {
private String name;
public Child(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
结构型(Structural):孩子们的组织秘籍!
1、适配器模式(Adapter):兼容新老玩具!
适配器模式就像一个万能插头,让新旧玩具可以共用。Java代码示例:
public interface OldToy {
void oldPlay();
}
public interface NewToy {
void newPlay();
}
public class ToyAdapter implements NewToy {
private OldToy oldToy;
public ToyAdapter(OldToy oldToy) {
this.oldToy = oldToy;
}
public void newPlay() {
oldToy.oldPlay();
}
}
2、桥接模式(Bridge):孩子与玩具的多样组合!
桥接模式就像拼图游戏,孩子和玩具可以任意组合。Java代码示例:
public interface Child {
void play(Toy toy);
}
public interface Toy {
void playWith();
}
public class Boy implements Child {
public void play(Toy toy) {
System.out.println("I'm a boy, and I'm playing with:");
toy.playWith();
}
}
public class Girl implements Child {
public void play(Toy toy) {
System.out.println("I'm a girl, and I'm playing with:");
toy.playWith();
}
}
3、组合模式(Composite):组织孩子们的团队!
组合模式就像树形结构,可以轻松地组织孩子们的团队。Java代码示例:
public abstract class ChildComponent {
public void add(ChildComponent component) {
throw new UnsupportedOperationException();
}
public void remove(ChildComponent component) {
throw new UnsupportedOperationException();
}
public ChildComponent getChild(int index) {
throw new UnsupportedOperationException();
}
public void play() {
throw new UnsupportedOperationException();
}
}
public class ChildGroup extends ChildComponent {
private List<ChildComponent> components = new ArrayList<>();
@Override
public void add(ChildComponent component) {
components.add(component);
}
@Override
public void remove(ChildComponent component) {
components.remove(component);
}
@Override
public ChildComponent getChild(int index) {
return components.get(index);
}
@Override
public void play() {
for (ChildComponent component : components) {
component.play();
}
}
}
行为型(Behavioral):孩子们的行为规范!
1、观察者模式(Observer):妈妈总是知道!
观察者模式就像是妈妈总是知道孩子们在做什么。当孩子们的行为发生变化时,妈妈会立即得知。Java代码示例:
public interface Observer {
void update(String message);
}
public interface Subject {
void registerObserver(Observer observer);
void removeObserver(Observer observer);
void notifyObservers();
}
public class Child implements Subject {
private List<Observer> observers;
private String activity;
public Child() {
observers = new ArrayList<>();
}
public void setActivity(String activity) {
this.activity = activity;
notifyObservers();
}
@Override
public void registerObserver(Observer observer) {
observers.add(observer);
}
@Override
public void removeObserver(Observer observer) {
observers.remove(observer);
}
@Override
public void notifyObservers() {
for (Observer observer : observers) {
observer.update(activity);
}
}
}
public class Mom implements Observer {
@Override
public void update(String message) {
System.out.println("Mom knows that the child is: " + message);
}
}
2、策略模式(Strategy):适应不同的环境!
策略模式就像孩子们会根据不同的环境选择不同的玩法。Java代码示例:
public interface PlayStrategy {
void play();
}
public class IndoorPlayStrategy implements PlayStrategy {
public void play() {
System.out.println("Playing indoors!");
}
}
public class OutdoorPlayStrategy implements PlayStrategy {
public void play() {
System.out.println("Playing outdoors!");
}
}
public class Child {
private PlayStrategy playStrategy;
public void setPlayStrategy(PlayStrategy playStrategy) {
this.playStrategy = playStrategy;
}
public void play() {
playStrategy.play();
}
}
3、责任链模式(Chain of Responsibility):家庭责任制!
责任链模式就像家庭责任制,孩子们可以按照家庭规矩来处理问题。Java代码示例:
public abstract class Handler {
protected Handler nextHandler;
public void setNextHandler(Handler nextHandler) {
this.nextHandler = nextHandler;
}
public abstract void handleRequest(String request);
}
public class Father extends Handler {
@Override
public void handleRequest(String request) {
if ("homework".equals(request)) {
System.out.println("Father: I'll help with the homework.");
} else if (nextHandler != null) {
nextHandler.handleRequest(request);
}
}
}
public class Mother extends Handler {
@Override
public void handleRequest(String request) {
if ("housework".equals(request)) {
System.out.println("Mother: I'll help with the housework.");
} else if (nextHandler != null) {
nextHandler.handleRequest(request);
}
}
}
以上就是创建型、结构型、行为型设计模式的概览,希望通过这些生动、幽默的例子能让你对设计模式有更深入的了解。设计模式的运用能让你的代码更加优雅、易读,让软件开发变得更加轻松愉快!
标签:play,String,void,概览,public,Child,设计模式,class,结构型 From: https://blog.51cto.com/u_13616584/6237835