Classification Creational patterns(创建模式) deal with object creation mechanisms Structural patterns(结构模式 describe how classes and objects can be combined to form larger structures Behavioral patterns(行为模式) identify common communication patterns between objects and realIze these patterns
11 Classification • Creational Patterns (创建模式) – deal with object creation mechanisms • Structural Patterns (结构模式) – describe how classes and objects can be combined to form larger structures. • Behavioral Patterns (行为模式) – identify common communication patterns between objects and realize these patterns
23 Design Patterns Creational Patterns Abstract Factory(抽象工厂) Prototype(原始模型) (创建模式) Singleton(单例) Builder(构建器) Factory Method(工厂方法) Structural Patterns Adapter适配器) bridge(桥梁) (结构模式) Composite(合成) Decorator(装饰 facade(外观) Flyweight(享元) Proxy(代理) Behavioral Patterns Chain of Responsibility(责任链) Command(命令) (行为模式) Iterator(迭代子) Mediator(调停者) Memento(备忘录) Observer(观察者) State(状态) Strategy(策略 Ⅴ isitor(访问者) Template Method(模版方法) Interpreter(翻译器)
12 23 Design Patterns Creational Patterns (创建模式) Abstract Factory (抽象工厂) Prototype (原始模型) Singleton (单例) Builder (构建器) Factory Method (工厂方法) Structural Patterns (结构模式) Adapter (适配器) Bridge (桥梁) Composite (合成) Decorator (装饰) Façade (外观) Flyweight (享元) Proxy (代理) Behavioral Patterns (行为模式) Chain of Responsibility (责任链) Command (命令) Iterator (迭代子) Mediator (调停者) Memento (备忘录) Observer (观察者) State (状态) Strategy (策略) Visitor (访问者) Template Method (模版方法) Interpreter (翻译器)
Topics Covered Today 2. 3 Advanced Class design 2. 3. 4 Design patterns 2.3.5 Singleton pattern 2.3.6 Strategy Pattern
13 Topics Covered Today • 2.3 Advanced Class Design – 2.3.4 Design Patterns – 2.3.5 Singleton Pattern – 2.3.6 Strategy Pattern
One of a Kind In some applications, there are classes that should only be instantiated once For instance system clock of an operating system one accounting system in a company; object used for logging, catalog of a library system
14 One of a Kind • In some applications, there are classes that should only be instantiated once. • For instance: – system clock of an operating system; – one accounting system in a company; – object used for logging; – catalog of a library system – …
Answer These Questions? How could you create a single object? New MyClasso What if wanted to create another my class object? Could it call new on MyClass again? Yes, if it is a public class And if not? Well, if it is not a public class only classes in the same package can instantiate it. But they can still instantiate it more than once 15
15 Answer These Questions? • How could you create a single object? – New MyClass(); • What if wanted to create another MyClass object? Could it call new on MyClass again? – Yes, if it is a public class. • And if not? – Well, if it is not a public class,only classes in the same package can instantiate it. But they can still instantiate it more than once