工厂模式
export {}
abstract class AmericaCoffee{}
abstract class LatteCoffee{}
abstract class CappuccinoCoffee{}
class StarBuckAmericanCoffee extends AmericanCoffee {}
class LuckinAmericanCoffee extends AmericaCoffee {}
class StarBuckLetteCoffee extends LatteCoffee{}
class LuckinLatteeCoffee extends LatteCoffee{}
class LuckinCappuccinoCoffeeCoffee extends LatteCoffee{}
class StarBuckCappuccinoCoffee extends CappuccinoCoffee{}
abstract class CafeFactory{
abstract createAmericanCoffee():AmericaCoffee
abstract createLatteeCoffee():LatteCoffee
abstract createCappuccinoCoffeeCoffee():CappuccinoCoffee
}
class StarBuckCafeFactory extends CafeFactory{
createAmericanCoffee(): AmericaCoffee {
return new StarBuckAmericanCoffee("")
}
createLatteeCoffee(): LatteCoffee {
return new StarBuckLetteCoffee()
}
createCappuccinoCoffeeCoffee(): CappuccinoCoffee {
return new StarBuckCappuccinoCoffee()
}
}
class LuckinCafeFactory extends CafeFactory{
createAmericanCoffee(): AmericaCoffee {
return new LuckinAmericanCoffee()
}
createLatteeCoffee(): LatteCoffee {
return new LuckinLatteeCoffee()
}
createCappuccinoCoffeeCoffee(): CappuccinoCoffee {
return new LuckinCappuccinoCoffeeCoffee()
}
}
let luckinCafeFactory=new LuckinCafeFactory()
console.log(luckinCafeFactory.createAmericanCoffee())
适配器模式
class Socket{
output(){
return '220V'
}
}
abstract class Power{
abstract charge():string
}
class PowerAdaptor extends Power{
constructor(public socket:Socket){
super();
}
charge(): string {
return this.socket.output()+'转换为24V'
}
}
let adaptor=new PowerAdaptor(new Socket())
console.log(adaptor.charge());
装饰器模式
abstract class Shape{
abstract draw():void
}
class Circle extends Shape{
draw(): void {
console.log('绘制圆形');
}
}
class Rectangle extends Shape{
draw(): void {
console.log('绘制矩形');
}
}
abstract class ColorfulShape extends Shape{
constructor(public shape:Shape){
super()
}
abstract draw(): void;
}
class RedColorfulShape extends ColorfulShape{
draw(): void {
this.shape.draw()
console.log('把边框涂成红色');
}
}
class GreenColorfulShape extends ColorfulShape{
draw(): void {
this.shape.draw()
console.log('把边框涂成绿色');
}
}
let redColorfulShape=new RedColorfulShape(new Circle())
redColorfulShape.draw()
namespace a{
interface Animal{
swings:number
fly():void
}
function flyable(target){
console.log(target);
target.prototype.swings=2
target.prototype.fly=function(){
console.log('我能飞');
}
}
@flyable
class Animal{
constructor(){}
}
let animal:Animal=new Animal
console.log(animal.swings);
animal.fly()
}
namespace b{
function instancePropertyDecorator(target:any,key: any) {
target.protoName="我是类的原型上的属性"
console.log('instancePropertyDecorator',target,key);
}
function classPropertyDecorator(target:any,key: any) {
console.log('classPropertyDecorator',target,key);
}
function instanceMethodDecorator(target:any,key: any,descriptor:any) {
console.log('instanceMethodDecorator',target,key,descriptor);
}
function classMethodDecorator(target:any,key: any,descriptor:any) {
console.log('classMethodDecorator',target,key,descriptor);
}
class Person{
@instancePropertyDecorator
instanceProperty:string
@classPropertyDecorator
static classProperty:string
@instanceMethodDecorator
instanceMethod(){}
@classMethodDecorator
static classMethod(){
}
}
}