编程中的池(decorator)

编程中池的概念性理解

一、C#

/*
 * The Using of Pool:
 *              In some conditions, the instance of the specific class consumes more time or memory resources during 
 *              initializing or releasing phase.
 *          Then the frequent 'new', 'release' or 'free' of the instance will be an uneffient solution.
 *
 *          So following is the reference of Pool.
 *          
 *          A Pool holds a appropriate amount of the instances we assign, and supplies instances have been initliazed. 
 *          And support the fake release of the instance cause a relative higher effiency.
 *          
 *          Pool: A tool or strategy to manage materials like mentioned above.
 *          
 * Demo Introduction:
 *              Coming to our daily ride bikes. In such a time that corona virus spreads widly around the world. When we 
 *          come to the public shared services like shared bicycles, some basic tools for health caring is very necessary.

 *          Assume that tools like:
 *              First, take some messures to make medical security.
 *              Second, put it in some assigned places for customers to ride.
 *              Third, after riding it, customer put it in another type of areas for staff to recycle and proceed 
 *              	the deal like First and Second steps.
 *
 *          Then consiquent oprations like those kind will not be intelligent.
 *          
 *              And to solve it, you take a month card for a single shared bike and make a tag of you, that none else 
 *          will be able to ride it.
 *              Then, the use process will be greatly simplified for we can  stop it everywhere and reduce the necessary 
 *          medical processes.
 *          
 *          So we get an mechanism supproting fake recycling and supplies normal function within the related class.
 * Warm warning:
 *          All the files and defination or classes dirtribution are simply for theory practice test, not feasible in real 
 *      development.
 *          
 */

using System;
using System.Collections;
using System.Threading;

interface BaseBicycle
{
    public void BeCleaned();
    public void Riding();
    public void Recycle();
}

class Bicycle : BaseBicycle
{
    public void BeCleaned()
    {
        Thread.Sleep(3000);
        Console.WriteLine("Cleaning the bicycle over.");
    }

    public void Riding()
    {   
        Console.WriteLine("Starts to move faster.");  
    }

    public void Recycle()
    {
        Thread.Sleep(5000);
        Console.WriteLine("Recording data and wisthdraw the bicycle.");
    }
}

class MyBicycle : BaseBicycle
{
    private BaseBicycle _bicycle;
    private Queue _pool;

    public MyBicycle(BaseBicycle bicycle, Queue pool)
    {
        _bicycle = bicycle;
        _pool = pool;
    }

    public void BeCleaned()
    {
        _bicycle.BeCleaned();
    }

    public void Riding()
    {
        _bicycle.Riding();
    }

    public void Recycle()
    {
        Console.WriteLine("Just put in my own yard, convinient to use anytime. ");
        _pool.Enqueue(this);
    }
}

class BicyclePool
{
    private static Queue _pool = new Queue();

    public BicyclePool()
    {
        for(int i = 0; i < 5; i++)
        {
            Bicycle bicycle = new Bicycle();
            bicycle.BeCleaned();
            MyBicycle mybicycle = new MyBicycle(bicycle, _pool);
            _pool.Enqueue(mybicycle);
        }
    }

    public BaseBicycle GetBicycle()
    {
        if(0 < _pool.Count)
        {
            return _pool.Dequeue() as BaseBicycle;
        }
        return null;
    }
}
class Program
{
    public static void Main()
    {
        BicyclePool pool = new BicyclePool();
        for(int i = 0; i < 20; i++)
        {
            Console.WriteLine($"-------------------------- {i + 1} -------------------------------");
            BaseBicycle bicycle = pool.GetBicycle();
            bicycle.Riding();
            bicycle.Recycle();
        }
    }
}

二、C++

#include<iostream>
#include<stdlib.h>
#include<list>
#include<Windows.h>
class BaseBicycle {
public:
    virtual void BeClean() = 0;
    virtual void Riding() = 0;
    virtual void Recycle() = 0;
};

class Bicycle : public BaseBicycle {
public:
    void BeClean()
    {
        Sleep(5000);
        std::cout << " It takes a while to clean. " << std::endl;
    }

    void Riding()
    {
        std::cout << " Starting to move faster. " << std::endl;
    }

    void Recycle()
    {
        Sleep(3000);
        std::cout << " It takes quite a while to recycle to the specific place. " << std::endl;
    }
};

