分类 设计模式 下的文章

The State Pattern is a behavioral design pattern that allows an object to alter(改变) its behavior when its internal(内部) state changes. This pattern is used to encapsulate(封装) varying(变化) behavior for the same routine(例行程序) based on an object's state object. It's essentially(本质上) a way to model state transitions(过渡) in an object-oriented(面向对象) way.

The Iterator Pattern is a design pattern used in software development to provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. It falls under the category of behavioral patterns, as it is used to manage algorithms, relationships, and responsibilities between objects.
迭代器模式(Iterator Pattern)是软件开发中使用的一种设计模式,它提供了一种在不暴露底层表示的情况下按顺序访问聚合对象元素的方法。它属于行为模式的范畴,因为它用于管理对象之间的算法、关系和责任。

The Template Method Pattern is a behavioral design pattern that defines the program skeleton of an algorithm in a method, deferring some steps to subclasses. It lets one or more steps of the algorithm vary by letting subclasses redefine certain steps of the algorithm without changing the algorithm's structure. This pattern is a fundamental technique in software engineering for reusing code and is particularly useful in situations where the overall structure of an operation is fixed, but the individual steps are flexible and subject to change.
模板方法模式(Template Method Pattern)是一种行为设计模式,它在一个方法中定义了算法的程序骨架,将某些步骤推迟到子类中。它允许子类在不改变算法结构的情况下重新定义算法的某些步骤,从而使算法的一个或多个步骤发生变化。这种模式是软件工程中重用代码的基本技术,尤其适用于操作的整体结构固定,但各个步骤灵活多变的情况。

Simple Factory Example

// Simple Factory
class AnimalFactory {
    public Animal getAnimal(String type) {
        if ("Dog".equalsIgnoreCase(type)) {
            return new Dog();
        } else if ("Cat".equalsIgnoreCase(type)) {
            return new Cat();
        }
        return null;
    }
}

interface Animal {
    void speak();
}

class Dog implements Animal {
    public void speak() {
        System.out.println("Woof");
    }
}

class Cat implements Animal {
    public void speak() {
        System.out.println("Meow");
    }
}

// Client code
public class Main {
    public static void main(String[] args) {
        AnimalFactory factory = new AnimalFactory();
        Animal myDog = factory.getAnimal("Dog");
        myDog.speak(); // Outputs: Woof
    }
}

The Command Pattern is a behavioral design pattern that turns a request into a stand-alone object. This object contains all information about the request. This transformation allows you to parameterize methods with different requests, delay or queue a request's execution, and support undoable operations. It's a part of the larger family of design patterns often utilized in software development for organizing code in a way that is both flexible and scalable.
命令模式是一种行为设计模式,它将请求转化为一个独立的对象。该对象包含请求的所有信息。通过这种转换,您可以为具有不同请求的方法设置参数,延迟或排队执行请求,并支持可撤消(undo)的操作。它是软件开发中常用的大型设计模式系列的一部分,用于以灵活和可扩展的方式组织代码。

如何定义一个设计模式呢?为何不能定义一个新的设计模式呢?难道就只有 GoF 中所说的 23 种设计模式吗?

Summary

The Chain of Responsibility pattern is a design pattern used in software development. It's a part of the behavioral design patterns, which are concerned with algorithms and the assignment of responsibilities between objects.

The Factory Pattern is a creational design pattern used in software development. It provides a way to encapsulate object creation without specifying the exact class of object that will be created.
工厂模式是一种用于软件开发的创建设计模式。它提供了一种封装对象创建的方法,而无需指定将要创建的对象的具体类别。

The Decorator Pattern is another fundamental design pattern used in software development, especially useful for adding new functionalities to objects dynamically. It provides a flexible alternative to subclassing for extending functionality.
装饰者模式(Decorator Pattern)是软件开发中使用的另一种基本设计模式,尤其适用于为对象动态添加新功能。它为扩展功能提供了一种灵活的子类化替代方案。

The Observer Pattern is a widely used design pattern in software engineering. It's particularly useful for creating a system where an object (the subject) needs to notify a list of observers about any state changes.
观察者模式是软件工程中广泛使用的一种设计模式。它特别适用于创建一个系统,在这个系统中,一个对象(主体)需要将任何状态变化通知一系列观察者。

策略模式定义了一个算法族,分别封装起来,使得它们之间可以互相变换。策略让算法的变化独立于使用它的客户。