Book Notes The Observer Pattern: defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically. Design Pattern: Example Publisher 需要提供注册、注销、通知观察者的接口。 WeatherData 是 publisher 的实现类。 // publisher interface public interface Subject { public void registerObserver(Observer o); public void removeObserver(Observer o); public void notifyObservers(); } // publisher implementation public class WeatherData implements Subject { private List<Observer> observers; // 观察者列表 private float temperature; private float humidity; private float pressure; public WeatherData() { observers = new ArrayList<Observer>(); } public void registerObserver(Observer o) { // 注册观察者 observers....

3 min

Book Notes The Decorator Pattern: attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. Decorators have the same supertype as the objects they decorate. You can use one or more decorators to wrap an object. The decorator adds its own behavior before and/or after delegating to the object it decorates to do the rest of the job. Design Pattern: Example 咖啡店饮品案例: Beverage 和 CondimentDecorator 是两个抽象类,Beverage 是基类,CondimentDecorator 是装饰者基类:...

2 min

简单工厂 问题描述 把不同类型的 pizza 的创建过程从 Pizza 类中抽出来封装成 SimplePizzaFactory 类,其他统一的操作放在 Pizza 类中 工厂方法 The Factory Method Pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. 工厂方法的目的是允许一个类将实例化延迟到它的子类。 抽象工厂 The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. 工厂方法和抽象工厂对比

1 min

Book notes The Singleton Pattern ensures a class has only one instance, and provides a global point of access to it. Java 的单例模式实现使用 private constructor,static method combine with static variable。 为多线程应用程序仔细选择合适的单例实现 可以使用 Java 的 enum 来简化单例实现。 example public class Singleton { private static Singleton uniqueInstance; private Singleton() {} // 私有构造函数,防止外部实例化 public static synchronized Singleton getInstance() { // synchronized 关键字确保线程安全 if (uniqueInstance == null) { uniqueInstance = new Singleton(); } return uniqueInstance; } public String getDescription() { return "I'm a thread safe Singleton!...

1 min

Book notes The Command Pattern encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations. command pattern 将发出请求的对象与知道如何执行请求的对象解耦。 Example command interface: public interface Command { public void execute(); } receiver: public class LightOnCommand implements Command { Light light; public LightOnCommand(Light light) { this.light = light; } public void execute() { light.on(); } } public class Light { String location = ""; public Light(String location) { this....

2 min

Book notes The Adapter Pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. object adpater: class adpater: java 不支持多重继承,所以无法使用 class adpater Example 用 java enumeration 实现 iterator 接口,这样在 client code 中可以像使用 iterator 一样使用: public class EnumerationIterator implements Iterator<Object> { Enumeration<?> enumeration; public EnumerationIterator(Enumeration<?> enumeration) { this.enumeration = enumeration; } public boolean hasNext() { // delegate iterator's hasNext() to enumeration hasMoreElements() return enumeration....

1 min

Book notes The Facade Pattern provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher level interface that makes the subsystem easier to use. Example 创建一个家庭影院的 facade class,封装家庭影院的多个子系统,提供统一的接口给客户端使用比如 watchMovie(), endMovie() 等,而不用逐一调用子系统的接口: facade class: public class HomeTheaterFacade { Amplifier amp; Tuner tuner; StreamingPlayer player; CdPlayer cd; Projector projector; TheaterLights lights; Screen screen; PopcornPopper popper; public HomeTheaterFacade(Amplifier amp, Tuner tuner, StreamingPlayer player, Projector projector, Screen screen, TheaterLights lights, PopcornPopper popper) { this....

2 min

Book notes The Template Method Pattern defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure. Key points: A template method defines the steps of an algorithm, deferring to subclasses for the implementation of those steps. The Template Method Pattern gives us an important technique for code reuse The template method’s abstract class may define concrete methods, abstract methods, and hooks....

1 min

https://design-patterns.readthedocs.io/zh-cn/latest/read_uml.html

1 min

Getting Started First-time Git setup /etc/gitconfig: system-wide configuration file. 影响整个系统的 git 配置. git config --system 选项. ~/.gitconfig 或 ~/.config/git/config: user-specific configuration file. 影响当前用户的 git 配置. git config --global 选项. .git/config: repository-specific configuration file. 影响当前仓库的 git 配置. git config --local 选项. 通过下面这个命令可以查看所有的配置, 以及在上述哪个文件中: $ git config --list --show-origin 配置邮箱和姓名, 默认编辑器: $ git config --global user.name "John Doe" $ git config --global user.email johndoe@example.com $ git config --global core.editor vim Git Basics short status git status -s: git status 的简化版....

1 min