AndroluaDocs AndroluaDocs
首页
文章
项目
资源
GitLab (opens new window)
首页
文章
项目
资源
GitLab (opens new window)
  • 工程架构

    • 认识工具
    • 基类构建/封装
      • 如何封装?
      • 封装举例
        • 详情
    • 项目的维护与Git使用
    • 软件自签名&发版维护
    • 错误捕捉&Debug
  • 脚手架构建
  • Dex相关

    • Dex介绍&自打包教程
    • 常用Dex库的下载与介绍
  • 常见实现

    • 应用远程更新及热更新实践
    • 爬虫程序实践
    • 图片载入
    • 音乐应用
  • 优化重构

    • 线程介绍与分离UI&数据层
    • 配色设计&美化优化应用
  • 电脑开发 luajava 应用
  • 项目
  • 工程架构
可可
2022-04-15
目录

基类构建/封装

本节谈谈工程架构中的封装与工具类,本节需要你的练习。

封装其实只是把一些重要或重复的代码拿出来打包成可以重复使用的库。 这对小白来说可能并不清楚,但当你开发项目就会发觉封装的好处。

# 如何封装?

首先,一个成功的封装应具备以下条件:

  • 1.简化逻辑

逻辑思路要清晰简单。 反例:“封装”了一个弹窗类,但用户必须要额外调用很多准备的琐碎方法才能使用。

  • 2.灵活性

比如业务逻辑需要取10条Rss展示,但你不能在基类里面写死 itemNum=10 ,这应该动态生成。

  • 3.可拓展性强

如果你写的代码适合在不同页面内使用,那么它可以称为一个封装类。 比如:封装了一个baseFile.lua,用于文件相关存读删创,那么它可以算一个很好的库。

  • 4.注释详细

  • 5.源于需求

  • 6.低耦合高内聚

解决问题。符合你的项目需求,不额外扩展。比如一个网络提交类里就不应该get.

  • 7.不变性 封装必须尽量不和业务需求打交道。不能因需求微小变动而大规模重构

# 封装举例

Yaml

使用 yaml.so库 构建的封装类,多用于设置

- name: Yaml
  desc: 使用 yaml.so库 构建的封装类,多用于设置
  avatar: https://s1.328888.xyz/2022/04/15/iRtoW.png
  link: https://github.com/sudoskys/Tiebar/blob/main/run/Yamlkit.lua
  bgColor: '#CBEAFA' # 可选,默认var(--bodyBg)。颜色值有#号时请添加单引号
  textColor: '#6854A1' # 可选,默认var(--textColor)

# 详情

点击查看
--about
--author = 'sudoskys'
--project = 'Tiebar'
--url = github@sudoskys-Tiebar
require "import"
import "android.app.*"
import "android.os.*"
import "android.widget.*"
import "android.view.*"
YAML = {}
function YAML:new()
  local funt = {}
  setmetatable(funt, self)
  self.__index = self
  --self.debuging = p["debug"]
  self.HOME = activity.getLuaDir()
  self.author= ("sudoskys")
  self.libyaml=require"yaml"
  self.road=function(path)
    import "java.io.File"
    if path then
      path=File(path).getAbsolutePath()
      if File(path).isFile() then
        return path
       else
        path=activity.getLuaDir()..path
        if File(path).isFile() then
          return path
         else
          return false
        end
      end
     else
      print("miss path")
    end
  end
  return funt
end
--定义类的方法

function YAML:loadf(path)
  reads=function(path)
    local content=io.open(path):read("*a")
    return content
  end
  if self.road(path) then
    as=reads(self.road(path))
    dcon=self.libyaml.load(as)
    return dcon
   else
    print("NO file  "..path)
    return false
  end
end

function YAML:dumpf(tables,path)
  save=function(content,path)
    import "java.io.File"
    local f=File(tostring(File(tostring(path)).getParentFile())).mkdirs()
    io.open(tostring(path),"w"):write(tostring(content)):close()
  end
  if type(tables) == type({}) or type(tables) == type(["x"]) then
    dcon=self.libyaml.dump(tables)
    return save(dcon,self.road(path))
   else
    print("STOP, Not table")
    return false
  end
end

function YAML:load(str)
  if type(str) == type("x") then
    dcon=self.libyaml.load(str)
    return dcon
   else
    print("Not string....CANT load")
    return false
  end
end

function YAML:dump(tables)
  if type(tables) == type({}) or type(tables) == type(["x"]) then
    dcon=self.libyaml.dump(tables)
    return dcon
   else
    print("Not table....CANT dump")
    return false
  end
end

--[[
path="/values/color/setting.yaml"
con=yaml:loadf(path)
--print(dump(con))
--]]
Gitlab在线编辑&贡献你的智慧 (opens new window)
上次更新: 2022/08/03, 09:42:19
认识工具
项目的维护与Git使用

← 认识工具 项目的维护与Git使用→

Theme by Vdoing | Copyright © 2022-2022 Created by coco | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式