10.6单例模式

Posted zhen1996

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了10.6单例模式相关的知识,希望对你有一定的参考价值。

2018-10-6 18:09:52

单例对象: 第一次导入模块的时候已经在模块里面实例化好了

在Django中 admin.site  site就是一个单例,被导入一次后面都直接用啦

 

只要在Django中导入一次模块,无论在其他地方再导入还是用的一个对象,

不是把代码非得写在一个脚本里面才是一个程序! Django本身就是一个程序,里面的文件只要导入一次后面无论再导入都是一个对象

记住: 只要是同一个文件,只要导入过一次,就从对应的文件拿数据,

func.py

from mysingleton import my_singleton

def bar():
    print(id(my_singleton))


import time

mysingleton.py

from mysingleton import my_singleton

def bar():
    print(id(my_singleton))


import time

main.py

# class Person(object):
#
#     def __init__(self,name,age):
#         self.name=name
#         self.age=age
#
#
# alex=Person("alex",33)
# egon=Person("egon",32)

# 单例模式


# 单例模式方式1 :__new__

# class Singleton(object):
#
#     _instance = None
#     def __new__(cls, *args, **kw):
#         if not cls._instance:
#             cls._instance = super(Singleton, cls).__new__(cls, *args, **kw)
#         return cls._instance
#
# class MyClass(Singleton):
#     a = 1
#
#
#
# mc1=MyClass()
#
# mc2=MyClass()
#
# mc3=MyClass()


# print(id(mc1))
# print(id(mc2))
# print(id(mc3))


# 单例模式方式2 :模块方式


# from mysingleton import my_singleton,My_Singleton

# my_singleton.foo()
#
# print(id(my_singleton))


# 思考1
# from mysingleton import my_singleton as my_singleton_new
#
# print(id(my_singleton_new))
# print(id(my_singleton))

# 思考2
# import func
#
# func.bar()

# 思考3

from mysingleton import my_singleton,My_Singleton

ms1=My_Singleton()

from mysingleton import my_singleton,My_Singleton

ms2=My_Singleton()

print(id(ms1))
print(id(ms2))

笔记

day81

sql:

  单表处理:
  Book
   
   
   id title  price    publish       email      addr
   1  python  100    苹果出版社   123@qq.com   北京
   2  go      200    橘子出版社   234@qq.com   南京
   3  java    211    橘子出版社   234@qq.com   南京
   4  linux   22     橘子出版社   234@qq.com   南京   
   
   
   ###################一对多######################多表:
   
   Book
   
   
   id title    price     publish_id 
   1  python    100         1
   2  go        200         2  
   3  java      211         2   
   4  linux     22          2   
   
  Publish
  
   id      name        email        addr      
   1     苹果出版社   123@qq.com   北京
   2     橘子出版社   234@qq.com   南京
   
   
   结论: 一旦确定表关系是一对多,那么关联字段建在多的表中
   
   book:
   
   Foreignkey   publish_id  refference Publish(id)
   
   
   #######################多对多#################################
   
   Book
   
   
   id title    price     publish_id   
   1  python    100         1
   2  go        200         2  
   3  java      211         2   
   4  linux     22          2  




1 聚合与分组
  
  
       # 统计不止一个作者的图书名称:

       select book.title,Count(author.name) as c from book inner join book-authors on ...
                          inner join author on ... 
                     
       group by book.id having c>1
 
  
  
  
2 admin配置参数

    后台数据库管理页面 
    
    
    
    admin model:
    
    对每一个app下的model设计出增伤改查4个url
    
        查看book:
        http://127.0.0.1:8000/admin/app01/book/
        添加book: 
        http://127.0.0.1:8000/admin/app01/book/add/    
        编辑book:
        http://127.0.0.1:8000/admin/app01/book/1/change/
        删除book:
        http://127.0.0.1:8000/admin/app01/book/1/delete/


    注册:
       from django.contrib import admin

            # Register your models here.

            from .models import *

            from django.utils.safestring import mark_safe


            class BookConfig(admin.ModelAdmin):

                def deletes(self):
                    return mark_safe("<a href=‘‘>删除</a>")



                list_display = ["title","price","publishDate",publish,deletes]
                list_display_links = ["price"]
                list_filter=["price","title","authors","publish"]
                list_editable=["title",]

                search_fields=["title","price"]




                def patch_init(self,request,queryset):

                    queryset.update(price=100)

                patch_init.short_description = "批量初始化"

                actions = [patch_init,]

                change_list_template="list.html"
                fields = (title,)


            admin.site.register(Book,BookConfig)


            admin.site.register(Publish)
            admin.site.register(Author)
            admin.site.register(AuthorDetail)



Admin的实现流程:
        1 启动:
              import admin
              def autodiscover():
                   autodiscover_modules(admin, register_to=site)
                   
                   
               执行每一个app下的admin.py文件
        2 注册
        
              单例模式
              
              
        
        
        3 设计URL
    
    
    
Xadmin组件





3 ModelForm组件

CRM 

1 Xadmin组件
2 权限
3 crm



‘‘‘ 
  select Max(book.price) as max_price  from author inner join book_authors on ...
                         inner join join book    on ...  

  group by author.name
    

 ‘‘‘



   
   
   
   
   
    Author
    
    id   name  age
    1    alex   38
    2    liwen  43
    
    
    Author_Book
    
    id   author_id    book_id 
    1       1            1
    2       1            2
    3       2            2
    
    
    结论;一旦是多对多的关系:需要建立第三张关系表
    
    #######################一对一#################################
    
    
    Author
    
    id   name  age     
    1    alex   38     
    2    liwen  43    
    
    
    AuthorDetail     
    
    id   tel     gf     author_id(unique)
    1    131    钢蛋        1 
    2    121    铁锤        2

    
    
    
    
    
SELECT 
"app01_book"."title" FROM "app01_publish" 
LEFT OUTER JOIN "app01_book" ON ("app01_publish"."nid" = "app01_book"."publish_id")
 WHERE "app01_publish"."name" = 苹果出版社


SELECT 
"app01_book"."title" FROM "app01_book" INNER JOIN 
"app01_publish" ON ("app01_book"."publish_id" = "app01_publish"."nid") 
WHERE "app01_publish"."name" = 苹果出版社


SELECT
 
"app01_book"."title", "app01_publish"."name"
 FROM "app01_book" INNER JOIN 
 "app01_book_authors" ON ("app01_book"."nid" = "app01_book_authors"."book_id") 
 INNER JOIN "app01_author" ON ("app01_book_authors"."author_id" = "app01_author"."nid") INNER JOIN "app01_authordetail" ON ("app01_author"."authorDetail_id" = "app01_authordetail"."nid")
 INNER JOIN "app01_publish" ON ("app01_book"."publish_id" = "app01_publish"."nid") 
 
 WHERE "app01_authordetail"."telephone" LIKE 151% ESCAPE 
 

 

以上是关于10.6单例模式的主要内容,如果未能解决你的问题,请参考以下文章

常用代码片段

常用代码片段

性能比较好的单例写法

片段作为 Android 中的单例

单例片段或保存网页视图状态

你熟悉的设计模式都有哪些?写出单例模式的实现代码