装饰模式--奖金计算(C#)与JAVA IO流

奖金分类,对于个人有当月奖金、个人累计奖金、个人业务增长奖金、及时回款奖金、限时成交加码奖金等;对于业务主管或者是业务经理,除了个人奖金外,还有团队累积奖金、团队业务增长奖金、团队盈利奖金等。 计算公式也有不同 计算奖金金额的基数也有不同 奖金的计算方式会经常变化。要适于调整和修改。

利用装饰模式:

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();//
        }
    }
}

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();
	}

}

猜你喜欢

转载自blog.csdn.net/lanshan1111/article/details/88814194