目录
一、装饰模式
要点
装饰模式,根据字面意思,是对一些类进行装饰,动态扩展功能。
Component类:抽象类或接口
ConcreteComponent类:代表具体的被装饰类,就像下文中的Person类
Decoraor类:装饰类,成员是Component类的对象
ConcreteDecoraor类:具体的装饰器类,对ConcreteComponent添加一些功能
从衣服装饰看起,就是把一个人不断包装。
应用场景:
1、程序希望动态增加一些类的功能,而又不影响其他子类对象。
2、采用继承来增强对象功能不利于系统扩展维护。
PROS AND CONS
PROS
装饰模式和继承都是要扩展功能,但装饰更加灵活
通过使用不同的具体装饰类及其排列组合,可以创造出不同行为的组合
CONS
会产生很多细粒度对象。
二、代码实例一
第一个类:抽象类组成类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
abstract class Component
{
public abstract void show();
}
}
定义一个抽象展现函数。
第二个类:装饰类(继承抽象组成类、成员函数是其父类组成类)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Finery:Component
{
protected Component com;
public Finery(Component com)
{
this.com = com;
}
public override void show()
{
if(com!=null)
{
com.show();
}
}
}
}
第三个类:人物类(成员是其名字、成员函数是展现函数、继承装饰类)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Person : Component
{
private string name;
public Person(string name)
{
this.name = name;
}
public Person()
{
}
public override void show()
{
Console.WriteLine (name);
}
}
}
第四、五、六都是衣服类(继承装饰类)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Kt:Finery
{
public Kt(Component com) : base(com) { }
public override void show()
{
Console.WriteLine("穿了裤子");
base.show();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Shoe:Finery
{
public Shoe(Component com) : base(com) { }
public override void show()
{
Console.WriteLine("穿了鞋子");
base.show();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Ts : Finery
{
public Ts(Component com) : base(com) { }//初始化父类
public override void show()
{
Console.WriteLine("穿了T-shirt");
base.show();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Dx:Finery
{
public Dx(Component com) : base(com) { }//初始化父类
public override void show()
{
Console.WriteLine("穿了短袖");
base.show();
}
}
}
然后是测试类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("第一种方式");
Component Mrs = new Person("小王");
Component Kt = new Kt(Mrs);
Component Ts = new Ts(Kt);
Component Shoe = new Shoe(Ts);
Shoe.show();
Console.WriteLine("-------------------");
Console.WriteLine("第二种方式");
Component Dx = new Dx(new Kt(new Shoe(new Person("小李"))));
Dx.show();
//递归调用
}
}
}
注意多体会写法。
三、代码实例二
对裸车进行装饰
第一个类:抽象车类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Decorator
{
abstract class Car
{
public abstract void Show();
}
}
第二个类:装饰车类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Decorator
{
class CarDecorator : Car
{
protected Car car ;
public CarDecorator(Car car)
{
this.car = car;
}
public override void Show()
{
if(car != null)
{
car.Show();
}
}
}
}
具体被装饰类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Decorator
{
class BenChi : Car
{
private string name;
public BenChi(string name)
{
this.name = name;
}
public BenChi()
{
}
public override void Show()
{
Console.WriteLine(name);
}
}
}
装饰类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Decorator
{
class ConcreteDeractorOne:CarDecorator
{
public ConcreteDeractorOne(Car c) : base(c) { }
public override void Show()
{
Console.WriteLine("装饰一");
base.Show();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Decorator
{
class ConcreteDeractorTwo:CarDecorator
{
public ConcreteDeractorTwo(Car c) : base(c) { }
public override void Show()
{
Console.WriteLine("装饰二");
base.Show();
}
}
}
测试类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Decorator
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Car tw = new ConcreteDeractorTwo(new ConcreteDeractorOne(new BenChi("aaa")));
tw.Show();//
}
}
}
四、IO流
JAVA中的Io流
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class IoTestOne {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
DataInputStream cin = null;
try {
cin = new DataInputStream(new BufferedInputStream(new FileInputStream("D:/Io.txt")));
byte bs[] = new byte[cin.available()];
cin.read(bs);
String content = new String(bs);
System.out.println(content);
} finally {
cin.close();//
}
}
}
改写后
import java.io.IOException;
import java.io.OutputStream;
public class EncryptOutputStream extends OutputStream {
private OutputStream os=null;
public EncryptOutputStream(OutputStream os) {
super();
this.os = os;
}
@Override
public void write(int b) throws IOException {
// TODO Auto-generated method stub
b=b+2;
if(b>=97+26)
{
b-=26;
}
this.os.write(b);//重写这个函数
}
}
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class IoTest {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
DataOutputStream cout = new DataOutputStream(
new BufferedOutputStream(new EncryptOutputStream(new FileOutputStream("D:/Io.txt"))));
cout.write("abcdd".getBytes());
cout.close();
}
}
五、代码实例三
奖金分类,对于个人有当月奖金、个人累计奖金、个人业务增长奖金、及时回款奖金、限时成交加码奖金等;对于业务主管或者是业务经理,除了个人奖金外,还有团队累积奖金、团队业务增长奖金、团队盈利奖金等。 计算公式也有不同 计算奖金金额的基数也有不同 奖金的计算方式会经常变化。要适于调整和修改。
利用装饰模式:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SalaryTwo
{
abstract class Component
{
public abstract string Calculate();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SalaryTwo
{
class Deractor:Component
{
Component component;
public Deractor(Component component)
{
this.component = component;
}
public override string Calculate()
{
while(component !=null)
{
return component.Calculate();
}
return component.Calculate();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SalaryTwo
{
class Employee : Component
{
string salary;
public Employee(string salary)
{
this.salary = salary;
}
public override string Calculate()
{
return salary;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SalaryTwo
{
class Management:Component
{
string salary;
public Management(string salary)
{
this.salary = salary;
}
public override string Calculate()
{
return salary;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SalaryTwo
{
class PersonalAdd : Deractor
{
public PersonalAdd(Component component) : base(component)
{
}
public override string Calculate()
{
return base.Calculate ()+"+3000";
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SalaryTwo
{
class PersonalSalary : Deractor
{
public PersonalSalary(Component com) : base(com) { }
public override string Calculate()
{
return base.Calculate()+"+8000";
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SalaryTwo
{
class TeamSalary:Deractor
{
public TeamSalary(Component component) : base(component)
{
}
public override string Calculate()
{
return base.Calculate()+"+900";
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SalaryTwo
{
class Program
{
static void Main(string[] args)
{
Component pd = new PersonalAdd(new TeamSalary(new PersonalSalary(new Management("600"))));
Console.WriteLine(pd.Calculate());
Component pd1 = new PersonalAdd(new Employee("300"));
Console.WriteLine(pd1.Calculate());
// out it !
}
}
}
简略实现一下,有时间再精致一下代码。
太丑了这个图...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Salary
{
abstract class Component
{
public abstract void show();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Salary
{
class Decorator:Component
{
Component component;
// int salary:
public Decorator(Component component)
{
this.component = component;
}
public override void show()
{
if(component !=null)
{
component.show();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Salary
{
class Employee : Component
{
string name;
public Employee(string name)
{
this.name = name;
}
public Employee()
{
}
public override void show()
{
Console.WriteLine("职员薪资:");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Salary
{
class Manager : Component
{
string name;
public Manager(string name)
{
this.name = name;
}
public Manager()
{
}
public override void show()
{
Console.WriteLine("经理薪资:");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Salary
{
class MonthSalary:Decorator
{
public MonthSalary (Component com) : base(com) { }
public override void show()
{
Console.WriteLine("月薪");
base.show();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Salary
{
class RewardSalary:Decorator
{
public RewardSalary(Component com): base(com){ }
public override void show()
{
Console.WriteLine("个人累计月奖金");
base.show();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Salary
{
class TeamAddSalary:Decorator
{
public TeamAddSalary(Component com) : base(com) { }
public override void show()
{
Console.WriteLine("团队业务增加奖金");
base.show();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Salary
{
class TeamSalary:Decorator
{
public TeamSalary(Component com) : base(com) { }
public override void show()
{
Console.WriteLine("团队绩效奖金");
base.show();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Salary
{
class Program
{
static void Main(string[] args)
{
Component salary = new MonthSalary(new RewardSalary (new Employee ("小王")));
salary.show();
Component salaryTwo = new MonthSalary(new RewardSalary(new TeamSalary(new TeamAddSalary (new Manager ("小李")))));
salaryTwo.show();//
}
}
}
UML图:
太丑了...