美文网首页
设计模式-结构型设计模式

设计模式-结构型设计模式

作者: nich | 来源:发表于2022-10-11 18:14 被阅读0次

    //本文参考菜鸟教程
    设计模式主要分为三大类创建型模式,结构型模式,行为型模式,本文主要讲结构型模式
    结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。

    1.适配器模式

    定义: 适配器模式将某个类的接口转换成客户端期望的另一个接口表示,目的是消除由于接口不匹配所造成的类的兼容性问题。

    interface  MediaPlayer{
        fun play(audioType:String,fileName:String)
    }
    
    interface AdvancedMediaPlayer{
        fun playVlc(fileName: String)
    
        fun playMp4(fileName: String)
    }
    
    class VlcImpl:AdvancedMediaPlayer{
        override fun playVlc(fileName: String) {
            println("playVlc is $fileName")
        }
    
        override fun playMp4(fileName: String) {
    
        }
    }
    
    class Mp4Impl:AdvancedMediaPlayer{
        override fun playVlc(fileName: String) {
        }
    
        override fun playMp4(fileName: String) {
            println("playMp4 is $fileName")
        }
    }
    
    class MediaPlayerAdapter(audioType: String) :MediaPlayer{
    
        lateinit var advancedMediaPlayer: AdvancedMediaPlayer
    
        init {
            when(audioType){
                "Mp4"->{
                    advancedMediaPlayer = Mp4Impl()
                }
                "Vlc"->{
                    advancedMediaPlayer = VlcImpl()
                }
            }
        }
    
        override fun play(audioType: String, fileName: String) {
            when(audioType){
                "Mp4"->{
                    advancedMediaPlayer.playVlc(fileName)
                }
                "Vlc"->{
                    advancedMediaPlayer.playMp4(fileName)
                }
            }
        }
    }
    
    class MediaPlayerImpl:MediaPlayer{
    
        lateinit var mediaPlayerAdapter: MediaPlayerAdapter
    
        override fun play(audioType: String, fileName: String) {
            when(audioType){
                "mp3"->{
                    println("自己的mp3")
                }
                "Mp4","Vlc"->{
                    mediaPlayerAdapter = MediaPlayerAdapter(audioType)
                    mediaPlayerAdapter.play(audioType,fileName)
                }
            }
        }
    }
    

    总结

    主要是为了一个类负责加入独立的或不兼容的接口功能

    2.装饰器模式

    定义:动态的将新功能附加到对象上。在对象功能扩展方面,它比继承更有弹性。
    1.Component(被装饰对象的基类)
    定义一个对象接口,可以给这些对象动态地添加职责。
    2.ConcreteComponent(具体被装饰对象)
    定义一个对象,可以给这个对象添加一些职责。
    3.Decorator(装饰者抽象类)
    维持一个指向Component实例的引用,并定义一个与Component接口一致的接口。
    4.ConcreteDecorator(具体装饰者)
    具体的装饰对象,给内部持有的具体被装饰对象,增加具体的职责。

    interface Shape{
        fun draw()
    }
    
    class Round:Shape{
        override fun draw() {
            println("round----draw")
        }
    }
    
    class Circle:Shape{
        override fun draw() {
            println("circle----draw")
        }
    }
    
    abstract class ColorShape(private val shape: Shape):Shape{
    
        override fun draw() {
            shape.draw()
        }
    }
    
    class RedColorShape(private val shape: Shape): ColorShape(shape) {
    
        override fun draw() {
            super.draw()
            setColor()
        }
    
         private fun setColor(){
            println("设置颜色")
        }
    
    }
    
    fun main() {
        val circleColorShape = RedColorShape(Circle())
        circleColorShape.draw()
    
        val roundColorShape = RedColorShape(Round())
        roundColorShape.draw()
    }
    

    3.代理模式

    定义:代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用。通俗的来讲代理模式就是我们生活中常见的中介。
    3.1静态代理

    interface House{
        fun bug()
    }
    
    class HouseImpl:House{
        override fun bug() {
           println("买房子")
        }
    }
    
    class HouseProxyImpl(private val house: House):House{
        
        override fun bug() {
            println("看房子")
            house.bug()
            decorateHouse()
        }
        
        private fun decorateHouse(){
            println("装饰房子")
        }
    }
    
    fun main() {
        val houseProxyImpl = HouseProxyImpl(HouseImpl())
        houseProxyImpl.bug()
    }
    

    3.2动态代理
    1.代理对象,不需要实现接口
    2.代理对象的生成,是利用JDK的API,动态的在内存中构建代理对象(需要我们指定创建代理对象/目标对象实现的接口的类型)
    代理类不用再实现接口了。但是,要求被代理对象必须有接口。

    class HouseTrendsProxy(private val obj: Any):InvocationHandler{
        override fun invoke(proxy: Any, method: Method?, args: Array<out Any>?): Any? {
            println("买房前准备")
            val result = method?.invoke(obj,args)
            println("买房后装修")
            return result
        }
    }
    fun main() {
        val house:House = HouseImpl()
        val proxyHouseImpl:House = Proxy.newProxyInstance(House::class.java.classLoader, arrayOf(House::class.java),HouseTrendsProxy(house)) as House
        proxyHouseImpl.bug()
    }
    

    4.外观模式

    定义: 隐藏了系统的复杂性,并向客户端提供了一个可以访问系统的接口。

    class CPU{
        fun start(){
            println("cpu start")
        }
    
        fun shutDown(){
            println("cpu shut down")
        }
    }
    
    class Disk{
        fun start(){
            println("disk start")
        }
    
        fun shutDown(){
            println("disk shut down")
        }
    }
    
    class Memory(){
        fun start(){
            println("memory start")
        }
    
        fun shutDown(){
            println("disk shut down")
        }
    }
    
    class Compuate(private val disk: Disk,private val memory: Memory,private val cpu: CPU){
    
        fun start(){
            disk.start()
            memory.start()
            cpu.start()
        }
    
        fun shutDown(){
            disk.shutDown()
            memory.shutDown()
            cpu.shutDown()
        }
    }
    

    优点 - 松散耦合,用户只需要关注门面类compuate不需要再关注子类,子类也容易扩展

    5.桥接模式

    定义: 将抽象部分与它的实现部分分离,使它们都可以独立地变化。

    interface DrawAPI {
        fun drawCircle(radius: Int, x: Int, y: Int)
    }
    
    class RedCircle() : DrawAPI {
        override fun drawCircle(radius: Int, x: Int, y: Int) {
            println(
                "Drawing Circle[ color: red, radius: "
                        + radius + ", x: " + x + ", " + y + "]"
            )
        }
    }
    
    class GreenCircle() : DrawAPI {
        override fun drawCircle(radius: Int, x: Int, y: Int) {
            println(
                "Drawing Circle[ color: green, radius: "
                        + radius + ", x: " + x + ", " + y + "]"
            )
        }
    }
    
    abstract class BridgeShape protected constructor(protected var drawAPI: DrawAPI) {
        abstract fun draw()
    }
    
    class BridgeCircle(private val x: Int, private val y: Int, private val radius: Int,drawAPI: DrawAPI) :
        BridgeShape(drawAPI) {
        override fun draw() {
            drawAPI.drawCircle(radius, x, y)
        }
    }
    
    fun main() {
        val redCircle: BridgeShape = BridgeCircle(100, 100, 10, RedCircle())
        val greenCircle: BridgeShape = BridgeCircle(100, 100, 10, GreenCircle())
        redCircle.draw()
        greenCircle.draw()
    }
    

    6.组合模式

    定义:有时又叫作部分-整体模式,它是一种将对象组合成树状的层次结构的模式,用来表示“部分-整体”的关系,使用户对单个对象和组合对象具有一致的访问性

    class Employee(private val name: String, private val dept: String, private val salary: Int) {
        private val subordinates: MutableList<Employee>
        fun add(e: Employee) {
            subordinates.add(e)
        }
    
        fun remove(e: Employee) {
            subordinates.remove(e)
        }
    
        fun getSubordinates(): List<Employee> {
            return subordinates
        }
    
        override fun toString(): String {
            return ("Employee :[ Name : " + name
                    + ", dept : " + dept + ", salary :"
                    + salary + " ]")
        }
        //构造函数
        init {
            subordinates = ArrayList()
        }
    }
    

    7享元设计模式

    定义:主要用于减少创建对象的数量,以减少内存占用和提高性能。这种类型的设计模式属于结构型模式,它提供了减少对象数量从而改善应用所需的对象结构的方式。

    interface IFlyweight {
        fun print()
    }
    
    class Flyweight(val id: String) : IFlyweight {
    
        override fun print() {
            println("Flyweight.id = $id ...")
        }
    }
    
    class FlyweightFactory {
        private val flyweightMap: MutableMap<String?, IFlyweight?> = HashMap<Any?, Any?>()
        fun getFlyweight(str: String?): IFlyweight {
            var flyweight = flyweightMap[str]
            if (flyweight == null) {
                flyweight = Flyweight(str!!)
                flyweightMap[str] = flyweight
            }
            return flyweight
        }
    
        fun getFlyweightMapSize(): Int {
            return flyweightMap.size
        }
    }
    
        fun main(args: Array<String>) {
            val flyweightFactory = FlyweightFactory()
            val flyweight1 = flyweightFactory.getFlyweight("A")
            val flyweight2 = flyweightFactory.getFlyweight("B")
            val flyweight3 = flyweightFactory.getFlyweight("A")
            flyweight1.print()
            flyweight2.print()
            flyweight3.print()
            println(flyweightFactory.getFlyweightMapSize())
       }
    

    相关文章

      网友评论

          本文标题:设计模式-结构型设计模式

          本文链接:https://www.haomeiwen.com/subject/amsoartx.html