class MyBicycle : public BaseBicycle
{
public:
    MyBicycle(BaseBicycle* bicycle, std::list<BaseBicycle*>* pool)
    {
        _bicycle = bicycle;
        _pool = pool;
    }

    ~MyBicycle()
    {
        delete _bicycle;
    }

    void BeClean()
    {
        _bicycle->BeClean();
    }

    void Riding()
    {
        _bicycle->Riding();
    }

    void Recycle()
    {
        _pool->push_back(this);
        std::cout << "Just put in my own yard, and be convient to use anytime. " << std::endl;
    }

private:
    BaseBicycle* _bicycle;
    std::list<BaseBicycle*>* _pool;
};

class BicyclePool {
public:

    BicyclePool()
    {
        for (int i = 0; i < 5; i++)
        {
            BaseBicycle* basebicycle = new Bicycle();
            basebicycle->BeClean();
            BaseBicycle* mybicycle = new MyBicycle(basebicycle, &_pool);
            _pool.push_back(mybicycle);
        }
    }

    ~BicyclePool()
    {
        while (_pool.size())
        {
            delete _pool.front();
            _pool.pop_front();
        }
    }

    BaseBicycle* GetBicycle()
    {
        if (0 < _pool.size())
        {
            BaseBicycle* res = _pool.front();
            _pool.pop_front();
            return res;
        }
        else
        {
            return NULL;
        }
    }

private:
    std::list<BaseBicycle*> _pool;
};

int main()
{
    BicyclePool pool;

    for (int i = 0; i < 20; i++) {
        std::cout << std::endl;
        std::cout << " --------------------- " << i + 1 << " ------------------------- " << std::endl;
        BaseBicycle* bicycle = pool.GetBicycle();
        if(NULL != bicycle)
        bicycle->Riding();
        bicycle->Recycle();
    }
    return 0;

}

三、Java

1、BaseBicycle.class

package wooden_pig.bicycle.com;

import java.util.TreeMap;

public interface BaseBicycle {
    void BeCleaned();

    void Riding();

    void BeRecycled();
}

2、Bicycle.java

package wooden_pig.bicycle.com;

public class Bicycle implements BaseBicycle {
    @Override
    public void BeCleaned() {
        try {
            Thread.sleep(5000);
            System.out.println("Clean the bicycle, enable to offer riding function in secure condition.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void Riding(){
        System.out.println("Start to moving faster.");
    }

    @Override
    public void BeRecycled(){
        try{
            Thread.sleep(3000);
            System.out.println("Recycle the bicycle. Unable to offer normal riding function");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

3、MyBicycle.java

package wooden_pig.bicycle.com;

import java.util.LinkedList;

public class MyBicycle implements BaseBicycle {
    private BaseBicycle _bicycle;
    private LinkedList<BaseBicycle> _pool;

    public MyBicycle(BaseBicycle bicycle, LinkedList<BaseBicycle> pool){
        _bicycle = bicycle;
        _pool = pool;
    }

    @Override
    public void BeRecycled(){
        System.out.println("Just put in my own yard, able to offer service as normal whenever needed. ");
        _pool.add(this);
    }

    @Override
    public void BeCleaned(){
        _bicycle.BeCleaned();
    }

    public void Riding(){
        _bicycle.Riding();
    }
}

4、BicyclePool.java

package wooden_pig.bicycle.com;

import java.util.LinkedList;

public class BicyclePool {
    private static LinkedList<BaseBicycle> _pool = new LinkedList<>();

    static {
        for(int i = 0; i < 5; i++){
            Bicycle bicycle =  new Bicycle();
            bicycle.BeCleaned();
            MyBicycle myBicycle = new MyBicycle(bicycle, _pool);
            _pool.add(myBicycle);
        }
    }

    public BaseBicycle GetBicycle(){
        if(0 < _pool.size()){
            return _pool.poll();
        }else{
            return null;
        }
    }
}

5、Test.java

import wooden_pig.bicycle.com.BaseBicycle;
import wooden_pig.bicycle.com.BicyclePool;

public class Test {

    public static void main(String args[]) {
        BicyclePool pool = new BicyclePool();
        for (int i = 0; i < 20; i++) {
            System.out.println("------------------" + (i + 1) + "-----------------");
            BaseBicycle bicycle = pool.GetBicycle();
            if (null != bicycle) {
                bicycle.Riding();
                bicycle.BeRecycled();
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值