tigerb / easy-tips Goto Github PK
View Code? Open in Web Editor NEWA little Tips in my Code Career with Go&PHP 🥳🥳🥳
Home Page: http://easy-tips.tigerb.cn
A little Tips in my Code Career with Go&PHP 🥳🥳🥳
Home Page: http://easy-tips.tigerb.cn
PS:构造方法也拼错了
抽象工厂模式中,一个工厂能创建一簇对象不是一类对象,见下面的UML图
拿原代码中的例子,plant工厂只能创造plant对象,Animal工厂只能创造Animal对象,不符合抽象工厂模式,只能算是工厂方法模式的嵌套,正确的的抽象工厂模式已提PR #20
整理的还挺全面的~
Mysql 常用语句里这个是不对的吧:
列出表:DESC TABLES;
列出表应该是 SHOW TABLES, 而查看表结构是 DESC table_name;
能否加上使用场景
单例模式 工厂模式 用得多 😳
很多flag都还是灰的
https://github.com/TIGERB/easy-tips/blob/master/builder/test.php
这个小米手机为什么不使用继承来实现呢?
这个例子没有让我理解创建模式的作用
命名空间出现问题
throw new Exception( "$type is not supported", 400 );
应该是添加跟空间
修改内层循环次数
/**
* 冒泡排序
*
* @param array $value 待排序数组
* @return array
*/
function bubble($value = [])
{
$length = count($value) - 1;
// 外循环
for ($j = 0; $j < $length; ++$j) {
// 内循环
for ($i = 0; $i < $length - $j; ++$i) {
// 如果后一个值小于前一个值,则互换位置
if ($value[$i + 1] < $value[$i]) {
$tmp = $value[$i + 1];
$value[$i + 1] = $value[$i];
$value[$i] = $tmp;
}
}
}
return $value;
}
print_r(bubble([5,3,8,1,2]));
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 5
[4] => 8
)
没见过benchmark就跑这么几次的。。。。 http://tigerb.cn/2017/03/04/php-magic-function
easy-tips/patterns/builder/ 建造者模式有错误,方法名字不对
应该方法名 produce
却写成了 构造
悲观锁的代码中如果已获得锁的程序执行时间超过过期时间,就会出现两个程序同时获得了锁,造成数据覆盖,俗称脑裂🙈,你觉得呢?
TCP/IP
应用层(http/https/websocket/ftp...) => 定义:文本传输协议,主要传输数据为报文
|
传输层(tcp/udp) => 定义:端口,主要传输数据为包,再次中会实现分组交换或电路交换
|
网络层(ip) => 定义:IP,主要传输数据为数据帧,典型有arp协议,再此中会添加ip->mac映射关系,crc冗余校验帧完整性等
|
网络接口层(光缆/电缆/交换机/路由/终端...) => 定义:物理 主要传输数据为二进制比特流
TIGERB 是我见过最好的PHP开发者
create TABLE one_table
LIKE another_table
;
How can I do like this?
$timeout = 500; do { $microtime = explode(' ', microtime()); $microtime = (int)round(($microtime[1]+$microtime[0])*1000, 0); $microtimeout = $microtime+$timeout+1; // 上锁 $isLock = $redis->setnx('lock.count', $microtimeout); if (!$isLock) { // 超时判断 $previousTime = $redis->getset('lock.count', $microtimeout); if ($previousTime <= $microtime) { // 超时 break; } } } while (!$isLock);
设计模式总结的不错,说明文档能详细点更好了
符合PSR-1/PSR-2的PHP编程规范实例
easy-tips/standard.php里,提到
/**
在 psr-2标准里,是不应该有下划线的。
`
4.2. Properties
Visibility MUST be declared on all properties.
The var keyword MUST NOT be used to declare a property.
There MUST NOT be more than one property declared per statement.
Property names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility.
A property declaration looks like the following.
`
Hello, I think it's very popular repo is China(?). But I think people around the world also want to get information from it. Can you please translate this thing to English
如题,作者代码单例例子部分代码如下
/**
* 魔法方法
* 禁止clone对象
*
* @return string
*/
public function __clone()
{
echo 'clone is forbidden';
}
个人觉得可以直接将魔术方法__clone设为私有,当外部利用clone克隆对象直接抛出致命错误,如果如作者设为公有,clone还是可以克隆对象,打印一句话只能警告而已。
class Two{
function do(){
echo 1111;
}
}
class One
{
private $closure;
public function __construct(Closure $closure)
{
$this->closure = $closure();
}
public function doSomething()
{
if (1) {
// 用的时候再实例化
// 实现懒加载
$instance = $this->closure;
$instance->do();
}
}
}
//或者
class One
{
private $closure;
public function __construct(Closure $closure)
{
$this->closure = $closure;
}
public function doSomething()
{
if (1) {
// 用的时候再实例化
// 实现懒加载
$instance = $this->closure;
$instance()->do();
}
}
}
$instance = new One(function () {
// 类One外部依赖了类Two
return new Two();
});
$instance->doSomething();//1111
clone方法感觉还是修改为pirvate修饰比较好。
请问,数据结构是打算这样写一个md文件去描述还是要上代码呢?
楼主设计模式看的哪本书?楼主的笔记是要点,理解有点困难,所以我想找书来看一下。
Default value for parameters with a class type hint can only be NULL D:\www\easy-tips\template\SmsCompanyOne.php on line 32
多了两种!
文档中的图片裂了。
麻烦将patterns/decorator/DecoratorBrand.php文件中的phone字符串批量替换成shoes;
另外,不得不说,设计模式章节提供的代码很赞,感谢🙏
Hi, just by brief looking at your code I noticed that you actually measure time with an instantiation of an object. That could possibly distort the results quite a bit since instantiation of an object is usually much more expensive then accessing method.
Meaning that if lets say creating new instance needs 100 units of time, getting property without magic method takes 1 and with magic method takes 10 your results will be 101 units without magic method and 110 with magic method.
This way it looks like it is only about 10% faster. But if you do it without the instantiation you will get 1 to 10 which is actually 10x faster, in percent its 1000%.
Because of that I would like to ask you to either remake your test and do it again.
// Do 执行子组件
// ctx 业务上下文
// currentConponent 当前组件
// wg 父组件的waitgroup对象
func (bc *BaseConcurrencyComponent) Do(ctx *Context, currentConponent Component, wg *sync.WaitGroup) (err error) {
defer wg.Done()
// 初始化并发子组件channel
if bc.logicResChan == nil {
bc.logicResChan = make(chan interface{}, 1)
}
go currentConponent.BusinessLogicDo(bc.logicResChan)
select {
// 等待业务执行结果
case <-bc.logicResChan:
// 业务执行结果
fmt.Println(runFuncName(), "bc.BusinessLogicDo wait.done...")
break
// 超时等待
case <-ctx.TimeoutCtx.Done():
// 超时退出
fmt.Println(runFuncName(), "bc.BusinessLogicDo timeout...")
bc.Err = ErrConcurrencyComponentTimeout
break
}
// 执行子组件
err = currentConponent.ChildsDo(ctx)
return
}
这个组件的并发设计中,子任务的执行是同时起生产者和消费者的,在消费者超时退出后,生产者(urrentConponent.BusinessLogicDo(bc.logicResChan))如果此时才获取到结果发送到消费者将会阻塞,这样会造成多go程阻塞,go程积压造成内存爆表。
我模仿写了一个伪代码:
func run(wg *sync.WaitGroup) {
defer wg.Done()
ch := make(chan string)
wg.Add(1)
go func() {
time.Sleep(9 * time.Second)
// 阻塞后死锁
ch <- "work1"
}()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
select {
case res := <-ch:
fmt.Println("接收到任务", res)
case <-ctx.Done():
fmt.Println("ctx err : ", ctx.Err())
return
}
}
func main() {
wg := sync.WaitGroup{}
wg.Add(1)
go run(&wg)
wg.Wait()
fmt.Println("success")
}
运行结果是go程阻塞死锁 :
ctx err : context deadline exceeded
fatal error: all goroutines are asleep - deadlock!
builder/PhoneInterface 接口的方法不能是private,不过这个接口最后居然没有用到是什么鬼
按我的理解,原型模式中依赖getPrototype()
方法得到的2个对象,应该是内存分开的不同对象。
不论是否是深拷贝,Prototype类里用instance来返回对象,而不是每次都返回clone,是有问题的吧?
use prototype\Prototype;
// 创建一个原型对象
$prototype = new Prototype();
// 获取一个原型的clone
$prototypeCloneOne = $prototype->getPrototype();
$prototypeCloneOne->_name = 'one';
$prototypeCloneOne->getName();
// 获取一个原型的clone
$prototypeCloneTwo = $prototype->getPrototype();
$prototypeCloneTwo->_name = 'two';
$prototypeCloneTwo->getName();
$prototypeCloneOne->getName();
//输出 one two two 说明2个对象其实是一个
申请格式如下:
文件名-githubID
希望国外的developer也可以受益于这个项目~
$list = [['key1'=>1,'key2'=>2,'value'=>'value']];
$map = array_combine(array_column($list,'key1'),$list);
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.