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