C#设计模式扩展——Meditor+Proxy模式实现

这里我们做一个简单的设计模式的组合,中介者模式+代理模式。代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProxyAndMeditor0922
{
    public class Agent:AbstractNeeder
    {
        public Agent(AbstractMeditor meditor)
            : base(meditor)
        {

        }
        private Needer need;
        public Needer Need
        {
            get { return need; }
            set { need = value; }
        }
        public void Sending(string message)
        {
            meditor.Send(message,need);
        }
        public void Notify(string message)
        {
            Console.WriteLine("待业者收到信息:" + message);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProxyAndMeditor0922
{
    public class Meditor:AbstractMeditor
    {
        private employeer employeering;

        public employeer Employeering
        {
            get { return employeering; }
            set { employeering = value; }
        }


        private Agent agent;

        public Agent Agent
        {
            get { return agent; }
            set { agent = value; }
        }




        public override void Send(string Message, AbstractNeeder employeer)
        {
            if (employeer == employeering)
            {
                employeering.Sending(Message);
            }
            else
            {
                agent.Sending(Message);
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProxyAndMeditor0922
{
    public class Needer:AbstractNeeder
    {
           public Needer(AbstractMeditor meditor)
            : base(meditor)
        { }
        public void Sending(string message)
        {
            meditor.Send(message,this);
        }
        public void Notify(string message )
        {
            Console.WriteLine("待业者收到信息:"+message);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProxyAndMeditor0922
{
    public class employeer:AbstractNeeder
    {
        public employeer(AbstractMeditor meditor)
            : base(meditor)
        { }
        public void Sending(string message)
        {
            meditor.Send(message,this );
        }
        public void Notify(string message )
        {
            Console.WriteLine("企业收到信息:"+message);
        }
    }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProxyAndMeditor0922
{
    public abstract class AbstractNeeder
    {
        protected AbstractMeditor meditor;
        public AbstractNeeder(AbstractMeditor meditor)
        {
            this.meditor = meditor;
        }

    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProxyAndMeditor0922
{
    public abstract class AbstractMeditor
    {
        public abstract void Send(string Message, AbstractNeeder employeer);
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProxyAndMeditor0922
{
    class Program
    {
        static void Main(string[] args)
        {
            Meditor md = new Meditor();
            Needer nd = new Needer(md);
            employeer ep = new employeer(md);
            Agent ag = new Agent(md);

            ag.Notify("我需要一个熟练使用unity3D的开发!");
            ep.Notify("我有Unity3D的开发经验,我想找一份与之相关的工作!");
            Console.ReadKey();
        }
    }
}

以下是一个简单的 Android 登录页面的示例,并包含记住账号密码功能的实现代码: 1. activity_login.xml ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <EditText android:id="@+id/username_edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Username" /> <EditText android:id="@+id/password_edittext" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword" /> <CheckBox android:id="@+id/remember_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Remember Me" /> <Button android:id="@+id/login_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Log In" /> </LinearLayout> ``` 2. LoginActivity.java ``` public class LoginActivity extends AppCompatActivity { private EditText mUsernameEditText; private EditText mPasswordEditText; private CheckBox mRememberCheckbox; private SharedPreferences mSharedPreferences; private SharedPreferences.Editor mEditor; private static final String PREFS_NAME = "MyPrefsFile"; private static final String KEY_USERNAME = "username"; private static final String KEY_PASSWORD = "password"; private static final String KEY_REMEMBER = "remember"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); mUsernameEditText = findViewById(R.id.username_edittext); mPasswordEditText = findViewById(R.id.password_edittext); mRememberCheckbox = findViewById(R.id.remember_checkbox); mSharedPreferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); mEditor = mSharedPreferences.edit(); boolean rememberMe = mSharedPreferences.getBoolean(KEY_REMEMBER, false); mRememberCheckbox.setChecked(rememberMe); if (rememberMe) { String username = mSharedPreferences.getString(KEY_USERNAME, ""); String password = mSharedPreferences.getString(KEY_PASSWORD, ""); mUsernameEditText.setText(username); mPasswordEditText.setText(password); } Button loginButton = findViewById(R.id.login_button); loginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String username = mUsernameEditText.getText().toString().trim(); String password = mPasswordEditText.getText().toString().trim(); if (username.isEmpty() || password.isEmpty()) { Toast.makeText(LoginActivity.this, "Please enter username and password", Toast.LENGTH_SHORT).show(); } else { if (mRememberCheckbox.isChecked()) { mEditor.putString(KEY_USERNAME, username); mEditor.putString(KEY_PASSWORD, password); mEditor.putBoolean(KEY_REMEMBER, true); mEditor.apply(); } else { mEditor.clear(); mEditor.apply(); } // Do login logic here } } }); } } ``` 在上面的代码中,我们使用了 Android 的 SharedPreferences 来存储记住的账号密码信息。如果用户勾选了“记住我”的复选框,则将账号密码信息存储到 SharedPreferences 中,并在下次打开应用时自动填充。如果用户没有勾选“记住我”的复选框,则清除 SharedPreferences 中的账号密码信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值