《2019年编程语言中的架构思考.pdf》由会员分享,可在线阅读,更多相关《2019年编程语言中的架构思考.pdf(66页珍藏版)》请在三个皮匠报告上搜索。
1、编程语言中的架构思考Dont Believe Me!Composition over inheritance?Does Golang need dependency injection?Do we need a complete interface?OOPInheritanceInheritanceInheritance is the mechanism of basing an object or class upon another object(prototypical inheritance)or class(class-based inheritance),retaining sim
2、ilar implementation.What is the role of inheritance?Code Reuse/DRY?When to use inheritance?IS-A?Java Standard LibraryStackMethods inherited from class java.util.Vector add,add,addAll,addAll,addElement,capacity,clear,clone,contains,containsAll,copyInto,elementAt,elements,ensureCapacity,equals,firstEl
3、ement,get,hashCode,indexOf,indexOf,insertElementAt,isEmpty,iterator,lastElement,lastIndexOf,lastIndexOf,listIterator,listIterator,remove,remove,removeAll,removeAllElements,removeElement,removeElementAt,removeRange,retainAll,set,setElementAt,setSize,size,subList,toArray,toArray,toString,trimToSizecla
4、ss Bird(object):name=Bird def fly(self):print(self.name+can fly)def lay(self):print(self.name+can lay)def run(self):print(self.name+can run)class Ostrich(Bird):name=Ostrich def swim(self):print(self.name+can swim)Inheritance break Encapsulationclass Bird(object):name=Bird def fly(self):pass def lay(
5、self):print(self.name+lay)def run(self):print(self.name+run)class Ostrich(Bird):name=Ostrich def fly(self):raise Exception(self.name+cant fly)class Goddess(self):name=Goddess def pray(self):print(self.name+pray)def release_bird(self,bird):self.pray()bird.fly()class Goddess(self):name=Goddess def pra
6、y(self):print(self.name+pray)def release_bird(self,bird):self.pray()if isinstance(bird,Ostrich):bird.run()else:bird.fly()Liskov substitution principleWhat is wanted here is something like the following substitution property:If for each object o1 of type S there is an object o2 of type T such that fo
7、r all programs P defined in terms of T,the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of TBuilding software systems with replaceable componentsComponents at the same level adhere to the same convention for easy replacementInheritance is for code to be reusedAPI need
8、 not know other systems how to use it If API didnt change its function and interface。class Base(object):name=Base def test(self):print(self.name)#do something class IBase(Base):def foo(self):#do something self.test()class Base(object):name=Base def test(self):print(self.name)self.foo()def foo(self):
9、#do something pass class IBase(Base):name=IBase def foo(self):#do something self.test()Fragile base classSo many problems,what can we do?Compositiontype stack struct s int func NewStack()*stack return&stackmake(int,0)func(s*stack)Push(v int)s.s=append(s.s,v)func(s*stack)Pop()(int,error)l:=len(s.s)if
10、 l=0 return 0,errors.New(empty Stack)res:=s.sl-1 s.s=s.s:l-1 return res,nil class Stack(object):def _init_(self):self._items=def push(self,item):self._items.append(item)def pop(self):self._items.pop()composition/inheritancefavor composition over inheritance(exclude interface inheritance)Building sof
11、tware systems with replaceable componentsDesign an orthogonal architecturetype Int64Slice int64 func(s Int64Slice)Len()int return len(s)func(s Int64Slice)Less(i,j int)bool return si sj func(s Int64Slice)Swap(i,j int)temp:=si si=sj sj=temp func main()data:=Int64Slice1,3,2,100 sort.Sort(data)type Inte
12、rface interface Len()int Less(i,j int)bool Swap(i,j int)https:/ inversion principlehttps:/ inversion principleHigh-level modules should not depend on low-level modules.Both should depend on abstractions.Abstractions should not depend on details.Details should depend on abstractions.FileInputStream f
13、ileStream=new FileInputStream(fileName);BufferedInputStream bufferedStream=new BufferedInputStream(fileStream);ObjectInputStream objectStream=new ObjectInputStream(bufferedStream);int open(const char*path,int flags,mode_t permissions);ssize_t read(int fd,void*buffer,size_t count);ssize_t write(int f
14、d,const void*buffer,size_t count);off_t lseek(int fd,off_t offset,int referencePosition);which is better?Java/Golangsubstring/:IndexOutOfBoundsExceptionPython:No Exception/ErrorSliceWhen the same answer is deleted multiple times?Task QueueChecked Exceptionhttp:/www.yinwang.org/blog-cn/2017/05/23/kot
15、linExposed interfaces(errors)increase both functionality and complexity,requiring trade-offsThe vast majority of clients use the same combining code to handle errors,invocation interfaces;We should reduce complexity,hide the details.Interfaces should be designed to make the most common usage as simp
16、le as possibleSeparate general-purpose and special-purpose codeDefine errors(and special cases)out of existencefavor composition over inheritance(exclude interface inheritance)Building software systems with replaceable componentsHigh-level modules should not depend on low-level modules.Both should d
17、epend on abstractionsComposition over inheritance?Does Golang need dependency injection?Do we need a complete interface?Reference敏捷软件开发 设计模式 冒号课堂 Clean Architecture 面向对象葵花宝典:思想、技巧与实践 Fluent Python 代码大全 松本行弘的程序世界 Encapsulation is not information hiding ENCAPSULATION WITH EXAMPLE AND BENEFITS IN JAVA&
18、OOP Introduction IoC,DIP,DI and IoC Container写了这么多年代码,你真的了解设计模式么?写了这么多年代码,你真的了解SOLID吗?Python SOLID 正交设计,OO与SOLID 解密“设计模式”为何大量设计模式在动态语言中不适用?Is Liskov Substitution Principle incompatible with Introspection or Duck Typing?Spring IoC有什么好处呢?SOLID Python:SOLID principles applied to a dynamic programming language SOLID 谈谈面向对象编程Q&A